Search Unity

Load AnimatorController from AssetBundle as RuntimeAnimatorController.

Discussion in 'Scripting' started by McGravity, Jan 2, 2017.

  1. McGravity

    McGravity

    Joined:
    Nov 2, 2013
    Posts:
    43
    Hi,

    I'm using Unity 5.5.0p3 on Windows 7 and this is how I currently try to dynamically load AnimatorControllers from an AssetBundle:

    Code (CSharp):
    1. RuntimeAnimatorController tmpController = assetBundle.LoadAsset<RuntimeAnimatorController>("MyAnimController") as RuntimeAnimatorController;
    2. RuntimeAnimatorController controller = Instantiate(tmpController);
    When I do so, I get the following warning when using it as the RuntimeAnimatorController for an Animator component:

    The Animator is initialized, valid and I apply the controller like this:

    Code (CSharp):
    1. myAnimator.runtimeAnimatorController = controller;
    From debugging I can see that the AnimatorController, after loading it from the AssetBundle, has no layers or parameters whatsoever. Which is obviously not the case when viewed from the editor. It has a "Base Layer" and two parameters.

    Weird enough it does work when I don't Instantiate the AnimatorController, but use it directly from the AssetBundle:

    Code (CSharp):
    1. myAnimator.runtimeAnimatorController = assetBundle.LoadAsset<RuntimeAnimatorController>("MyAnimController") as RuntimeAnimatorController;
    That on the other hand leads to a silent crash of Unity when calling the GetBehaviours<T>() method.

    This is how I create the AssetBundle:

    Code (CSharp):
    1. BuildPipeline.BuildAssetBundles("Assets/AssetBundles", BuildAssetBundleOptions.None, BuildTarget.StandaloneWindows64);
     
    moheji likes this.
  2. fernforce

    fernforce

    Joined:
    Jan 5, 2018
    Posts:
    4
    assigning straight from the assetBundle without Instantiating helped me. You still do it like this?
     
  3. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,654
    You do not need to instantiate controllers. They're asset meant to be assigned to instantiated game objects with animators of them. Animator is the object wich holds instance data and controller holds common data. You probably can clone the controller but I do not see any reason ever you need a copy of it.