Hello Guest

Author Topic: Solved: Creating full copies of first sprite sheet in same collection  (Read 4423 times)

Kevin

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 9
    • View Profile
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:
Code: [Select]
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!
Code: [Select]
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 ();
}
}
« Last Edit: September 03, 2014, 08:15:51 pm by Kevin »

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Creating full copies of first sprite sheet in same collection
« Reply #1 on: September 03, 2014, 12:32:10 pm »
The sprite sheet stuff creates the individual sprites too. You might be better off just copying those (the tk2dSpriteCollectionDefinition). Identify the ones with fromSpriteSheet and hasSpriteSheetId, copy them, assign your new texture to it, and set hasSpriteSheetId to false. That should be it really, and then rebuild the sprite collection. It probably isn't worth copying the sprite sheet object unless you want to be able to edit it in the editor.

Kevin

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 9
    • View Profile
Re: Creating full copies of first sprite sheet in same collection
« Reply #2 on: September 03, 2014, 08:15:40 pm »
I've done it a bit differently, but it works! Here is my code:
Code: [Select]
using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class CloneSpriteDefinitions : MonoBehaviour {

public tk2dSpriteCollection BaseCollection;
public int offset = 288;



// Use this for initialization
void Start () {
tk2dSpriteCollectionDefinition[] cDefs = BaseCollection.textureParams;
List<tk2dSpriteCollectionDefinition> newCDefs = new List<tk2dSpriteCollectionDefinition>();
foreach (tk2dSpriteCollectionDefinition def in cDefs) {
newCDefs.Add (def);
}
for(int i = offset; i <  newCDefs.Count; i++) {
string defName = newCDefs[i].name;
int defSpriteSheetId = newCDefs[i].spriteSheetId;
Texture2D defTexture = newCDefs[i].texture;
int materialId = newCDefs[i].materialId;
tk2dSpriteCollectionDefinition.Source source = newCDefs[i].source;
newCDefs[i].CopyFrom(newCDefs[i - offset]);
newCDefs[i].name = defName;
newCDefs[i].spriteSheetId = defSpriteSheetId;
newCDefs[i].texture = defTexture;
newCDefs[i].materialId = materialId;
newCDefs[i].source = source;

}
BaseCollection.textureParams = newCDefs.ToArray();
BaseCollection.Upgrade ();
}
}

Basicly, what you need to do is add a sprite sheet to the same collection, give it some values. And then commit it. Then you need to assign this script in an empty scene and add the collection to it. Set the offset with how many sprites there are in the first sprite sheet, for me it was 288 sprites so thats the default value. Then play it once, check your sprite sheet. it copied the Anchors and such!