【问题标题】:How to script a Button press in C#, to trigger another event?如何在 C# 中编写按钮按下脚本以触发另一个事件?
【发布时间】:2015-03-11 02:57:41
【问题描述】:

我正在使用 Vuforia 开发 Unity。

我有虚拟按钮,我需要像键盘上的向上/向下箭头一样移动不在其图像目标中的对象,所以我正在寻找基础知识。

我的课是这样开始的:

public void OnButtonPressed(VirtualButtonAbstractBehaviour vb){
...
}

我需要在其中添加什么才能使它像向上按钮一样?

如果没有这些虚拟按钮,我的脚本会像这样移动对象:

void FixedUpdate(){
        float moveHortizonal = Input.GetAxis("Horizontal");
        float moveVertical = Input.GetAxis ("Vertical");

        Vector3 movement = new Vector3 (moveHortizonal, 0, moveVertical);

        rigidbody.AddForce (movement * speed);
    }

【问题讨论】:

    标签: c# unity3d vuforia virtual-reality


    【解决方案1】:

    想出了如何去做,这里有一个关于我的发现的小教程

    首先,您从注册按钮开始:

    void Start () {
            VirtualButtonBehaviour[] vbs = transform.GetComponentsInChildren<VirtualButtonBehaviour> ();
    
            for (int i=0; i < vbs.Length; ++i) {
                vbs[i].RegisterEventHandler(this);
            }
    

    现在你继续,从按钮的功能开始

       public void OnButtonPressed(VirtualButtonAbstractBehaviour vb){
        //specify which button you want to function by using the if statement
        if(vb.name=="ButtonName") { callButtonfunction();}
        }
    

    类似地,如果您希望按钮在发布时执行某项操作:

       public void OnButtonReleased(VirtualButtonAbstractBehaviour vb){
        //specify which button you want to function by using the if statement
        if(vb.name=="ButtonName") { callButtonfunction();}
        }
    

    如果您希望您的按钮控制游戏对象,请继续在类中将游戏对象声明为公共变量,以便可以在检查器中对其进行访问并进行相应的分配。

    public GameObject human;

    其中GameObject 是变量类型,human 是我们用作参考的变量名称

    就这么简单。

    Vuforia 日志的记录非常糟糕,因此您几乎永远无法 从那里得到答案,所以我希望这会有所帮助。

    【讨论】:

    • 发现这很有帮助!这在其他任何地方都没有正确提及。
    • 不客气 :) 是的,Vuforia 的文档并不是最好的。
    猜你喜欢
    • 1970-01-01
    • 2011-11-23
    • 1970-01-01
    • 2021-12-21
    • 1970-01-01
    • 2011-02-06
    • 2014-04-24
    • 2020-08-01
    • 2021-07-18
    相关资源
    最近更新 更多