jueves, 24 de octubre de 2013

SetActiveRecursively, Unity4 ,and older unity3.x projects




If you upgrade some projects from unity 3.x to unity 4, you will notice that the old SetActiveRecursively is obsolete.

There are a newer functionality regarding active and desactive gameObjects, now gameObject.active (or SetActive) do the same as SetActiveRecursivley, but with one difference:
Each gameObject on the hierarchy has their own active state, and the gameObject will be (or not) active if their own active state is true, and the state of their parents are true.

This is really cool, because you can active recursively a gameObject with some desactived gameObjects, but it´s not good if your projects come from older unity version:

If you check the active checkbox on the Inspector, the gameObject will be active without the old popUp window ask you for activate the childs too.

If you try it with scenes created on unity4 you don´t have this problem, but you can find this project on older scenes (from unity 3.x) with disabled gameObject, you will have this problem, and the solution:

Extend the contextMenu for Transform Component to add active and desactive recurively:



 

static function _activeRecursive (command:MenuCommand){  

       var mTransform : Transform = command.context;  
       activeRecursively(mTransform,true); 
 
}  


@MenuItem ("CONTEXT/Transform/DesActiveRecursive")  
static function _desActiveRecursive (command:MenuCommand){
  
       var mTransform : Transform = command.context;  
       activeRecursively(mTransform,false);  
}  

static function activeRecursively(mTransform:Transform,mState:boolean){  
        
      mTransform.gameObject.SetActive(mState);  
      for ( var child : Transform in mTransform){  
            activeRecursively(child,mState);  
       }  
}  




I had some problems using the graphics emulation (gles), so disable it.
After using the script the first time, you can use the new way: the checkbox from inspector.



No hay comentarios:

Publicar un comentario