![](/img/trans.png)
[英]How can I use a bool flag to decide if to use reverse or not in the MyComparer class?
[英]How can I use a bool in two scripts to decide if to call a function or not?
在 FixedUpdate() 的第一個腳本中,我調用了這個函數:
private void FixedUpdate()
{
RotateView();
}
內部旋轉視圖:
public void RotateView()
{
gameObject.GetComponent<FPEMouseLook>().LookRotation(transform, m_Camera.transform);
}
在第二個腳本中,我像這樣調用 RotateView:
Whilefun.FPEKit.FPEPlayer.Instance.GetComponent<Whilefun.FPEKit.FPEFirstPersonController>().RotateView();
但是現在我想使用 RotateView 函數向腳本添加一個布爾值,以便我可以在第二個腳本中決定是否調用 FixedUpdate 中的 RotateView。
我想控制是否會在第二個腳本的 FixedUpdate 中調用 RotateView 或者更好地控制以下行:
gameObject.GetComponent<FPEMouseLook>().LookRotation(transform, m_Camera.transform);
是否執行取決於我在第二個腳本中設置的標志,例如:
Whilefun.FPEKit.FPEPlayer.Instance.GetComponent<Whilefun.FPEKit.FPEFirstPersonController>().RotateView(false);
false 所以它不會使用該行:
gameObject.GetComponent<FPEMouseLook>().LookRotation(transform, m_Camera.transform);
true 會使用它。 如果從第一個腳本中的 FixedUpdate 內部調用 RotateView,則為 false/true。
有很多方法可以做到:
[SerializeField] OtherScript foo;
通過檢查器將一個腳本分配給另一個腳本[SerializeField] OtherScript foo;
static
,然后可以通過OtherScript.foo
訪問它Start()
中的另一個組件,例如FindObjectByType()
或GetComponent<...>()
或GetComponentInChildren<...>()
,布爾可以然后是 public 或 public 但使用 { get; 私人訂制; 訪問器,或通過像GetFoo()
這樣的公共函數提供PlayerPrefs
類(但它通常會被放入另一個變量中)RotateView
調用等。這在一定程度上取決於您如何構建程序、類應該做什么、它們的作用域是什么等等。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.