Avoid several prefabs' animations from looping at the same time when assigned to asset bundle
Unity version: 2017.3.0f3
Scenario: I have 4 fbxs imported in my proyect. With an editor script i create a new prefab from each of those. Then i dynamically create an AnimatorController for each new prefab, with the one clip that they all have. I then create a transition to itself to force it to loop since i cannot "check" the "Loop Time" checkbox from code nor the "Clamp Range" option. I also try to force the animation to play till the end by setting the transition to "HasExitTime = true" and "ExitTime = 1" After that i set the AssetBundle name and i create the bundle.
Issue: There are 4 prefabs in the bundle, two of them have an "animationClip.length" of 2 and the rest last for like 1.33. Still all of them "end" and start looping at time 1.33, the shortest among them.
Question: 1 - Why? 2 - How can i force their animations to loop at their respective end times correctly even after being bundled together?
This is my code after decomposing each prefab into an Object array, in order to be able to iterate through its components and get the animation clip, etc.
foreach (Object[] item in lista) {
AnimationClip clip = new AnimationClip ();
GameObject mainObj = prefabs [lista.IndexOf (item)];
Animator animator = null;
//Obtain the animation clip
foreach (Object obj in item) {
if (obj is AnimationClip) {
obj.name = mainObj.name + "clip";
clip = obj as AnimationClip;
Debug.Log ("Animation clip length > " + clip.length);
}
}
//Get the animator of the prefab to work with it
UnityEditor.Animations.AnimatorController animatorController;
animator = mainObj.GetComponent<Animator> ();
//create an AnimatorController to later assign it inside the animator, based on the obtained clip
string controllerPath = "Assets/Resources/CodeAnimControllers/" + mainObj.name + "controller.controller";
animatorController = AnimatorController.CreateAnimatorControllerAtPathWithClip (controllerPath, clip);
//Create a "false" parameter to the AnimatorController
animatorController.AddParameter ("Reset", AnimatorControllerParameterType.Bool);
//Get the only state machine, with its lone state and create a transition to itself
var stateMachineA = animatorController.layers [0].stateMachine;
var stateA = stateMachineA.states [0].state;
var resetTransitionA = stateA.AddTransition (stateA);
//Create the condition of reproduction, setting it to > play if Reset is false
//Give an exit time to avoid endless restarting, the time is 1 so it completes its animation before restarting
//Give itself no transition time for nice looping
resetTransitionA.AddCondition (UnityEditor.Animations.AnimatorConditionMode.IfNot, 0, "Reset");
resetTransitionA.canTransitionToSelf = true;
resetTransitionA.destinationState = stateA;
resetTransitionA.hasExitTime = true;
resetTransitionA.exitTime = 1;
resetTransitionA.duration = 0;
//Asignar el controlador creado al objeto prefab
animator.runtimeAnimatorController = animatorController as RuntimeAnimatorController;
//Asignar un AssetBundle al prefab y su animatorController / Assign the AssetBundle name to the prefab and its animat orController
AssetImporter AI;
string assetPrefabPath = AssetDatabase.GetAssetPath(mainObj);
AI = AssetImporter.GetAtPath (assetPrefabPath);
AI.assetBundleName = "otro";
string assetPath = AssetDatabase.GetAssetPath(animatorController);
AI = AssetImporter.GetAtPath (assetPath);
AI.assetBundleName = "otro";
}
BuildPipeline.BuildAssetBundles (parentDirectory + "/" + newSubFolder, BuildAssetBundleOptions.None, BuildTarget.Android);