我有2個gui紋理。在GUI紋理上檢測觸摸
根據屏幕寬度和高度,我把它放在gui中。
一個用於操縱桿,另一個用於射手。
現在觸摸射手遊戲杆移動到該特定部分。
我用rect.Contains。
void Start() {
xx = Screen.width - Screen.width/12;
yy = Screen.height - Screen.height/8;
lb = Screen.width/10;
rect = new Rect(-xx/2, -yy/2, lb, lb);
shooter.pixelInset = rect;
shooter.enabled = false;
}
void OnGUI(){
if(characterScript.playbool){
shooter.enabled = true;
}
if (rect.Contains(Event.current.mousePosition)){
shootBool = true;
print("shoot");
alert.text="shoot";
}
}
對我來說工作不正常。認爲空間座標不同於gui座標。如何解決這個問題。任何人都可以建議其他好方法
獲取錯誤錯誤CS1061:類型'UnityEngine.Rect'不包含'HitTest'的定義,並且沒有找到'UnityEngine.Rect'類型的擴展方法'HitTest'(你是否缺少using指令或程序集參考?) – Sona 2013-03-18 10:14:39
哦,原諒我,只適用於GUI元素。你可以試試'rect.Contains(touch.position)'! – Joetjah 2013-03-18 10:33:19
我編輯了我的答案以反映這一點。 – Joetjah 2013-03-18 10:33:43