Hello Guest

Author Topic: Create Sprite Animation With Code  (Read 5777 times)

zhaoqingqing

  • Newbie
  • *
  • Posts: 2
    • View Profile
Create Sprite Animation With Code
« on: April 29, 2014, 09:52:59 am »
Hello Unikron,Our team is doing packed images and Use scripts to automate generate sprite Animation script  but not success
Here is my script
Code: [Select]
[MenuItem("Game/AutoCreate Sprite Animation")]
public static void CreateSpriteAnimaTest()
{
//sprite animation
string animaSpPath = "Assets/Actor/ActorAnimation.prefab";
tk2dSpriteAnimation newSpriteAnimation = null;//
if (animaSpPath.Length != 0)
{
GameObject go = new GameObject();
newSpriteAnimation = go.AddComponent<tk2dSpriteAnimation>();
tk2dEditorUtility.SetGameObjectActive(go, false);
UnityEngine.Object p = PrefabUtility.CreateEmptyPrefab(animaSpPath);
PrefabUtility.ReplacePrefab(go, p, ReplacePrefabOptions.ConnectToPrefab);
//GameObject.DestroyImmediate(go);
tk2dEditorUtility.GetOrCreateIndex().AddSpriteAnimation(AssetDatabase.LoadAssetAtPath(animaSpPath, typeof(tk2dSpriteAnimation)) as tk2dSpriteAnimation);

tk2dEditorUtility.CommitIndex();

//AnimationEditorPopup
tk2dSpriteAnimationEditorPopup editor = EditorWindow.GetWindow(typeof(tk2dSpriteAnimationEditorPopup), false, "Sprite Animation") as tk2dSpriteAnimationEditorPopup;
editor.CreateNewClip();

CBase.Log("new clip");
editor.SetSpriteAnimation(newSpriteAnimation);

//AutoFillFrames.

editor.Commit();
editor.Close();
}
}
However, the script did not like my expectations, it also creates SpriteAnimation, but SpriteAnimation is empty, Does not contain any  NewClip,
ask for help

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Create Sprite Animation With Code
« Reply #1 on: April 29, 2014, 11:37:20 am »
Hi you shouldn't use the editor to create animations - once you create the newSpriteAnimation object, you can simply fill in the contents directly. I've attached an example to this thread - you will need to register your copy of 2D Toolkit to download attachments.

zhaoqingqing

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: Create Sprite Animation With Code
« Reply #2 on: April 30, 2014, 02:35:52 am »
OK, I understand ,thanks you !