【问题标题】:how to create dynamic sidebar ( menu ) for unity app如何为统一应用程序创建动态侧边栏(菜单)
【发布时间】:2015-11-19 00:48:48
【问题描述】:

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

【问题讨论】:

  • 我建议将其分成 2 个单独的问题

标签: android unity3d augmented-reality vuforia


【解决方案1】:

我很快就做了一些事情。这应该可以帮助您入门。

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;
        }
    }
}

【讨论】:

  • 当我尝试向脚本检查器添加动画时,它不接受它并保持为空!我该如何解决?
  • 只需将具有动画组件的游戏对象拖放到检查器中的此脚本中即可。注意:它使用旧版动画(不是 Animator);
  • 如何创建遗留动画?我使用 Window-> 动画来创建动画,但我无法将其拖放到脚本检查器中。不知道我用对了没有!
  • 1) 将动画组件添加到您想要动画的面板中。 2) Windows->Animation 创建动画。如果不执行 1,默认情况下 Unity 将创建与 Animator 兼容的动画,而不是 Legacy Animation。 3)制作动画后,将PANEL拖放到脚本检查器中。
  • 要了解更多信息,我强烈建议您通过 unity3d.com/learn/tutorials/modules/beginner/editor/… 了解 Unity 基础知识
【解决方案2】:
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);
    }
  }
}
  1. 确保面板的名称在检查器中为“panel”并且它最初被禁用。

2.将此脚本附加到按钮上,然后将您的“面板”拖到检查器面板中脚本的公共游戏对象中。

3.从按钮onclick调用toggle函数。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-03-25
    • 2019-03-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-09-26
    相关资源
    最近更新 更多