Hello Guest

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Messages - Euthyphro

Pages: [1] 2
1
Support / spriteDefinitions has empty values in it?
« on: December 11, 2014, 09:18:58 pm »
The sprites on one of my sprite sheets randomly come out like this image rather than the actual sprite:


This doesn't happen every time. I'm trying to pull out a random sprite with the following code added to t2dSpriteCollectionData which returns "blank" values:

Code: [Select]
public string GetRandomSpriteName()
{
return spriteDefinitions[Random.Range(0, spriteDefinitions.Length)].name;
}

Any suggestions on why this would return a value most of the time but the odd time it is an empty string? In the collection, every sprite has a name and yet this sometimes returns an empty string.

2
You cant save that to disk at runtime, unity serialization only works in the editor...

Yea that's my point, you can't serialize. So how might I begin to actually save the sprite collection data while at runtime? A lot of my players request to be able to make custom spritemaps by dropping their images in a game folder so they can load their own custom textures in-game but right now with tk2d the way it is they would have to create the collection from scratch each time using CreateFromTexture and as a result that's huge load time every time you go in-game especially when there are 40+ tilemaps each with 256 tiles per tilemap. Ideally instead I would like to have them created once and saved. Short of rewriting tk2d, is there a quicker way to do this?

3
How would won save tk2dSpriteCollectionData to disk so that it doesn't have to be recreated at a later date? I'm guessing it wouldn't be able to be serialized and saved. I couldn't find a decent explanation and I've been fishing around the tk2d files for the past several hours for clues.

This is the code I have, it creates a sprite collection from a png file but the file save does not work because it wont let me serialize tk2dSpriteCollectionData.

Code: [Select]
using UnityEngine;
using System.Collections;
using System.IO;
using PixelShootLibrary;
public class PixelShootCreateTileSpriteCollectiona : MonoBehaviour {


string imageLocation = @"C:\Users\Euthyphro\AppData\Roaming\PixelShoot\MapTextures\stone16x16px.png";
string spriteCollectionDataLocation = @"C:\Users\Euthyphro\AppData\Roaming\PixelShoot\MapTextures\SpriteCollection.dat";
public tk2dSpriteCollectionData spriteCollectionData;
public Texture2D texture;
tk2dBaseSprite spriteInstance = null;

void Start ()
       {
CreateSpriteCollection ();
}
void CreateSpriteCollection()
{
FileManager fm = new FileManager();
if(File.Exists(spriteCollectionDataLocation))
{
spriteCollectionData = (tk2dSpriteCollectionData)fm.OpenStream(spriteCollectionDataLocation);
}
else
{
if(File.Exists(imageLocation))
{
byte[] fileData = File.ReadAllBytes(imageLocation);
spriteCollectionData = Resources.Load ("Sprites/TileMapCollection", typeof(tk2dSpriteCollectionData)) as tk2dSpriteCollectionData;
texture = new Texture2D(288,270);
texture.filterMode = FilterMode.Point;
texture.LoadImage(fileData);
spriteCollectionData.textureFilterMode = FilterMode.Point;
string[] names = new string[256];
Rect[] regions = new Rect[256];
Vector2[] anchors = new Vector2[256];
for(int  y = 0; y < 16; y++)
{
for(int x = 0; x < 16; x++)
{
int pos = x+(16*y);
names[x+(16*y)] = pos.ToString()+"tile";
int offsetX = x;
int offsetY = y;
if(y > 0)
{
offsetY = y*16+(2*y);
}
if(x > 0)
{
offsetX = x*16+(2*x);
}
regions[x+(16*y)] = new Rect(offsetX, offsetY, 16, 16) ;
anchors[x+(16*y)] = new Vector2(offsetX, offsetY);
spriteCollectionData = tk2dSpriteCollectionData.CreateFromTexture(texture,tk2dSpriteCollectionSize.ForTk2dCamera(), names, regions, anchors);
}
}

fm.WriteStream(spriteCollectionDataLocation, spriteCollectionData);
}
}
}


}

4
That fixed it, thanks again!

5
Since having done this edit I seem to be having a problem with the TileMapData file. It works perfectly until I close the editor and open. When I reopen the editor, the tilemap render layers create a new gameobject for the layers.

Here is a screen grab of before and after followed by code based on your suggestions - which like I said, works wonderfully until closing/opening - in which case I then have to rebuild the layers. Likewise I cannot compile an exe without this same issue happening when opening the exe after.
Before close:

After close/reopen of Unity (this also happens when compiling the game into exe and running out of the editor)


Code: [Select]
public tk2dTileMap[] tileMaps;

public void BuildLayers()
{
                       tileMaps = FindObjectsOfType(typeof(tk2dTileMap)) as tk2dTileMap[];
Debug.Log("Tile map count: "+tileMaps.Count().ToString());
int counterMaps = 0;
int counterLayers = 0;
foreach (tk2dTileMap tileMap in tileMaps)
{
                                //not entirely sure why this is here, it was in example given by unikronsoftware
int layerId = -1;
for (int i = 0; i < tileMap.data.Layers.Length; ++i) {
if (tileMap.data.Layers[i].name == "Blocker") {
layerId = i;
break;
}
}

tileMap.BeginEditMode();
                                //clear old layers out since we'll be recreating them. May not be necessary as we can just use "FindOrCrateLayer()" but kept in for now.
for(int i = 0; i <tileMap.data.Layers.Length; i++)
{
Debug.LogError("Deleting layer: "+tileMap.data.Layers[i].name);
tk2dEditor.TileMap.TileMapUtility.DeleteLayer(tileMap,  i);


}

        //set tilemap so that we are using the sorting layer sort method
tileMap.data.useSortingLayers = true;
for(int i = 0; i < 6; i++)
{
int newLayerIndex = tk2dEditor.TileMap.TileMapUtility.FindOrCreateLayer(tileMap, "Layer "+i.ToString());
// tileMap.data.Layers[].generateCollider = true;

                                       
                                        //index from TileMapUtility is oddly not ordered based on the order it was created. Sometimes
                                        // the new layer index returned is different. i.e. this is created from 0 to 5 but sometimes index returned is not sequential to the order created.
                                        //instead we just give the layer the name "Layer +" index number.
tileMap.data.Layers[newLayerIndex].name = "Layer "+newLayerIndex.ToString(); 
tileMap.data.Layers[newLayerIndex].z = 1;
tileMap.data.Layers[newLayerIndex].unityLayer = 20+i;
int t = 5-i;
tileMap.data.Layers[newLayerIndex].sortingLayerName = "TileMapLayer"+t.ToString();
if(newLayerIndex != 0)
{
tileMap.data.Layers[newLayerIndex].generateCollider = false;
}
counterLayers++;
tileMap.Layers[newLayerIndex].Optimize();
}
counterMaps++;
Debug.Log("Completed maps: "+counterMaps.ToString()+" Completed layers: "+counterLayers.ToString());
tileMap.EndEditMode();

}
}

6


You dont need to modify or copy any of the functions. tk2dEditor IS accessible from editor scripts, and if you're doing that in the editor you should be using an editor script.

How might I do that?  The beginning of my class looks like this, and as far as I'm aware, this would make it an editor script. With that said, not able to access tk2dEditor. "Using tk2dEditor" is no go and calling tk2dEditor from anywhere it doesn't show up within any methods or functions of this editor script.


*Edit* I think I got it, I put it in the same folder as tk2deditor and it seems to be working now.

Code: [Select]
#if UNITY_EDITOR
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEditor;
using tk2dRuntime.TileMap;

[CustomEditor(typeof(TileDataScript))] //tile data script attached to GameManager GameObject.
public class TileDataEditor : Editor {

/***
functions and methods etc

*/

}
#endif

7
If you wnat to delete a layer, look at tk2dTIleMapUtility.DeleteLayer. That deletes a layer correct - its what the editor code uses.

Any chance on a quick little tutorial on looping through to create tile map layers properly? This is getting extremely messy having to modify methods and functions that are not public and either rewrite them or change the tk2d scripts.

The problem I'm having creating them is that they aren't all instantiating after having essentially copied and pasted the FindOrCreateLayer(tilemap, name) function. I guess there is more to it but that is my point, it's getting very messy because you can't just access tk2deditor from outside scripts without doing several other things.

8
I have about 40 tile maps and multiple layers, rather than modifying each one by hand and wishing I was a robot, I'm trying to write a script to do that. The problem is that this is leaving the old layers under the render data game object and creating the layers rather oddly. Obviously I'm doing this wrong, what is the best practice to add/remove tile map layers through code to ensure everything is taken care of?

Here is my code for what I have:

Code: [Select]
if(GUILayout.Button("Create Tile Map Layers"))
{
List<tk2dTileMapData> tileMapDataList = new List<tk2dTileMapData>();
tileMaps = FindObjectsOfType(typeof(tk2dTileMap)) as tk2dTileMap[];
Debug.LogError("Size: "+tileMaps.Count().ToString());
foreach (tk2dTileMap tileMap in tileMaps)
{
tileMap.data.tileMapLayers.Clear();
tileMap.BeginEditMode();

for(int i = 0; i < 5; i++)
{
tk2dRuntime.TileMap.LayerInfo newLayer = new tk2dRuntime.TileMap.LayerInfo();
newLayer.name = "Layer "+i.ToString();
newLayer.generateCollider = false;
newLayer.useColor = true;
newLayer.z = i;
newLayer.unityLayer = 20 + i;
newLayer.sortingOrder = i;
newLayer.hash = 0x70d32b98;
if(i == 1)
{
newLayer.generateCollider = true;

}
if(i == 0)
{
tileMap.data.tileMapLayers[i] = newLayer;
}
else tileMap.data.tileMapLayers.Add(newLayer);

Debug.LogError(tileMap.name+" i:"+i.ToString());
}
tileMap.EndEditMode();
}
}



9
Support / Re: Setting Tilemap opacity to 100% opaque
« on: June 27, 2014, 10:04:02 pm »
Solved, problem was the z layer of tiles were both set to 0 which caused random behaviour with transparency.

10
Support / Setting Tilemap opacity to 100% opaque
« on: June 27, 2014, 09:12:02 pm »
It seems that when I build my tilemap through code, the tiles opacity is not 100%. This is problematic with lighting because it makes the tiles look somewhat transparent. What is more, when using multiple tile layers it causes the layers beneath to show through the top layer.

I've tried turning cursor tile opacity to 1.0 but I do not think this is where the problem rests.

The shader I'm using is tk2d/LitBlendVertexColor. I've tried every other shader as well and this problem persists.

The actual image files are 100% opaque but for some reason with tk2d they are showing up as somewhat transparent whenever I use any lighting shaders.

Any suggestions? How can I set the opacity by code, if this is necessary?


Thanks.

11
Support / Drawing sprites and merging together
« on: June 17, 2014, 10:48:26 pm »
For my project I need to merge two transparent sprites onto one sprite by sampling data from one and merging certain pixels of that sprite onto another. Is there a way with tk2d to do this? Looking through the support docs I don't see any functionality that allows a dev to even sample what colour is at a pixel coordinate of a sprite image. Any suggestions? Coming from Systems.Drawing (which isn't supported in Unity, I'm kind of looking fo something along the lines of Bitmap.GetPixel(x,y) to sample and then redrawing the image from there.

Thanks.

12
Support / Rendering path not lighting correctly
« on: April 09, 2014, 03:32:29 pm »
The rendering path seems to be cutting out and only displaying within a small box as you can see in the image below. In the editor, the lighting looks good, but in the game-preview the rendering path only lights a small box.



I've tried tweaking the settings and adjusting, any suggestions?


Thanks.

13
Support / Re: IndexOutOfRangeException FindChunkAndCoordinate
« on: January 15, 2014, 11:55:20 pm »
It is x and y 32 that is causing the issue as the tileset is only 32 wide/high, thus anything over 31 would be out of bounds. With that said, why would it return 32 from "GetTileAtPosition"? in the first place? Also, might I suggest in a future release that instead of an out of bounds tile causing tk2d to fail (i.e. tk2d functions no longer work after this type of error), instead maybe a try/catch? Essentially if this is called out of bounds, like I said before, you have to restart the entire scene since tk2d tilemap dies out.

px,py = on screen coordiantes, x,y are the coordinates returned from tileMap.GetTileAtPosition(whichBlockToCheck, out x, out y); "whichBlockToCheck" originates from the world coordinates of a mouse click.

Code: [Select]
px,py:256,844  x,y:32,22
UnityEngine.Debug:Log(Object)

Here in another case:
Code: [Select]
px,py:201,833  x,y:32,23
UnityEngine.Debug:Log(Object)

Code: [Select]
x,y:222,810  px,py:21,32
UnityEngine.Debug:Log(Object)

14
Support / IndexOutOfRangeException FindChunkAndCoordinate
« on: January 15, 2014, 07:10:50 am »
I receive the following error randomly when trying to modify a tile programatically. It is difficult because I cannot replicate it with any one tile, each time it seems to happen to a different tile. When the error does occur, tk2d stops working altogether and the scene has to be stopped and restarted.

Code:
Code: [Select]
tileMap.GetTileAtPosition(whichBlockToCheck,  out x, out y);
tileMap.SetTile(x, y, 0, -1);
tileMap.Build();

Error:
Code: [Select]
IndexOutOfRangeException: Array index is out of range.
tk2dRuntime.TileMap.Layer.FindChunkAndCoordinate (Int32 x, Int32 y, System.Int32& offset) (at Assets/TK2DROOT/tk2dTileMap/Code/tk2dTileMapChunks.cs:372)
tk2dRuntime.TileMap.Layer.GetRawTileValue (Int32 x, Int32 y, System.Int32& value) (at Assets/TK2DROOT/tk2dTileMap/Code/tk2dTileMapChunks.cs:384)
tk2dRuntime.TileMap.Layer.GetTileFlags (Int32 x, Int32 y) (at Assets/TK2DROOT/tk2dTileMap/Code/tk2dTileMapChunks.cs:428)
tk2dRuntime.TileMap.Layer.SetTile (Int32 x, Int32 y, Int32 tile) (at Assets/TK2DROOT/tk2dTileMap/Code/tk2dTileMapChunks.cs:450)
PLTileGridManager+<MineBlock>c__IteratorA.MoveNext () (at Assets/Scripts/PLTileGridManager.cs:606)
UnityEngine.MonoBehaviour:StartCoroutine(IEnumerator)

15
You're not meant to reposition / move chunks in the tile map editor, which is why this isn't documented. Its impossible to manage and support all the permutations of what people could do with it, and we need to draw a line somewhere to make it maintainable.

If you wanted to do this, the static sprite batcher could work quite well - you'd have to step through and position the tiles yourself, but that should be a constant anyway. You'd just have to create the pooled static sprite batchers yourself, and you can pretty much do what you want with them, pool them and recycle as needed. That will almost certainly be easier to manage than trying to force the tile map system to do something it wasn't designed for.

Thanks. I've actually got it to work this way, despite it being unintended. With some modification, it seems to work quite well. You should consider adding more features about this as more and more game dev's are looking to create procedurally generated large scale maps. With your great product, it would be a strong selling point.

One other question, is there a way to tell which layer of a tilemap was clicked with what you have? If not I'll modify the plugin possibly but I'll also try your sprite batcher suggestion as well. The thing is that you can mine through the tiles, so either using sprite batcher to build or the tilemap to build after each tile is modified can be a problem. I have an idea how to increase speed on this but going for what ever takes less time to modify.



Just wanted to add, even with Unity 2D being released, your product is well worth the $65. Unity 4.3 is great but I find SpriteRenderer can be quite slow compared to meshes. Even just slicing up of sprites on tk2d is much better than 4.3.



Pages: [1] 2