Search Unity

Request for AnimationClipStats and additional overload to AnimatorOverrideController.ApplyOverrides

Discussion in 'Animation' started by kognito1, Dec 16, 2019.

  1. kognito1

    kognito1

    Joined:
    Apr 7, 2015
    Posts:
    331
    Hello we have two small requests:

    1. Can Unity make AnimationClipStats (and the corresponding AnimationUtility.GetAnimationClipStats([Animation Clip]) static function) public? We utilize asset bundles heavily and (for hand waving webgl reasons) we try to cap the max size of any single asset bundle. We've been utilizing AnimationClipStats through reflection to achieve a max cap for our animation asset bundles but would like to do it a more "official way" :)

    Code (CSharp):
    1. private static int GetCompressedAnimationClipSize(AnimationClip clip)
    2. {
    3.     BindingFlags flags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Default | BindingFlags.DeclaredOnly | BindingFlags.Static | BindingFlags.GetField | BindingFlags.GetProperty | BindingFlags.SetProperty | BindingFlags.SetField;
    4.     System.Type aut = typeof(AnimationUtility);
    5.     MethodInfo mi = aut.GetMethod("GetAnimationClipStats", flags);
    6.     if (mi != null)
    7.     {
    8.         var generic = mi.Invoke(null, new object[] { clip });
    9.         if (generic != null)
    10.         {
    11.             FieldInfo fi = generic.GetType().GetField("size");
    12.             if (fi != null)
    13.             {
    14.                 object objSize = fi.GetValue(generic);
    15.                 if (objSize != null)
    16.                 {
    17.                     return System.Convert.ToInt32(objSize);
    18.                 }
    19.             }
    20.         }
    21.     }
    22.     return 0;
    23. }

    2. Can Unity add a IList<KeyValuePair<string, AnimationClip>> overload to AnimatorOverrideController.ApplyOverrides()? For more webgl hand waving reasons, we need to load assets progressively over a period of time (to reduce peak memory usage). Because of this approach, we can't load all the animation clips from an animation controller at once (i.e. animation controller bundles X/Y/Z are loaded, instantiated, then unloaded before the clip bundles A/B/C are loaded). We use AnimatorOverrideController as a workaround to assign clips from asset bundles to a controller that's already been instantiated. Since the animation controller has no references to its animation clips when its instantiated (the bundles aren't loaded), we have to assign the clips by name (string) using the index operator. This works but in the documentation it states: "Note: You should avoid calling this function more than once per frame per Animator as each call will trigger a reallocation of the animator's clip bindings. Instead use AnimatorOverrideController.ApplyOverrides." But since all we have is names/strings of the clips, we can't use ApplyOverrides in its given state. Not the end of the world since looping with the index operator does work for us, but we'd just like to be more optimal about it. :D

    I'm aware our use case is outside the norm so I understand if we can't be accommodated. :p Let me know if you need further context or explanation. Hopefully our approach makes sense!