Wednesday, December 9, 2015

How to: Add Persistent Bool Listener programatically - Unity 5

 

    State is ScriptableObject so your persistence can be done on something persistent (Prefab).
If event is on Scene GameObject then you can add presistent listener from some of the scene GameObjects;

               if(PrefabUtility.GetPrefabType(serializedProperty.serializedObject.targetObject)==PrefabType.Prefab)
                   prefab=serializedProperty.serializedObject.targetObject;
                else
                   prefab=PrefabUtility.GetPrefabParent(serializedProperty.serializedObject.targetObject);



               
UnityAction<boolm=(UnityAction<bool>)Delegate.CreateDelegate(typeof(UnityAction<bool>),prefab,typeof(MonoBehaviour).GetProperty("enabled").GetSetMethod());

                if(objPrev!=null){


                    UnityEventTools.RemovePersistentListener<bool>((objPrev as State).OnEnter,m);
                    UnityEventTools.RemovePersistentListener<bool>((objPrev as State).OnExit,m);

                }
                    
                UnityEventTools.AddBoolPersistentListener((serializedProperty.objectReferenceValue as State).OnEnter,m,true);
                UnityEventTools.AddBoolPersistentListener((serializedProperty.objectReferenceValue as State).OnExit,m,false);

No comments:

Post a Comment