// Start is called before the first frame update
    void Start()
    {
        if (instance != null)
        {
            //There is another instance already present, remove this one
            Destroy(gameObject);
            return;
        }
        //Assign this instance to a static variable so you can access the movement direction directly at MobileJoystick_UI.instance.moveDirection
        instance = this;

        //This function will initialize canvas element along with the joystick button
        GameObject tmpObj = new GameObject("Canvas");

        tmpObj.transform.position = Vector3.zero;
        mainCanvas              = tmpObj.AddComponent <Canvas>();
        mainCanvas.renderMode   = RenderMode.ScreenSpaceOverlay;
        mainCanvas.pixelPerfect = true;

        //Add Canvas Scaler component
        CanvasScaler canvasScaled = tmpObj.AddComponent <CanvasScaler>();

        canvasScaled.scaleFactor            = 1;
        canvasScaled.referencePixelsPerUnit = 100;

        //Add Graphic Raycaster element
        tmpObj.AddComponent <GraphicRaycaster>();

        //Setup navigation background
        GameObject cntrlTmpObj = new GameObject("Movement Circle");

        cntrlTmpObj.transform.position    = Vector3.zero;
        cntrlTmpObj.transform.parent      = tmpObj.transform;
        moveTouch.backgroundCircle        = cntrlTmpObj.AddComponent <Image>();
        moveTouch.backgroundCircle.sprite = navigationCircle;
        moveTouch.backgroundCircle.rectTransform.anchorMin = new Vector2(0, 0);
        moveTouch.backgroundCircle.rectTransform.anchorMax = new Vector2(0, 0);
        moveTouch.backgroundCircle.rectTransform.sizeDelta = new Vector2(circleSize, circleSize);
        moveTouch.backgroundCircle.rectTransform.pivot     = new Vector2(0, 0);
        moveTouch.backgroundCircle.rectTransform.position  = new Vector3(marginLeft, marginBottom, 0);

        //Navigation button
        cntrlTmpObj = new GameObject("Movement Button");
        cntrlTmpObj.transform.position = Vector3.zero;
        cntrlTmpObj.transform.parent   = tmpObj.transform;
        moveTouch.mainButton           = cntrlTmpObj.AddComponent <Image>();
        moveTouch.mainButton.sprite    = navigationButton;
        moveTouch.mainButton.rectTransform.anchorMin = new Vector2(0, 0);
        moveTouch.mainButton.rectTransform.anchorMax = new Vector2(0, 0);
        moveTouch.mainButton.rectTransform.sizeDelta = new Vector2(buttonSize, buttonSize);
        moveTouch.mainButton.rectTransform.pivot     = new Vector2(0, 0);
        moveTouch.mainButton.rectTransform.position  = new Vector3(marginLeft + (circleSize - buttonSize) / 2, marginBottom + (circleSize - buttonSize) / 2, 0);

        //Save the default location of the joystick button to be used later for input detection
        moveTouch.defaultArea = new Rect(moveTouch.mainButton.rectTransform.position.x,
                                         moveTouch.mainButton.rectTransform.position.y,
                                         moveTouch.mainButton.rectTransform.sizeDelta.x,
                                         moveTouch.mainButton.rectTransform.sizeDelta.y);
    }
    public void EnableInput(bool enable)
    {
        if (enable)
        {
            joyStick         = Instantiate(joyStickPrefab, Vector2.zero, new Quaternion(0, 0, 0, 0));
            dragDirIndicator = joyStick.GetComponent <JoyStickUI>();
            joyStick.SetActive(false);
            inputUpdateCoroutine = StartCoroutine(InputUpdate());
        }
        else
        {
            if (joyStick != null)
            {
                joyStick.SetActive(false);
            }

            if (inputUpdateCoroutine != null)
            {
                StopCoroutine(inputUpdateCoroutine);
                inputUpdateCoroutine = null;
            }
        }
    }
Exemple #3
0
 public override void onAwake()
 {
     mIns      = this;
     viewPivot = this.CacheTrans.Find("bg").GetComponent <RectTransform>();
     pot       = this.CacheTrans.Find("bg/bar").GetComponent <RectTransform>();
 }