Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to create dynamic sidebar ( menu ) for unity app

I want to create a sidebar (like nav drawer in android) for my AR Unity app, when I touch left border of screen and drag to right, a sidebar should appear with a list of buttons such as (settings, about us..).

like image 550
Ahmad Avatar asked Nov 09 '22 06:11

Ahmad


1 Answers

Just something quick I whipped up. This should get you started.

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;
        }
    }
}
like image 147
Puneet Avatar answered Nov 14 '22 21:11

Puneet