diff --git a/Runtime/ArenaClientScene.cs b/Runtime/ArenaClientScene.cs index b831872..1d89b9e 100644 --- a/Runtime/ArenaClientScene.cs +++ b/Runtime/ArenaClientScene.cs @@ -678,7 +678,7 @@ private void CreateUpdateObject(string object_id, string storeType, bool persist JToken amObj = jData.SelectToken("animation-mixer"); if (amObj != null) { - ArenaUnity.ToUnityAnimationMixer(data, jData, ref gobj); + ArenaUnity.ToUnityAnimationMixer(jData, ref gobj); } if (aobj != null) diff --git a/Runtime/ArenaUnity.cs b/Runtime/ArenaUnity.cs index 7849d17..95f29db 100644 --- a/Runtime/ArenaUnity.cs +++ b/Runtime/ArenaUnity.cs @@ -818,10 +818,10 @@ internal static void ToArenaAnimationMixer(GameObject gobj, ref JObject jData) ArenaAnimationMixer am = gobj.GetComponent(); jData["animation-mixer"] = am.json.SaveToString(); } - internal static void ToUnityAnimationMixer(dynamic data, JObject jData, ref GameObject gobj) + internal static void ToUnityAnimationMixer(JObject jData, ref GameObject gobj) { JToken amObj = jData.SelectToken("animation-mixer"); - if (amObj != null && amObj.HasValues) + if (amObj != null && amObj.Type != JTokenType.Null) { ArenaAnimationMixer am = gobj.GetComponent(); if (am == null) diff --git a/Runtime/Components/ArenaAnimationMixer.cs b/Runtime/Components/ArenaAnimationMixer.cs index d68e28e..8c12923 100644 --- a/Runtime/Components/ArenaAnimationMixer.cs +++ b/Runtime/Components/ArenaAnimationMixer.cs @@ -13,6 +13,7 @@ namespace ArenaUnity.Components [ExecuteInEditMode] [DisallowMultipleComponent] [HelpURL("https://docs.arenaxr.org/content/schemas/message/animation-mixer")] + [RequireComponent(typeof(ArenaObject))] public class ArenaAnimationMixer : MonoBehaviour { // STATUS @@ -33,6 +34,8 @@ public class ArenaAnimationMixer : MonoBehaviour internal bool apply = false; + private string updatedJson = null; + protected virtual void Start() { apply = true; @@ -40,7 +43,7 @@ protected virtual void Start() protected void OnValidate() { - apply = true; + UpdateObject(); } protected void Update() @@ -58,21 +61,24 @@ internal void ApplyAnimations() Animation anim = GetComponentInChildren(true); if (anim == null) return; // set animation mixer properties - if (anim.isPlaying) anim.Stop(); - anim.cullingType = AnimationCullingType.BasedOnRenderers; - anim.playAutomatically = true; - switch (json.Loop.ToString()) - { - default: - case "repeat": anim.wrapMode = WrapMode.Loop; break; - case "once": anim.wrapMode = WrapMode.Once; break; - case "pingpong": anim.wrapMode = WrapMode.PingPong; break; - } - if (json.ClampWhenFinished) anim.wrapMode = WrapMode.ClampForever; + anim.Stop(); + anim.cullingType = AnimationCullingType.AlwaysAnimate; + anim.playAutomatically = false; var aobj = GetComponent(); if (aobj != null) animations = aobj.animations; + if (animations.Count > 0) anim.clip = anim[animations[0]].clip; + + if (json == null) return; + switch (json.Loop) + { + default: + case ArenaAnimationMixerJson.LoopType.Repeat: anim.wrapMode = WrapMode.Loop; break; + case ArenaAnimationMixerJson.LoopType.Once: anim.wrapMode = WrapMode.Once; break; + case ArenaAnimationMixerJson.LoopType.Pingpong: anim.wrapMode = WrapMode.PingPong; break; + } + if (json.ClampWhenFinished) anim.wrapMode = WrapMode.ClampForever; // play animations according to clip and wildcard string pattern = @$"{json.Clip.Replace("*", @"\w*")}"; // update wildcards for .Net @@ -108,12 +114,17 @@ internal void ApplyAnimations() internal void UpdateObject() { - var aobj = GetComponent(); - if (aobj != null) + var newJson = json.SaveToString(); + if (updatedJson != newJson) { - aobj.PublishUpdate($"{{\"{ArenaAnimationMixerJson.componentName}\":{json.SaveToString()}}}"); - apply = true; + var aobj = GetComponent(); + if (aobj != null) + { + aobj.PublishUpdate($"{{\"{ArenaAnimationMixerJson.componentName}\":{newJson}}}"); + apply = true; + } } + updatedJson = newJson; } } } diff --git a/Runtime/Components/ArenaAnimationMixerEditor.cs b/Runtime/Components/ArenaAnimationMixerEditor.cs index a452a6b..9f53739 100644 --- a/Runtime/Components/ArenaAnimationMixerEditor.cs +++ b/Runtime/Components/ArenaAnimationMixerEditor.cs @@ -22,14 +22,6 @@ public override void OnInspectorGUI() var aobj = am.GetComponent(); if (aobj != null) GUI.enabled = aobj.HasPermissions; - if (am.json != null) - { - if (GUILayout.Button($"Publish {ArenaAnimationMixerJson.componentName}")) - { - am.UpdateObject(); - } - } - GUI.enabled = true; DrawDefaultInspector(); @@ -42,6 +34,7 @@ public override void OnInspectorGUI() if (GUILayout.Toggle((am.json.Clip == "*"), "All")) { am.json.Clip = "*"; + if (am.json != null) am.UpdateObject(); } GUILayout.EndHorizontal(); for (int i = 0; i < aobj.animations.Count; i++) @@ -50,11 +43,13 @@ public override void OnInspectorGUI() if (GUILayout.Toggle((am.json.Clip == aobj.animations[i]), $"{i}: {aobj.animations[i]}")) { am.json.Clip = aobj.animations[i]; + if (am.json != null) am.UpdateObject(); } GUILayout.EndHorizontal(); } } + GUI.enabled = true; } } #endif diff --git a/Runtime/Schemas/ArenaAnimationMixerJson.cs b/Runtime/Schemas/ArenaAnimationMixerJson.cs index 27fe358..09486fe 100644 --- a/Runtime/Schemas/ArenaAnimationMixerJson.cs +++ b/Runtime/Schemas/ArenaAnimationMixerJson.cs @@ -40,8 +40,7 @@ public bool ShouldSerializeClampWhenFinished() public string Clip = defClip; public bool ShouldSerializeClip() { - if (_token != null && _token.SelectToken("clip") != null) return true; - return (Clip != defClip); + return true; // required in json schema } private static float defCrossFadeDuration = 0f;