如何在unity中检测鼠标点击的GUITexture

本文关键字:GUITexture 鼠标 检测 unity | 更新日期: 2023-09-27 17:50:27

我试图在游戏中通过鼠标代替键盘添加控件。我通过unity的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,它给出无效参数错误所以我无法添加如何检查按钮是否被点击

谢谢

如何在unity中检测鼠标点击的GUITexture

GUITexture是遗留GUI系统的一部分。这里有一个如何让它作为按钮工作的示例。

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