public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
        {
            //TODO change to obtain controller thru property.serializedObject.Find(attribute.animator)....
            UnityEditor.Animations.AnimatorController animatorController = ((MonoBehaviour)property.serializedObject.targetObject).GetComponent <Animator> ().runtimeAnimatorController as UnityEditor.Animations.AnimatorController;

            if (animatorController != null)
            {
                //property.serializedObject.Update();

                animatorStateValues = MecanimUtility.GetAnimatorStates(animatorController);
                displayOptions      = MecanimUtility.GetDisplayOptions(animatorController);


                EditorGUI.indentLevel = 0;


                //if already have been serialzied before
                if (property.objectReferenceValue != null)
                {
                    if (animatorStateValues.Length > 0)
                    {
                        animatorStateSelected = animatorStateValues.FirstOrDefault((itm) => itm.nameHash == ((ws.winx.unity.AnimatorState)property.objectReferenceValue).nameHash) as UnityEditor.Animations.AnimatorState;
                    }
                }

                //animaStateInfoSelected = property.objectReferenceValue as UnityEditor.Animations.AnimatorState;



                // Check if it was modified this frame, to avoid overwriting the property constantly

                UnityEditor.Animations.AnimatorState prevAnimatorStateSelected = animatorStateSelected;
                animatorStateSelected = EditorGUILayoutEx.CustomObjectPopup(label, animatorStateSelected, displayOptions, animatorStateValues, null, null, null, null, position) as UnityEditor.Animations.AnimatorState;



                if (animatorStateSelected != null && animatorStateSelected.nameHash != prevAnimatorStateSelected.nameHash)
                {
                    ws.winx.unity.AnimatorState state = property.objectReferenceValue as ws.winx.unity.AnimatorState;

                    if (state == null)
                    {
                        state = ScriptableObject.CreateInstance <ws.winx.unity.AnimatorState>();
                    }

                    state.motion   = animatorStateSelected.motion;
                    state.nameHash = animatorStateSelected.nameHash;
                    state.name     = animatorStateSelected.name;

                    if (state.motion is UnityEditor.Animations.BlendTree)
                    {
                        BlendTree tree           = (BlendTree)state.motion;
                        int       blendParamsNum = tree.GetRecursiveBlendParamCount();

                        state.blendParamsHashes = new int[blendParamsNum];

                        for (int i = 0; i < blendParamsNum; i++)
                        {
                            state.blendParamsHashes[i] = Animator.StringToHash(tree.GetRecursiveBlendParam(i));
                        }
                    }
                    else
                    {
                        state.blendParamsHashes = null;
                    }



                    property.objectReferenceValue = state;
                    property.serializedObject.ApplyModifiedProperties();
                }
            }
        }
Example #2
0
        //
        // Methods
        //



        /// <summary>
        /// Handles the onGUI event.
        /// </summary>
        /// <param name="property">Property.</param>
        /// <param name="node">Node.</param>
        /// <param name="guiContent">GUI content.</param>
        public override void OnGUI(SerializedNodeProperty property, ActionNode node, GUIContent guiContent)
        {
            //if (animatorSerialized == null || aniController == null) {
            if (aniController == null)
            {
                //!!! Serialization never serialized Animator cos its initialized in Reset after
//								NodePropertyIterator iter= property.serializedNode.GetIterator();
//								iter.Find(attribute.animatorFieldName);
//								 animatorSerialized=iter.current;
                //				//								if(animatorSerialized==null || animatorSerialized.value==null){
                //										Debug.LogError("AnimatorStateNodePropertyDrawer> No Animator component set on node parent GameObject");
                //									return;
                //								}

                //runtimeContoller =( (Animator)animatorSerialized.value).runtimeAnimatorController;
                Animator animator = node.GetType().GetField(attribute.animatorFieldName).GetValue(node) as Animator;



                RuntimeAnimatorController runtimeContoller;


                runtimeContoller = animator.runtimeAnimatorController;

                if (runtimeContoller is AnimatorOverrideController)
                {
                    aniController = ((AnimatorOverrideController)runtimeContoller).runtimeAnimatorController as UnityEditor.Animations.AnimatorController;
                }
                else
                {
                    aniController = runtimeContoller as UnityEditor.Animations.AnimatorController;
                }
            }



            animatorStateDisplayOptions = MecanimUtility.GetDisplayOptions(aniController);
            animatorStateValues         = MecanimUtility.GetAnimatorStates(aniController);



            if (property.value != null)
            {
                if (animatorStateValues.Length > 0)
                {
                    animatorStateSelectedPrev = animatorStateSelected = animatorStateValues.FirstOrDefault((itm) => itm.nameHash == ((ws.winx.unity.AnimatorState)property.value).nameHash);
                }
            }


            animatorStateSelected = EditorGUILayoutEx.CustomObjectPopup(guiContent, animatorStateSelected, animatorStateDisplayOptions, animatorStateValues);                                     //,compare);



            //TODO try Begin/End Check
            if (animatorStateSelectedPrev != animatorStateSelected)
            {
                NodePropertyIterator iter = property.serializedNode.GetIterator();
                iter.Find(attribute.layerIndexFieldName);
                SerializedNodeProperty layerIndexSerialized = iter.current;

                layerIndexSerialized.value = MecanimUtility.GetLayerIndex(aniController, animatorStateSelected);
                layerIndexSerialized.ApplyModifiedValue();



                ws.winx.unity.AnimatorState state = property.value as ws.winx.unity.AnimatorState;
                if (state == null)
                {
                    state = ScriptableObject.CreateInstance <ws.winx.unity.AnimatorState>();
                }

                state.motion   = animatorStateSelected.motion;
                state.nameHash = animatorStateSelected.nameHash;
                state.layer    = (int)layerIndexSerialized.value;

                if (state.motion is UnityEditor.Animations.BlendTree)
                {
                    BlendTree tree           = (BlendTree)state.motion;
                    int       blendParamsNum = tree.GetRecursiveBlendParamCount();

                    state.blendParamsHashes = new int[blendParamsNum];

                    for (int i = 0; i < blendParamsNum; i++)
                    {
                        state.blendParamsHashes[i] = Animator.StringToHash(tree.GetRecursiveBlendParam(i));
                    }
                }
                else
                {
                    state.blendParamsHashes = null;
                }

                //property.value=state;
                property.ValueChanged();


                property.ApplyModifiedValue();

                animatorStateSelectedPrev = animatorStateSelected;
            }



            if (animatorStateSelected.motion == null)
            {
                Debug.LogError("Selected state doesn't have Motion set");
            }
        }