Skip to content

Commit

Permalink
fix(animation-mixer): pub wire msg on edit, fix loop control, require…
Browse files Browse the repository at this point in the history
… clip
  • Loading branch information
mwfarb committed Feb 23, 2023
1 parent 12a2872 commit e1bf687
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 29 deletions.
2 changes: 1 addition & 1 deletion Runtime/ArenaClientScene.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions Runtime/ArenaUnity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -818,10 +818,10 @@ internal static void ToArenaAnimationMixer(GameObject gobj, ref JObject jData)
ArenaAnimationMixer am = gobj.GetComponent<ArenaAnimationMixer>();
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<ArenaAnimationMixer>();
if (am == null)
Expand Down
43 changes: 27 additions & 16 deletions Runtime/Components/ArenaAnimationMixer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -33,14 +34,16 @@ public class ArenaAnimationMixer : MonoBehaviour

internal bool apply = false;

private string updatedJson = null;

protected virtual void Start()
{
apply = true;
}

protected void OnValidate()
{
apply = true;
UpdateObject();
}

protected void Update()
Expand All @@ -58,21 +61,24 @@ internal void ApplyAnimations()
Animation anim = GetComponentInChildren<Animation>(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<ArenaObject>();
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
Expand Down Expand Up @@ -108,12 +114,17 @@ internal void ApplyAnimations()

internal void UpdateObject()
{
var aobj = GetComponent<ArenaObject>();
if (aobj != null)
var newJson = json.SaveToString();
if (updatedJson != newJson)
{
aobj.PublishUpdate($"{{\"{ArenaAnimationMixerJson.componentName}\":{json.SaveToString()}}}");
apply = true;
var aobj = GetComponent<ArenaObject>();
if (aobj != null)
{
aobj.PublishUpdate($"{{\"{ArenaAnimationMixerJson.componentName}\":{newJson}}}");
apply = true;
}
}
updatedJson = newJson;
}
}
}
11 changes: 3 additions & 8 deletions Runtime/Components/ArenaAnimationMixerEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,6 @@ public override void OnInspectorGUI()
var aobj = am.GetComponent<ArenaObject>();
if (aobj != null)
GUI.enabled = aobj.HasPermissions;
if (am.json != null)
{
if (GUILayout.Button($"Publish {ArenaAnimationMixerJson.componentName}"))
{
am.UpdateObject();
}
}
GUI.enabled = true;

DrawDefaultInspector();

Expand All @@ -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++)
Expand All @@ -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
Expand Down
3 changes: 1 addition & 2 deletions Runtime/Schemas/ArenaAnimationMixerJson.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit e1bf687

Please sign in to comment.