如何统一检测鼠标点击GUITexture

How to detect mouse click on GUITexture in unity

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

我没明白,如果点击方向按钮(GUITexture)如何让玩家移动

按钮脚本

使用UnityEngine; 使用 System.Collections;

public class 右按钮: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,它给出了无效参数错误 所以我无法添加如何检查按钮是否被点击

谢谢

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