Exemplo n.º 1
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");
            }
        }
        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();
                }
            }
        }
    public IEnumerator InteractWithObject(GameObject sender, PositionOffset position, string action, bool forceTransform, string animationState, System.Action <bool> callback)
    {
        // var position = FindPosition();
        if (position == null)
        {
            position = FindPosition();
        }

        if (this.actions.Length == 0)
        {
            throw new System.Exception("Interactive object provides no actions");
        }

        if (action == null)
        {
            BT = actions[0].BT;
        }
        else
        {
            BT = actions.First(a => a.action == action).BT;
        }
        if (BT == null)
        {
            Debug.LogError(name + " does not have a behaviour tree assigned");
            yield break;
        }

        // get agent reference for log purposes
        this.agent = sender.GetComponent <Agent>();

        // we can force that user will appear at the defined location
        if (forceTransform)
        {
            // wait till avatar is at full stop
            if (!string.IsNullOrEmpty(animationState))
            {
                yield return(StartCoroutine(MecanimUtility.WaitForState(sender.GetComponent <Animator>(), animationState)));
            }


            // itween to the new position
            sender.transform.DOMove(position.Position, 0.5f).OnComplete(() => {
                sender.transform.DOLookAt(position.LookAt, 0.5f, AxisConstraint.Y);
            });

            this.Log("Position Forced");
            yield return(new WaitForSeconds(0.5f));
        }


        // take blackboard from component
        var blackboard = sender.GetComponent <Blackboard>();

        if (blackboard == null)
        {
            blackboard = sender.AddComponent <Blackboard>();
        }


        // copy all values from object blackboard
        blackboard.SetValue("CurrentAction", action);
        blackboard.SetValue("InteractiveObject", gameObject);
        blackboard.SetValue("OffsetPosition", position.Position);
        blackboard.SetValue("LookAtPosition", position.LookAt);
        //blackboard.SetValue ("Actor", sender);

        BT.repeat = false;
        var i = (BehaviourTree)Instantiate(BT);

        // get the behaviour tree owner
        var owner = GetComponent <BehaviourTreeOwner>();

        if (owner != null)
        {
            owner.graph = i;
        }

        // TODO: Investigate the parameter
        i.StartGraph(sender.transform, blackboard, true, (result) => GraphStoppedCallback(callback, result, position.Index));

        this.Log("Tree started.");

        yield return(null);
    }