【问题标题】:Unity How do I set up a GUITexture as a button?Unity 如何将 GUITexture 设置为按钮?
【发布时间】:2014-06-20 21:49:58
【问题描述】:

我通过转到游戏对象>>创建其他>>GUI纹理添加了GUITexture,并在检查器中添加了它的纹理我现在想要的是当用户触摸(游戏适用于android和IOS)GUITexture向上移动立方体时我试过这段代码

var up : GUITexture;
var player : Rigidbody2D;


function Update () {

    for (var evt : Touch in Input.touches) {

        var HitTest1 = up.HitTest(evt.position);

        if (evt.phase == TouchPhase.Began) {

            if(HitTest1){

                player.rigidbody2D.AddForce(Vector2(-2,0));

                fireButton.enabled = false;

            } 
        } 
    } 
}

但它没有用。我能做什么?

【问题讨论】:

    标签: button unity3d unityscript


    【解决方案1】:
    if (GUI.Button(Rect(10,10,50,50),up))
    //Code to move cube up
    

    这将从您的纹理中创建一个 GUIButton。您需要将其放入脚本中的 OnGUI 函数中。这是 GUIButtons 参考的链接:GUIButtons

    【讨论】:

    • 是的,但这不会让我的按钮在小屏幕设备上脱离屏幕??
    • 10,10,50,50 只是设置矩形。所以十位只是按钮在屏幕上的位置,五十位是按钮的大小。我不知道这是否回答了你的问题。
    【解决方案2】:

    在您的代码中,您已将播放器声明为刚体

    var player : Rigidbody2D;
    

    及以下正在尝试访问它的刚体

    player.rigidbody2D.AddForce(Vector2(-2,0)); //this won't work
    player.AddForce(Vector2(-2,0));//*use this instead*
    


    尝试将玩家声明为游戏对象

    var player:GameObject
    

    并正确加力

    player.rigidbody2D.AddForce(Vector2(-2,0));
    

    【讨论】:

    • 'AddForce' 不是 'UnityEngine.GameObject' 的成员。
    猜你喜欢
    • 1970-01-01
    • 2021-02-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-22
    • 2013-04-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多