我希望为我的AR Unity应用程序创建一个侧边栏(类似于Android中的导航抽屉),当我触摸屏幕的左边缘并向右拖动时,应该出现一个侧边栏,其中包含一系列按钮,例如(设置、关于我们...)。
我希望为我的AR Unity应用程序创建一个侧边栏(类似于Android中的导航抽屉),当我触摸屏幕的左边缘并向右拖动时,应该出现一个侧边栏,其中包含一系列按钮,例如(设置、关于我们...)。
我匆忙为您准备了一些东西。这应该可以帮助您入门。
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;
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ToggleInfo : MonoBehaviour
{
public GameObject panel;
int counter;
public void toggle()
{
counter++;
if(counter%2==1)
{
panel.gameObject.SetActive(false);
}
else
{
panel.gameObject.SetActive(true);
}
}
}
- 确保面板在检视器中的名称为“panel”,并且它最初处于禁用状态。
2.将此脚本附加到按钮上,并将您的“panel”拖放到检视器面板中脚本的公共游戏对象中。
3.从按钮的onclick调用toggle函数。