[英]Need Logic for hiding/ unhiding GameObjects (Collection of terrains) using Toggles in Unity
我想到了; The easiest way out was to simply set Active the terrain as a whole object and then unchecking the active state at the topmost checkbox object and then directly implementing logic in Unity GUI under OnClick method boxes. 到目前為止,這為我解決了問題:)
看起來您的代碼位於您嘗試停用的 object 中。 不用擔心您不必將其移動到其他 GameObject。 您可以這樣做並從其他 object 將其關閉 (setActive(false)) 但如果您不想這樣做,這就是您可以做的。
您可以獲取生態系統的所有子對象並將它們一一關閉。 或者,如果您只想隱藏它們,就停止渲染它們(碰撞仍然存在)。
為此,我們首先開始獲取第 0 個元素並將其關閉。 然后向上移動,直到我們不能做更多。 我目前正在使用手機,所以如果某些東西不起作用或發生什么事情,我會很糟糕。 您還沒有共享您的代碼,所以我將自己編寫代碼如下:
public GameObject tog;
public bool isonnow,isonprev;
void Update(){
isonnow=tog.GetComponent<Toggle>().isOn;
//If it was off in the last frame and is on now or vice versa
if (isonnow != isonprev){
int i=0;
while (true){
if (this.transform.GetChild(i)==null)break;
else
this.transform.GetChild(i).gameObject.SetActive(isnow);
i++;
}
}
//Save current state as previous for the next frame
isonprev=isonnow;
}
希望我對您有所幫助。 讓我知道它是否有效。 祝你好運
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.