簡體   English   中英

需要使用 Unity 中的切換來隱藏/取消隱藏游戲對象(地形集合)的邏輯

[英]Need Logic for hiding/ unhiding GameObjects (Collection of terrains) using Toggles in Unity

我正在嘗試使用 Unity 的切換開關隱藏/取消隱藏包含多個子地形的游戲對象(名為“生態系統”)。 我的錯,我嘗試將 Object 設置為活動/非活動但無濟於事。 請給我一個可行的解決方案。

我正在嘗試實施的想法的圖片。 我面臨的困境的 UI 截圖; 統一版本 2020.1

我想到了; 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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM