//============= Copyright (c) Ludic GmbH, All rights reserved. ============== // // Purpose: Part of the My Behaviour Tree Controller Code // //============================================================================= using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using MyBT; using UnityEngine.Video; #if UNITY_EDITOR using UnityEditor; [CustomEditor(typeof(NamedAnimator))] public class NamedAnimatorInspector : ComponentHandlerInspector { } #endif [System.Serializable] public class NamedAnimator : ComponentHandler { public override string TypeLabel () { return "Animator"; } public override string ContentLabel() { UpdateComponent(); if (animatorComponent == null) { return "animator is null"; } if (animatorComponent.runtimeAnimatorController == null) { return "runtimeAnimatorController is null"; } ReadTriggerNamed(); // OutputDebugData(); return animatorComponent.runtimeAnimatorController.name; } // for testing public void OutputDebugData () { Debug.Log($"-----------{Task.getState}-------------"); // layer infos for (int layerIndex = 0; layerIndex < animatorComponent.layerCount; layerIndex++) { // Debug.Log($"{animator.runtimeAnimatorController}"); Debug.Log($"{animatorComponent.HasState(0, 0)}"); // int layerIndex = lId; AnimatorStateInfo currentAnimatorStateInfo = animatorComponent.GetCurrentAnimatorStateInfo(0); AnimatorStateInfo nextAnimatorStateInfo = animatorComponent.GetNextAnimatorStateInfo(layerIndex); Debug.Log($"GetCurrentAnimatorStateInfo {currentAnimatorStateInfo} {currentAnimatorStateInfo.length} {currentAnimatorStateInfo.normalizedTime}"); Debug.Log($"GetNextAnimatorStateInfo {nextAnimatorStateInfo} {nextAnimatorStateInfo.length} {nextAnimatorStateInfo.normalizedTime}"); AnimatorClipInfo[] currentAnimatorClipInfos = animatorComponent.GetCurrentAnimatorClipInfo(layerIndex); AnimatorClipInfo[] nextAnimatorClipInfos = animatorComponent.GetNextAnimatorClipInfo(layerIndex); Debug.Log($"currentAnimatorClipInfos.Length {currentAnimatorClipInfos.Length}"); Debug.Log($"nextAnimatorClipInfos.Length {nextAnimatorClipInfos.Length}"); Debug.Log($"IsInTransition: {animatorComponent.IsInTransition(layerIndex)}"); AnimatorTransitionInfo animatorTransitionInfo = animatorComponent.GetAnimatorTransitionInfo(layerIndex); Debug.Log($"GetAnimatorTransitionInfo: duration {animatorTransitionInfo.duration}"); Debug.Log($"layername: {animatorComponent.GetLayerName(layerIndex)}"); int animatorClipInfoCount = animatorComponent.GetCurrentAnimatorClipInfoCount(layerIndex); Debug.Log($"animatorClipInfoCount: {animatorClipInfoCount}"); for (int i = 0; i < animatorClipInfoCount; i++) { AnimatorClipInfo[] animatorClipInfos = animatorComponent.GetCurrentAnimatorClipInfo(i); Debug.Log($"animatorClipInfos.Length: {animatorClipInfos.Length}"); for (int l = 0; l < animatorClipInfos.Length; l++) { Debug.Log($"{animatorClipInfos[l].clip.length} {animatorClipInfos[l].clip.name}"); } } for (int i = 0; i < currentAnimatorClipInfos.Length; i++) { Debug.Log($"currentAnimatorClipInfos {i} {currentAnimatorClipInfos[i]}"); Debug.LogWarning($"--- {i} {currentAnimatorClipInfos[i].weight} {currentAnimatorClipInfos[i].clip}"); } for (int i = 0; i < nextAnimatorClipInfos.Length; i++) { Debug.LogWarning($"!!! nextAnimatorClipInfos {i} {nextAnimatorClipInfos[i]} {nextAnimatorClipInfos[i].clip}"); } } } public void ReadTriggerNamed () { triggerNames = new List(); for (int i = 0; i < animatorComponent.parameterCount; i++) { AnimatorControllerParameter animatorControllerParameter = animatorComponent.GetParameter(i); //animatorComponent.GetParameter() // Debug.Log($"animatorControllerParameter.name: {i} {animatorControllerParameter.name}"); triggerNames.Add(animatorControllerParameter.name); } if (!gameObject.activeSelf) { Debug.LogError($"NamedAnimator.ReadTriggerNamed: gameObject '{gameObject.name}' must be active to read trigger names"); } //Debug.Log($"NamedAnimator.ReadTriggerNamed: read {animatorComponent.parameterCount} parameters"); } public override void UpdateComponent() { base.UpdateComponent(); animatorComponent = GetComponent(); } public Animator animatorComponent; public List triggerNames; public int checkLayer = 0; public override void Start() { } public override string titleText { get { return "Set: \"Trigger\" or \"ResetTrigger\" or an Animation"; } } public override string[][] helpText { get { return new string[][] { new string[] {"Set Animation Trigger", $"BTC.Set(\"{roomId}\", \"{gameObject.name}\", \"Trigger\", \"TriggerName\")"}, new string[] {"Reset Animation Trigger", $"BTC.Set(\"{roomId}\", \"{gameObject.name}\", \"ResetTrigger\", \"TriggerName\")"} }; } } public override void Run(MyBT.NodeState nodeState) { // whan aborting if (nodeState == NodeState.Aborting) { // animator.(); return; } // at start if (nodeState == NodeState.FirstRun) { // reset event trigger // animator.Play(); } // during runtime if (nodeState == NodeState.Running) { // if (!animator.isPlaying) { // Task.SetSucceeded(); // return; // } } } // public void TriggerAnimation(string roomId, string animatorName, string triggerName, string animationName) { // // NamedAnimator na = GetNamedObject(roomId, animatorName) as NamedAnimator; // foreach (NamedAnimator na in GetHandlers(roomId, animatorName)) { // if (na != null) { // // on start, play // if (Task.isStarting) { // Debug.Log($"TriggerAnimation.isStarting : animator {animatorName} trigger {triggerName} {roomId.ToString()} on {na.gameObject.name} {animationName}"); // // reset all previous triggers // foreach (AnimatorControllerParameter p in na.animator.parameters) // if (p.type == AnimatorControllerParameterType.Trigger) // na.animator.ResetTrigger(p.name); // na.animator.SetTrigger(triggerName); // } // // cannot finish on starting // else { // Task.log = $"{na.name} {na.animator.GetCurrentAnimatorStateInfo(0).normalizedTime.ToString()} {na.animator.GetCurrentAnimatorStateInfo(0).IsName(animationName)}"; // //Debug.Log("TriggerAnimation.isStarting : " + roomId.ToString() + " on " + na.gameObject.name + " starting animatorName=" + animatorName + " " + triggerName + " " + animationName + " " + (!na.animator.GetCurrentAnimatorStateInfo(0).IsName(animationName)) + " " + (!na.animator.IsInTransition(0))); // if (animationName != "") { // if (!na.animator.GetCurrentAnimatorStateInfo(0).IsName(animationName) && !na.animator.IsInTransition(0)) { // Task.SetSucceeded(); // } // } // else { // Task.SetSucceeded(); // } // } // } // } // } public bool waitingForTransition = true; public override void Set(NodeState nodeState, string key, string value) { if (nodeState == NodeState.FirstRun) { if (key == "Trigger") { // disable all other triggers foreach (string setOff in triggerNames) { if (setOff != value) { // Debug.Log($"Reset Trigger {setOff}"); animatorComponent.ResetTrigger(setOff); } // if (triggerNamesCopy.Contains(value)) { // triggerNamesCopy.Remove(value); // } } // enable the one animatorComponent.SetTrigger(value); waitingForTransition = true; // animator.Update(); // Debug.Log($"NamedAnimator.Set: FirstRun Trigger {value}"); // OutputDebugData(); // Task.SetSucceeded(); return; } if (key == "ResetTrigger") { animatorComponent.ResetTrigger(value); // Debug.Log($"NamedAnimator.Set: FirstRun ResetTrigger {value}"); // OutputDebugData(); // Task.SetSucceeded(); return; } return; } if (nodeState == NodeState.Running) { if (animatorComponent.IsInTransition(checkLayer)) { waitingForTransition = false; } else { // not yet started, wait for isInTransition if (waitingForTransition) { } // the transition is finished else { Task.SetSucceeded(); } } // Debug.Log($"NamedAnimator.Set: Running Trigger {value}"); // bool anyTrue = false; // foreach (string trigger in triggerNames) { // anyTrue |= animator.GetTrigger(trigger); // } // Debug.Log($"AnyTrue {anyTrue}"); // OutputDebugData(); // Task.SetSucceeded(); return; } if (nodeState == NodeState.Aborting) { waitingForTransition = false; // disable all triggers foreach (string setOff in triggerNames) { // Debug.Log($"Reset Trigger {setOff}"); animatorComponent.ResetTrigger(setOff); } return; } Task.SetFailed(); } public override void Update() { base.Update(); } #region both values setting // public override void SetAlpha(float alpha) { // if (text != null) { // Color col = text.color; // col.a = 1 - alpha; // text.color = col; // } // } // public override float GetAlpha() { // if (text != null) { // return 1-text.color.a; // } // return 0; // } #endregion }