![](/img/trans.png)
[英]Unity issues with Oculus OVRCamera and GUI Texture not visible
[英]Unity - Gui Button issues (Android)
我是 Unity 的新手,我正在為 android 創建我的第一個游戲,作為一個游戲。 我有這個游戲,你可以通過按一個按鈕來使用提升。 玩家可以在此過程中獲得多個提升。
目前,我正在使用此代碼來使用提升:
public void OnGUI()
{
if (GUI.RepeatButton(new Rect(20, Screen.height - 150, Screen.width/10, Screen.width/10), boostButtonIcon))
{
pressedButton = true;
//do boost stuff
}
else
{
pressedButton = false;
}
}
哪個工作得很好,除非我在手機上測試它,並且我收集了 4 個提升,所有的提升都將一次性使用。
我也嘗試過 GUI.Button 而不是 GUI.RepeatButton,但如果我使用它,則沒有任何效果。
我做錯了什么還是有更好的方法?
這很正常,因為每幀都會調用OnGUI
。 您應該檢查最后一個值是否為真,這意味着用戶沒有按下按鈕,只是按住了按鈕。 嘗試這個:
if (GUI.RepeatButton(new Rect(20, Screen.height - 150, Screen.width/10, Screen.width/10), boostButtonIcon))
{
if (!pressedButton)
{
//do boost stuff
}
else pressedButton = true;
}
希望我有所幫助!
我找到了一次性使用所有提升的原因,我使用的是 RepeatButton 而不是 Button。 盡管一開始這不起作用,但我將其與 (&& boosts > 0) 結合使用,現在可以完美運行:)
public void OnGUI()
{
if (GUI.Button(new Rect(20, Screen.height - 150, Screen.width/10, Screen.width/10), boostButtonIcon) && boosts > 0)
{
useBoostSound.Play();
rigidbody.AddForce(boostVelocity, ForceMode.VelocityChange);
boosts -=1;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.