示例#1
0
    // 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 override void OnStartGame()
    {
        base.OnStartGame();

        Debug.Log("Phone Start");

        Instantiate(mobileCanvasPrefab, transform);

        joystick = GetComponentInChildren <MobileJoystick_UI>();
    }
示例#3
0
    // 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;

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

        cntrlTmpObj.transform.position    = Vector3.zero;
        cntrlTmpObj.transform.parent      = canvas.transform;
        moveTouch.backgroundCircle        = cntrlTmpObj.AddComponent <Image>();
        moveTouch.backgroundCircle.sprite = leftCircle;
        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   = canvas.transform;
        moveTouch.mainButton           = cntrlTmpObj.AddComponent <Image>();
        moveTouch.mainButton.sprite    = leftButton;
        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);
    }