Hello 2D Toolkit,
I want to create a copy from a sprite sheet including all sprite definitions.
I had this before in another game, but these were copied outside Unity, then I just changed the Spritesheet texture.
But now I want it in the same collection as the first one? How can I best do this? I am trying to make an Editor script. But I have no clue where to start.
I found a CopyFrom method in the script. But I don't know how to use it, because if I add an extra sprite sheet and copy this information, after that change the Texture. I am afraid it will not be saved or corrupt my Object somehow, because there will not be made sprite definitions for the new sheet I guess?
But I am afraid I break anything that has to do with 2D Toolkit, or maybe even my game. (though I made a back-up before I started this)
After this is done I want to create animations for it.
Some info:
- The sprite sheet consists of a front arm, back arm, legs and a male body, a female body and 4 heads.
- Some sprites have attachPoints while others have an different anchor.
So what I want is help with a script which copies the current sprite sheet, including definitions. Then change texture of the sprite sheet. (Maybe an idea for the Editor, a Copy button for sprite sheets)
I want all character sprites in one collection, because it will be called in the same Draw Call, at least that's what I've read. (So there will be better performance);
Currently what I have is this, put in a new scene on main camera, I just run the scene, then stop scene and I have new sprite sheet but not the data:
using UnityEngine;
using UnityEditor;
using System.Collections;
using System.Collections.Generic;
public class CloneSpriteCollections : MonoBehaviour {
public tk2dSpriteCollection BaseCollection;
public Texture2D[] textures;
void Start() {
List<tk2dSpriteSheetSource> spriteSheets = new List<tk2dSpriteSheetSource>();
tk2dSpriteCollectionData data = BaseCollection.spriteCollection;
tk2dSpriteSheetSource[] sources = BaseCollection.spriteSheets;
foreach (var v in spriteSheets)
{
var t = new tk2dSpriteSheetSource();
t.CopyFrom(v);
spriteSheets.Add(t);
}
// Adding the new spritesheets
foreach(Texture2D texture in textures) {
var t = new tk2dSpriteSheetSource();
t.CopyFrom (sources [0]);
t.texture = texture;
spriteSheets.Add(t);
}
BaseCollection.spriteSheets = spriteSheets.ToArray ();
List<tk2dSpriteDefinition> newDefs = new List<tk2dSpriteDefinition>();
tk2dSpriteDefinition[] defs = data.spriteDefinitions;
foreach (tk2dSpriteDefinition def in defs) {
newDefs.Add (def);
}
int defOffset = 0;
foreach (var v in spriteSheets) {
int y = 288;
for (int i = 0; i < y; i++) {
tk2dSpriteDefinition def = defs[i];
def.name = v.Name + "/" + i;
newDefs.Add (def);
}
}
}
}
While i reread my code I found out why, I don't convert the list back to the array! Let me try that first
So now I got this: Though it does not yet add the correct textures, it makes the definitions and stuff, it does not work correctly yet!
using UnityEngine;
using UnityEditor;
using System.Collections;
using System.Collections.Generic;
public class CloneSpriteCollections : MonoBehaviour {
public tk2dSpriteCollection BaseCollection;
public Texture2D[] textures;
void Start() {
List<tk2dSpriteSheetSource> spriteSheets = new List<tk2dSpriteSheetSource>();
tk2dSpriteCollectionData data = BaseCollection.spriteCollection;
tk2dSpriteSheetSource[] sources = BaseCollection.spriteSheets;
foreach (var v in sources)
{
var t = new tk2dSpriteSheetSource();
t.CopyFrom(v);
spriteSheets.Add(t);
}
// Adding the new spritesheets
foreach(Texture2D texture in textures) {
var t = new tk2dSpriteSheetSource();
t.CopyFrom (sources [0]);
t.texture = texture;
spriteSheets.Add(t);
}
BaseCollection.spriteSheets = spriteSheets.ToArray ();
tk2dSpriteCollectionDefinition[] cDefs = BaseCollection.textureParams;
List<tk2dSpriteCollectionDefinition> newCDefs = new List<tk2dSpriteCollectionDefinition>();
foreach (tk2dSpriteCollectionDefinition def in cDefs) {
newCDefs.Add (def);
}
int sheetId = 1;
foreach (var v in spriteSheets) {
int y = 288;
for (int i = 0; i < y; i++) {
tk2dSpriteCollectionDefinition def = cDefs[i];
def.spriteSheetId = sheetId;
def.name = v.Name + "/" + i;
newCDefs.Add (def);
}
sheetId++;
}
BaseCollection.textureParams = newCDefs.ToArray();
List<tk2dSpriteDefinition> newDefs = new List<tk2dSpriteDefinition>();
tk2dSpriteDefinition[] defs = data.spriteDefinitions;
foreach (tk2dSpriteDefinition def in defs) {
newDefs.Add (def);
}
foreach (var v in spriteSheets) {
int y = 288;
for (int i = 0; i < y; i++) {
tk2dSpriteDefinition def = defs[i];
def.name = v.Name + "/" + i;
newDefs.Add (def);
}
}
BaseCollection.spriteCollection.spriteDefinitions = newDefs.ToArray ();
}
}