GameObject的Active與InActive activeSelf, activeInHierarchy, SetActiveRecursively

http://blog.csdn.net/mr_jis/article/details/18502983

 

1.Script可以控制InActive的GameObject,但前提是Script所依附的GameObject不能是InActive,一旦爲InActive,自身所有控件均失效。

 

2.SetActive(bool isActive)設置GameObject是否活動,設置後,其子類物體也會變成InActive,值得注意的是,4.0以後的版本Active分了兩類,GameObject.activeSelf和 GameObject.activeInHierarchy,當一個GameObject爲InActive時,其子對象本地狀態即GameObject.activeSelf其實爲ture,但實際在屏幕上的狀態GameObject.activeInHierarchy是false。

 

3.無論子對象GameObject.activeSelf和 GameObject.activeInHierarchy是什麼狀態,一旦其父對象狀態爲InActive時,它們的便失效,無法用GetComponentsInChildren和Find的方法找到,但用可以用Transform.GetChild()的方法找到子物體。

 

4.記住SetActive改變的是自身的activeSelf和子物體的activeInHierarchy,如果你有一個子物體的activeSelf是false,就算父物體SetActive(true),該子物體的activeSelf是不變。

 

5.activeSelf=false的時候activeInHierarchy一定爲false,true同理。

 

http://www.mamicode.com/info-detail-256427.html

 

activeSelf(read only只讀):物體本身的active狀態,對應於其在inspector中的checkbox是否被勾選
activeInHierarchy(read only只讀):物體在層次中是否是active的。也就是說要使這個值爲true,這個物體及其所有父物體(及祖先物體)的activeself狀態都爲true。

一個物體要在場景中是可見的(不是隱藏的),那麼不僅僅其本身的activeSelf要爲true,其所有父物體(及祖先物體)的activeself狀態都要爲true。

總結:
activeInHierarchy狀態代表物體在場景中的實際的active狀態。實際上代表的是物體及其所有祖先物體的activeSelf狀態。而activeSelf對應於其在inspector中的checkbox是否被勾選

activeSelf狀態代表物體自身的activeSelf狀態,所以當物體本身activeSelf爲true,而其所有祖先物體的activeSelf狀態不全爲true時,這個物體的activeInHierarchy狀態爲false。

activeSelf==物體自身
activeInHierarchy==物體自身及其所有祖先物體==物體在場景中實際上是否激活

至於SetActive,改變的是物體自身的activeSelf狀態,所以,對一個物體SetActive時,其在場景中可能不會被激活,因爲其祖先物體可能存在未被激活的。
SetActiveRecursively,改變物體自身及其所有子物體的activeSelf狀態,相當於對物體自身及其所有子物體調用SetActive.
由於SetActiveRecursively已過時(obsolete),未來將移除,所以,當設置一個物體及其所有子物體的active狀態時,可以調用一下方法


 
  1. void DeactivateChildren(GameObject g, bool a) {

  2. g.activeSelf = a;

  3.  
  4. foreach (Transform child in g.transform) {

  5. DeactivateChildren(child.gameObject, a);

  6. }

  7. }

Advanced Skill :

 Using Extension Method

 


 
  1. public static class Extensions

  2. {

  3. public static void SetactivateForAllChildren(this GameObject go, bool state)

  4. {

  5. DeactivateChildren(go, state);

  6. }

  7.  
  8. public static void DeactivateChildren(GameObject go, bool state)

  9. {

  10. go.SetActive(state);

  11.  
  12. foreach (Transform child in go.transform)

  13. {

  14. DeactivateChildren(child.gameObject, state);

  15. }

  16. }

  17. }

  

Now You Can Use Like That:


 
  1. public class MyTest : MonoBehaviour {

  2.  
  3. public GameObject go;

  4. // Use this for initialization

  5. void Start () {

  6. //過時

  7. //go.SetActiveRecursively(true);

  8. go.SetactivateForAllChildren(true);

  9. }

  10. }

 

bubuko.com,布布扣

 

bubuko.com,布布扣

 

參考:

  http://blog.csdn.net/czlilove/article/details/23827267

      Unity 3.5 到 4.0升級指南 Upgrade Guide from Unity 3.5 to 4.0

      http://game.ceeger.com/Manual/UpgradeGuide3540.html

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章