Hi,
Great tool!
I spent some time agonizing over the pain of implementing animated tiles to complement my game. I saw your suggestion about using materials, and decided that that is the best approach, although it had some drawbacks.
Due to the heavy use of animations in my game, each tile set needed quite a few Materials made. This is where I ran into problem number 1. Adding a new material performs a Commit. Each commit takes five minutes, so adding 20 materials takes quite a long time. My suggestion here is to remove the automatic Commit when adding a material since you need to still perform some work before you do your real Commit. I tested out this theory and everything works fine. You can also probably remove the automatic commit when deleting a material.
Then problem number 2 came. Animated tiles' materials' textures don't normally have the same UV coordinates as the tile set. Modifying material tiling and offsets seemed kind of hacky and didn't solve some of my issues. There is no great solution to this problem, but what worked for me is to keep it simple and allow the Sprite Collection to store custom UV values - only when doing additive materials - and let the Material and Shader do the rest. I think this solution would work for most use cases.
Which leads me into problem number 3. I am now stuck in my current version of the 2D Toolkit. Unless you were kind enough to consider implementing a UV solution similar to mine...
My main goal was not to touch tk2dSpriteDefinition. Here's my code.
Class: tk2dSpriteCollectionDefinition
Added public fields:
public bool UseCustomUV = false;
public Vector2[] CustomUV = new Vector2[] {new Vector2(0,0),
new Vector2(1,0),
new Vector2(0,1),
new Vector2(1,1)};
public Vector2 UvOffset;
Function: CopyFrom
Added:
CustomUV = src.CustomUV;
UseCustomUV = src.UseCustomUV;
Class: tk2dSpriteCollectionBuilder
Function: UpdateVertexCache
Code location: Directly above the line:
coll.spriteDefinitions.normalizedUvs = CalculateNormalizedUvs(uvs);
Code:
if (gen.textureParams.UseCustomUV)
{
coll.spriteDefinitions.uvs = gen.textureParams.CustomUV;
for (int l = 0; l < coll.spriteDefinitions.uvs.Length; l++)
{
coll.spriteDefinitions.uvs[l] = coll.spriteDefinitions.uvs[l] + gen.textureParams.UvOffset;
}
}
And then the Editor changes.
Class: tk2dSpriteCollectionEditorSpriteView.cs
Function: DrawSpriteEditorInspector
Code location: Just after the Material popup.
Code:
param.UseCustomUV = EditorGUILayout.Toggle("Custom UV", param.UseCustomUV);
param.CustomUV[0] = EditorGUILayout.Vector2Field("uv1", param.CustomUV[0]);
param.CustomUV[1] = EditorGUILayout.Vector2Field("uv2", param.CustomUV[1]);
param.CustomUV[2] = EditorGUILayout.Vector2Field("uv3", param.CustomUV[2]);
param.CustomUV[3] = EditorGUILayout.Vector2Field("uv4", param.CustomUV[3]);
param.UvOffset = EditorGUILayout.Vector2Field("uvOffset", param.UvOffset);
Note: When it is placed in this location, it will only show if the user is using multiple materials.
Thank you for the great tool!