|
| 1 | +using System; |
| 2 | +using Newtonsoft.Json.Linq; |
| 3 | +using MCPForUnity.Editor.Helpers; |
| 4 | +using UnityEditor; |
| 5 | +using UnityEditor.Animations; |
| 6 | +using UnityEngine; |
| 7 | + |
| 8 | +namespace MCPForUnity.Editor.Tools.Animation |
| 9 | +{ |
| 10 | + internal static class AnimatorControl |
| 11 | + { |
| 12 | + public static object Play(JObject @params) |
| 13 | + { |
| 14 | + var go = ObjectResolver.ResolveGameObject(@params["target"], @params["searchMethod"]?.ToString()); |
| 15 | + if (go == null) |
| 16 | + return new { success = false, message = "Target GameObject not found" }; |
| 17 | + |
| 18 | + var animator = go.GetComponent<Animator>(); |
| 19 | + if (animator == null) |
| 20 | + return new { success = false, message = $"No Animator component on '{go.name}'" }; |
| 21 | + |
| 22 | + string stateName = @params["stateName"]?.ToString(); |
| 23 | + if (string.IsNullOrEmpty(stateName)) |
| 24 | + return new { success = false, message = "'stateName' is required" }; |
| 25 | + |
| 26 | + int layer = @params["layer"]?.ToObject<int>() ?? -1; |
| 27 | + |
| 28 | + Undo.RecordObject(animator, "Play Animation State"); |
| 29 | + animator.Play(stateName, layer); |
| 30 | + |
| 31 | + return new { success = true, message = $"Playing state '{stateName}' on '{go.name}'" }; |
| 32 | + } |
| 33 | + |
| 34 | + public static object Crossfade(JObject @params) |
| 35 | + { |
| 36 | + var go = ObjectResolver.ResolveGameObject(@params["target"], @params["searchMethod"]?.ToString()); |
| 37 | + if (go == null) |
| 38 | + return new { success = false, message = "Target GameObject not found" }; |
| 39 | + |
| 40 | + var animator = go.GetComponent<Animator>(); |
| 41 | + if (animator == null) |
| 42 | + return new { success = false, message = $"No Animator component on '{go.name}'" }; |
| 43 | + |
| 44 | + string stateName = @params["stateName"]?.ToString(); |
| 45 | + if (string.IsNullOrEmpty(stateName)) |
| 46 | + return new { success = false, message = "'stateName' is required" }; |
| 47 | + |
| 48 | + float duration = @params["duration"]?.ToObject<float>() ?? 0.25f; |
| 49 | + int layer = @params["layer"]?.ToObject<int>() ?? -1; |
| 50 | + |
| 51 | + Undo.RecordObject(animator, "Crossfade Animation State"); |
| 52 | + animator.CrossFade(stateName, duration, layer); |
| 53 | + |
| 54 | + return new { success = true, message = $"Crossfading to '{stateName}' over {duration}s on '{go.name}'" }; |
| 55 | + } |
| 56 | + |
| 57 | + public static object SetParameter(JObject @params) |
| 58 | + { |
| 59 | + var go = ObjectResolver.ResolveGameObject(@params["target"], @params["searchMethod"]?.ToString()); |
| 60 | + if (go == null) |
| 61 | + return new { success = false, message = "Target GameObject not found" }; |
| 62 | + |
| 63 | + var animator = go.GetComponent<Animator>(); |
| 64 | + if (animator == null) |
| 65 | + return new { success = false, message = $"No Animator component on '{go.name}'" }; |
| 66 | + |
| 67 | + string paramName = @params["parameterName"]?.ToString(); |
| 68 | + if (string.IsNullOrEmpty(paramName)) |
| 69 | + return new { success = false, message = "'parameterName' is required" }; |
| 70 | + |
| 71 | + string paramType = @params["parameterType"]?.ToString()?.ToLowerInvariant(); |
| 72 | + |
| 73 | + // Auto-detect type if not specified |
| 74 | + if (string.IsNullOrEmpty(paramType)) |
| 75 | + { |
| 76 | + for (int i = 0; i < animator.parameterCount; i++) |
| 77 | + { |
| 78 | + var p = animator.GetParameter(i); |
| 79 | + if (p.name == paramName) |
| 80 | + { |
| 81 | + paramType = p.type.ToString().ToLowerInvariant(); |
| 82 | + break; |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + if (string.IsNullOrEmpty(paramType)) |
| 87 | + return new { success = false, message = $"Parameter '{paramName}' not found. Specify 'parameterType' explicitly or check the parameter name." }; |
| 88 | + } |
| 89 | + |
| 90 | + JToken valueToken = @params["value"]; |
| 91 | + |
| 92 | + // In Edit mode, runtime Animator.SetFloat/SetInteger/SetBool are no-ops because |
| 93 | + // the Animator graph isn't active. Instead, modify the controller asset's default |
| 94 | + // parameter values so changes actually persist. |
| 95 | + bool isPlaying = Application.isPlaying; |
| 96 | + |
| 97 | + if (isPlaying) |
| 98 | + { |
| 99 | + Undo.RecordObject(animator, $"Set Animator Parameter {paramName}"); |
| 100 | + |
| 101 | + switch (paramType) |
| 102 | + { |
| 103 | + case "float": |
| 104 | + float fVal = valueToken?.ToObject<float>() ?? 0f; |
| 105 | + animator.SetFloat(paramName, fVal); |
| 106 | + return new { success = true, message = $"Set float '{paramName}' = {fVal}" }; |
| 107 | + |
| 108 | + case "int": |
| 109 | + case "integer": |
| 110 | + int iVal = valueToken?.ToObject<int>() ?? 0; |
| 111 | + animator.SetInteger(paramName, iVal); |
| 112 | + return new { success = true, message = $"Set int '{paramName}' = {iVal}" }; |
| 113 | + |
| 114 | + case "bool": |
| 115 | + case "boolean": |
| 116 | + bool bVal = valueToken?.ToObject<bool>() ?? false; |
| 117 | + animator.SetBool(paramName, bVal); |
| 118 | + return new { success = true, message = $"Set bool '{paramName}' = {bVal}" }; |
| 119 | + |
| 120 | + case "trigger": |
| 121 | + animator.SetTrigger(paramName); |
| 122 | + return new { success = true, message = $"Set trigger '{paramName}'" }; |
| 123 | + |
| 124 | + default: |
| 125 | + return new { success = false, message = $"Unknown parameter type: {paramType}. Valid: float, int, bool, trigger" }; |
| 126 | + } |
| 127 | + } |
| 128 | + else |
| 129 | + { |
| 130 | + // Edit mode: modify the AnimatorController asset's default parameter values |
| 131 | + var controller = animator.runtimeAnimatorController as AnimatorController; |
| 132 | + if (controller == null) |
| 133 | + return new { success = false, message = $"No AnimatorController assigned to Animator on '{go.name}'. Cannot set parameter defaults in Edit mode." }; |
| 134 | + |
| 135 | + var allParams = controller.parameters; |
| 136 | + int paramIndex = -1; |
| 137 | + for (int i = 0; i < allParams.Length; i++) |
| 138 | + { |
| 139 | + if (allParams[i].name == paramName) |
| 140 | + { |
| 141 | + paramIndex = i; |
| 142 | + break; |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + if (paramIndex < 0) |
| 147 | + return new { success = false, message = $"Parameter '{paramName}' not found on controller '{controller.name}'." }; |
| 148 | + |
| 149 | + Undo.RecordObject(controller, $"Set Parameter Default {paramName}"); |
| 150 | + |
| 151 | + switch (paramType) |
| 152 | + { |
| 153 | + case "float": |
| 154 | + float fVal = valueToken?.ToObject<float>() ?? 0f; |
| 155 | + allParams[paramIndex].defaultFloat = fVal; |
| 156 | + controller.parameters = allParams; |
| 157 | + EditorUtility.SetDirty(controller); |
| 158 | + AssetDatabase.SaveAssets(); |
| 159 | + return new { success = true, message = $"Set float '{paramName}' = {fVal} (default value, Edit mode)" }; |
| 160 | + |
| 161 | + case "int": |
| 162 | + case "integer": |
| 163 | + int iVal = valueToken?.ToObject<int>() ?? 0; |
| 164 | + allParams[paramIndex].defaultInt = iVal; |
| 165 | + controller.parameters = allParams; |
| 166 | + EditorUtility.SetDirty(controller); |
| 167 | + AssetDatabase.SaveAssets(); |
| 168 | + return new { success = true, message = $"Set int '{paramName}' = {iVal} (default value, Edit mode)" }; |
| 169 | + |
| 170 | + case "bool": |
| 171 | + case "boolean": |
| 172 | + bool bVal = valueToken?.ToObject<bool>() ?? false; |
| 173 | + allParams[paramIndex].defaultBool = bVal; |
| 174 | + controller.parameters = allParams; |
| 175 | + EditorUtility.SetDirty(controller); |
| 176 | + AssetDatabase.SaveAssets(); |
| 177 | + return new { success = true, message = $"Set bool '{paramName}' = {bVal} (default value, Edit mode)" }; |
| 178 | + |
| 179 | + case "trigger": |
| 180 | + return new { success = true, message = $"Trigger '{paramName}' noted (triggers are runtime-only, no default to set)" }; |
| 181 | + |
| 182 | + default: |
| 183 | + return new { success = false, message = $"Unknown parameter type: {paramType}. Valid: float, int, bool, trigger" }; |
| 184 | + } |
| 185 | + } |
| 186 | + } |
| 187 | + |
| 188 | + public static object SetSpeed(JObject @params) |
| 189 | + { |
| 190 | + var go = ObjectResolver.ResolveGameObject(@params["target"], @params["searchMethod"]?.ToString()); |
| 191 | + if (go == null) |
| 192 | + return new { success = false, message = "Target GameObject not found" }; |
| 193 | + |
| 194 | + var animator = go.GetComponent<Animator>(); |
| 195 | + if (animator == null) |
| 196 | + return new { success = false, message = $"No Animator component on '{go.name}'" }; |
| 197 | + |
| 198 | + float speed = @params["speed"]?.ToObject<float>() ?? 1f; |
| 199 | + |
| 200 | + Undo.RecordObject(animator, "Set Animator Speed"); |
| 201 | + animator.speed = speed; |
| 202 | + |
| 203 | + return new { success = true, message = $"Set animator speed to {speed} on '{go.name}'" }; |
| 204 | + } |
| 205 | + |
| 206 | + public static object SetEnabled(JObject @params) |
| 207 | + { |
| 208 | + var go = ObjectResolver.ResolveGameObject(@params["target"], @params["searchMethod"]?.ToString()); |
| 209 | + if (go == null) |
| 210 | + return new { success = false, message = "Target GameObject not found" }; |
| 211 | + |
| 212 | + var animator = go.GetComponent<Animator>(); |
| 213 | + if (animator == null) |
| 214 | + return new { success = false, message = $"No Animator component on '{go.name}'" }; |
| 215 | + |
| 216 | + bool enabled = @params["enabled"]?.ToObject<bool>() ?? true; |
| 217 | + |
| 218 | + Undo.RecordObject(animator, "Set Animator Enabled"); |
| 219 | + animator.enabled = enabled; |
| 220 | + |
| 221 | + return new { success = true, message = $"Animator {(enabled ? "enabled" : "disabled")} on '{go.name}'" }; |
| 222 | + } |
| 223 | + } |
| 224 | +} |
0 commit comments