如何检测鼠标单击GUITexture

时间:2015-05-23 10:38:30

标签: c# android unity3d click touch

我正在尝试通过鼠标添加控件来代替游戏中的键盘。 我通过GUI纹理统一添加了4个移动键和1个激活按钮。 在游戏中已经存在一个通过键盘敲击来控制玩家的玩家控制器

我没有得到,如果单击方向按钮(GUITexture),如何让玩家移动

按钮脚本

使用UnityEngine; 使用System.Collections;

公共类RightButton:MonoBehaviour {

public Texture2D bgTexture;
public Texture2D airBarTexture;
public int iconWidth = 32;
public Vector2 airOffset = new Vector2(10, 10);


void start(){
    }

void OnGUI(){
    int percent = 100;

    DrawMeter (airOffset.x, airOffset.y, airBarTexture, bgTexture, percent);
}

void DrawMeter(float x, float y, Texture2D texture, Texture2D background, float percent){
    var bgW = background.width;
    var bgH = background.height;

    GUI.DrawTexture (new Rect (x, y, bgW, bgH), background);

    var nW = ((bgW - iconWidth) * percent) + iconWidth;

    GUI.BeginGroup (new Rect (x, y, nW, bgH));
    GUI.DrawTexture (new Rect (0, 0, bgW, bgH), texture);
    GUI.EndGroup ();


}

}

我无法添加GUI按钮代替GUI.DrawTexture,它给出了无效的参数错误 所以我无法添加如何检查按钮是否被点击

由于

1 个答案:

答案 0 :(得分:1)

GUITexture是传统GUI系统的一部分。如何将其作为按钮使用的示例是here

using UnityEngine;
using System.Collections;

public class RightButton : MonoBehaviour {

    public Texture bgTexture;
    public Texture airBarTexture;
    public int iconWidth = 32;
    public Vector2 airOffset = new Vector2(10, 10);


    void start(){
    }

    void OnGUI(){
        int percent = 100;

        DrawMeter (airOffset.x, airOffset.y, airBarTexture, bgTexture, percent);
    }

    void DrawMeter(float x, float y, Texture texture, Texture background, float percent){
        var bgW = background.width;
        var bgH = background.height;

        if (GUI.Button (new Rect (x, y, bgW, bgH), background)){
            // Handle button click event here
        }

        var nW = ((bgW - iconWidth) * percent) + iconWidth;

        GUI.BeginGroup (new Rect (x, y, nW, bgH));
        GUI.DrawTexture (new Rect (0, 0, bgW, bgH), texture);
        GUI.EndGroup ();
    }
}