Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Cannot load animation clip from asset bundle!!! :( How to set animation clip to legacy in code?

Discussion in 'Scripting' started by hychum2, Jul 14, 2017.

  1. hychum2

    hychum2

    Joined:
    Nov 4, 2016
    Posts:
    4

    So, I packed a model, a animation clip of it and material into asset bundle with the following code:

    Code (CSharp):
    1. public static void buildAllAssetBundles()
    2.     {
    3.         System.Guid fileGUID = System.Guid.NewGuid();
    4.  
    5.         AssetBundleBuild[] buildInfo = new[]
    6.         {
    7.             new AssetBundleBuild {assetBundleName=fileGUID.ToString()+".unity3d", assetNames=new[]{"Assets/ARAsset"}}
    8.         };
    9.        
    10.         string projectPath = Directory.GetParent (Application.dataPath).FullName;
    11.  
    12.         string outputPathWeb = projectPath + "/assetbundle/";
    13.  
    14.         Directory.CreateDirectory (outputPathWeb);
    15.  
    16.         EditorUserBuildSettings.SwitchActiveBuildTarget (BuildTarget.WebGL);
    17.         BuildPipeline.BuildAssetBundles (outputPathWeb, buildInfo,BuildAssetBundleOptions.CompleteAssets |  BuildAssetBundleOptions.CollectDependencies, BuildTarget.WebGL);
    18.         System.IO.File.Move(outputPathWeb + "/" + fileGUID.ToString()+".unity3d", outputPathWeb + "/" + "asset.unity3d");
    19.         System.IO.File.Move(outputPathWeb + "/" + fileGUID.ToString()+".unity3d.manifest", outputPathWeb + "/" + "asset.unity3d.manifest");
    20.         Debug.Log ("AssetProcess: Assetbundle for Web created");
    21.  
    22.     }
    Then, I load the model and the animation with the following code:

    Code (CSharp):
    1.     IEnumerator download(string inUrl)
    2.     {
    3.         while (!Caching.ready)
    4.             yield return null;
    5.        
    6.         var www = new WWW(inUrl);
    7.         yield return www;
    8.         if (!string.IsNullOrEmpty(www.error))
    9.         {
    10.             Debug.Log(www.error);
    11.         }
    12.         var myLoadedAssetBundle = www.assetBundle;
    13.  
    14.         var prefab = myLoadedAssetBundle.LoadAsset<GameObject>("01_Idel");
    15.         currentObj = Instantiate (prefab);
    16.         currentObj.transform.position = new Vector3 (0, -0.5f, 0);
    17.  
    18.         AnimationClip animationClip = myLoadedAssetBundle.LoadAsset<AnimationClip> ("Take002);
    19.        animationClip.legacy = true;
    20.  
    21.        currentObj.GetComponent<Animation> ().AddClip(animationClip,animationClip.name);
    22.      
    23.        currentObj.GetComponent<Animation> ().Play ("Take002");
    24.  
    25.    }
    But the animation clip is empty!!!


    If I set the .anim file to legacy manually in debug mode first then export the asset bundle, the animation will works fine.


    The thing is, I have thousands of hundreds of those clip I need to pack, it's not possible to set it to legacy manually :( I wonder is there anyway to set it in code?
    I try to do it with AssetPostprocessor with no success:
    Code (CSharp):
    1. private void OnPreprocessAnimation()
    2.     {
    3.         Debug.Log ("AssetProcess: Importing animation");
    4.         Debug.Log ("dllm");
    5.  
    6.         var modelImporter = assetImporter as ModelImporter;
    7.         modelImporter.clipAnimations = modelImporter.defaultClipAnimations;
    8.         foreach(ModelImporterClipAnimation anim in modelImporter.clipAnimations){
    9.             anim.wrapMode = WrapMode.Loop;
    10.         }
    11.     }
    There is not any parameter for me to set the animation type.

    Please Help!!! I'm stuck for a week now!!!:(:(:(
     
  2. Arvin6

    Arvin6

    Joined:
    Jun 2, 2017
    Posts:
    27
    Did you manage to solve this problem? I'm stuck in this too.
     
  3. scottzhang

    scottzhang

    Joined:
    May 4, 2019
    Posts:
    1
    F***, I'm stuck in this too too.
     
  4. normus28

    normus28

    Joined:
    Feb 16, 2014
    Posts:
    5
    I have this problem too, is there any solution?
     
  5. ICTS

    ICTS

    Joined:
    Sep 26, 2018
    Posts:
    3
    For who came from google, fixed doing this:

    Code (CSharp):
    1.  
    2. string url = "http://downloadurl/animation.prefab";
    3.  
    4. using (UnityWebRequest www = UnityWebRequestAssetBundle.GetAssetBundle(url))
    5. {
    6.     yield return www.SendWebRequest();
    7.  
    8.     if (www.result == UnityWebRequest.Result.Success)
    9.     {
    10.         AssetBundle bundle = DownloadHandlerAssetBundle.GetContent(www);
    11.         AssetBundleRequest assetBR = bundle.LoadAssetAsync<AnimationClip>("/Assets/Animation.anim");
    12.         AnimationClip anim = assetBR.asset as AnimationClip;
    13.  
    14.         Debug.Log(anim);
    15.     }
    16.     else
    17.     {
    18.         Debug.Log("Download Failed");
    19.     }
    20. }
    21.