Android 如何为unity应用程序创建动态侧栏(菜单)
我想为我的AR Unity应用程序创建一个侧边栏(比如android中的nav drawer),当我触摸屏幕的左边框并向右拖动时,侧边栏应该会显示一个按钮列表,例如(设置,关于我们…)。这是我快速创建的。这应该让你开始Android 如何为unity应用程序创建动态侧栏(菜单),android,unity3d,augmented-reality,vuforia,Android,Unity3d,Augmented Reality,Vuforia,我想为我的AR Unity应用程序创建一个侧边栏(比如android中的nav drawer),当我触摸屏幕的左边框并向右拖动时,侧边栏应该会显示一个按钮列表,例如(设置,关于我们…)。这是我快速创建的。这应该让你开始 using UnityEngine; using System.Collections; using UnityEngine.UI; public class SlidePanel : MonoBehaviour { //Process touch for panel
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;
}
}
}
使用UnityEngine;
使用系统集合;
使用UnityEngine.UI;
公共类幻灯片面板:MonoBehavior
{
//如果触摸低于此阈值,则处理触摸以打开面板显示。
专用浮动左边缘=屏幕宽度*0.25f;
//显示/隐藏面板的最小滑动距离。
浮动开关间距=10f;
浮式startXPos;
bool processTouch=false;
bool-isExpanded=false;
公共动画;
无效更新(){
如果(输入.触摸.长度>0)
面板(Input.GetTouch(0));
}
空心面板(触摸式)
{
开关(接触相){
案例接触阶段。开始:
//获得触摸屏的起始位置。
startXPos=touch.position.x;
Debug.Log(startXPos);
//检查是否需要处理此触摸以显示面板。
if(startXPosSwipedInstance),则为else
{
panelAnimation.CrossFade(“SlideIn”);
isExpanded=true;
}
startXPos=0f;
processTouch=false;
}
打破
违约:
返回;
}
}
}
我建议将此问题分为两个单独的问题。当我尝试将动画添加到脚本检查器时,它不会接受动画,并且仍然为空!我如何解决这个问题?只需将包含动画组件的游戏对象拖放到inspector中的脚本中即可。注意:它使用传统动画(不是Animator);如何创建遗留动画?我使用“窗口->动画”创建动画,但无法将其拖放到脚本检查器中。我不知道我是否使用了正确的方法!1) 将动画组件添加到要设置动画的面板中。2) Windows->Animation以创建动画。如果不执行1,默认情况下Unity将创建与Animator兼容的动画,而不是传统动画。3) 制作动画后,将面板拖放到脚本检查器
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);
}
}
}