Hello Guest

Author Topic: Create Sprite Collections With Code  (Read 7202 times)

aninocoders

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 10
    • View Profile
Create Sprite Collections With Code
« on: April 24, 2013, 07:54:15 am »
Hello Unikron,

I'm building a Unity parser for another game engine. The game engine spouts XML files that we can read. Basically, the whole game can be constructed from these files.

I'm working on a Unity editor for this. The editor will ask for the folder of the XML files, parse these, copy image files, and create game objects on click of a button. Right now, I'm stuck on how to create Sprite Collections with code. Is this possible? I've rummaged through the 2D Toolkit source code. I can see how it goes from Open Editor... I just can't get the code flow when doing it without the editor.

aninocoders

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 10
    • View Profile
Re: Create Sprite Collections With Code
« Reply #1 on: April 24, 2013, 10:10:49 am »
I've done it. This is the minimal code. I had to make HandleDroppedPayload() public, though, which is a hack. Maybe you have a more elegant way of doing this.

    private void CreateSpriteCollection() {
      // create game object with sprite collection
      GameObject go = new GameObject("TestSpriteCollection");
      tk2dSpriteCollection collection = go.AddComponent<tk2dSpriteCollection>();
      collection.version = tk2dSpriteCollection.CURRENT_VERSION;
      tk2dEditorUtility.SetGameObjectActive(go, false);
      
      string path = "Assets/HomageData/TestSpriteCollection.prefab";
      Object p = PrefabUtility.CreateEmptyPrefab(path);
        GameObject collectionPrefabGo = PrefabUtility.ReplacePrefab(go, p, ReplacePrefabOptions.ConnectToPrefab);
      tk2dSpriteCollection collectionFromPrefab = collectionPrefabGo.GetComponent<tk2dSpriteCollection>();
      GameObject.DestroyImmediate(go);
      
      tk2dSpriteCollectionEditorPopup editor = EditorWindow.GetWindow(typeof(tk2dSpriteCollectionEditorPopup), false, "SpriteCollection" ) as tk2dSpriteCollectionEditorPopup;
      editor.SetGenerator(collectionFromPrefab);
      
      // load textures
      Texture2D texture1 = (Texture2D)AssetDatabase.LoadAssetAtPath("Assets/Game/Textures/leg_cut1.png", typeof(Texture2D));
      Texture2D texture2 = (Texture2D)AssetDatabase.LoadAssetAtPath("Assets/Game/Textures/leg_cut2.png", typeof(Texture2D));
      Object[] payload = new Object[]{ texture1, texture2 };
      
      // simulate drag and drop to editor
      editor.HandleDroppedPayload(payload);
      editor.Commit(); // create the sprite collection
      
      editor.Close();
   }

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Create Sprite Collections With Code
« Reply #2 on: April 24, 2013, 10:48:56 am »
There is indeed a much more elegant way of doing this.
You don't need to open the editor at all.

After creating the sprite collection object, all you need to do is fill the textureParams array with the data you need - i.e. create and fill tk2dSpriteCollectionDefinitions. You should only need to set 3 parameters in there:
Code: [Select]
params[i].name = texture.name;
params[i].colliderType = tk2dSpriteCollectionDefinition.ColliderType.UserDefined;
params[i].texture = texture;
All the rest can be defaults, or you can configure as required. Setting colliderType is required for backwards compatibility.

After you've configured it as you need it, call
Code: [Select]
tk2dSpriteCollectionBuilder.Rebuild( collection );

aninocoders

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 10
    • View Profile
Re: Create Sprite Collections With Code
« Reply #3 on: April 24, 2013, 11:09:43 am »
Thanks. Will try that.