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. Dismiss Notice

How to get a copy (.anim file) of a splitted clip?It is inside .fbx...(......with editor class)

Discussion in 'Scripting' started by JohnSonLi, Feb 7, 2015.

  1. JohnSonLi

    JohnSonLi

    Joined:
    Apr 15, 2012
    Posts:
    586
    whenever I wanna copy an asset ,I need the path.
    here the problem is : the animation clip I wanna copy does not have an asset path,since it is inside of fbx....
    how???????
     
  2. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
  3. JohnSonLi

    JohnSonLi

    Joined:
    Apr 15, 2012
    Posts:
    586
    I tried http://answers.unity3d.com/questions/227141/automatically-duplicate-animation-from-imported-fi.html ,
    but it's not working, ...

    replied by other one as following:
    string path = AssetDatabase.GetAssetPath(sourceClip); but when Debug.Log(path), I found path is empty,you should think of other ways
     
  4. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    Hmmmm
    I haven't actually tried this, but i can see a pitfall where the model might need to be imported as legacy, otherwise the resulting gameobject won't actually have the Animation component, rather an Animator, so the getcomponent doesn't work
    You could probably instead just check all assets in the object using AssetDatabase, to match typeof(animationClip)
     
  5. JohnSonLi

    JohnSonLi

    Joined:
    Apr 15, 2012
    Posts:
    586
    I find a way.....
    you can retrieve every curve data from the clip inside of .fbx file. then instantiate a newclip which you copy every curve into. and create this newclip as an asset. I tried, and it works.
    Code (csharp):
    1.  
    2.     private static void createAnimAsset(AnimationClip clip, string path)
    3.     {
    4.         AnimationClipCurveData[] curves = AnimationUtility.GetAllCurves(clip, true);
    5.         AnimationClip newClip = new AnimationClip();
    6.         for (int i = 0; i < curves.Length; ++i)
    7.         {
    8.             newClip.SetCurve(curves[i].path, curves[i].type, curves[i].propertyName, curves[i].curve);
    9.         }
    10.         AssetDatabase.CreateAsset(newClip, path);
    11.     }
    12.