如何为统一应用程序创建动态边栏(菜单)

Moh*_*mad 5 android unity-game-engine augmented-reality vuforia

我想为我的AR Unity应用程序创建一个侧边栏(如android中的导航抽屉),当我触摸屏幕的左边框并向右拖动时,侧边栏应出现并带有按钮列表,如(关于我们的设置)。 。

Pun*_*eet 2

我很快就想到了一些东西。这应该可以帮助您开始。

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class SlidePanel : MonoBehaviour
{

    //Process touch for panel display on if the touch is less than this threshold.
    private float leftEdge = Screen.width * 0.25f;

    //Minimum swipe distance for showing/hiding the panel.
    float swipeDistance = 10f;


    float startXPos;
    bool processTouch = false;
    bool isExpanded = false;
    public Animation panelAnimation;



    void Update(){
        if(Input.touches.Length>0)
            Panel(Input.GetTouch(0));
    }




    void Panel (Touch touch)
    {
        switch (touch.phase) {
        case TouchPhase.Began:
            //Get the start position of touch.

            startXPos = touch.position.x;
            Debug.Log(startXPos);
            //Check if we need to process this touch for showing panel.
            if (startXPos < leftEdge) {
                processTouch = true;
            }
            break;
        case TouchPhase.Ended:
            if (processTouch) {
                //Determine how far the finger was swiped.
                float deltaX = touch.position.x - startXPos;


                if(isExpanded && deltaX < (-swipeDistance))
                {

                    panelAnimation.CrossFade("SlideOut");
                    isExpanded = false;
                } 
                else if(!isExpanded && deltaX > swipeDistance) 
                {
                    panelAnimation.CrossFade("SlideIn");
                    isExpanded = true;
                }

                startXPos = 0f;
                processTouch = false;
            }
            break;
        default:
            return;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)