I’m trying to create a map system. What I’d like to do is have an object with a 2d array which contains a class (or struct) at each index. (pixelMap contains a 2d array of pixel chunks) When my map object (drawPixelsToMap) goes to draw, it selects each index and looks at three different values inside the struct to determine what color to draw and at what position on the map. Here are the two c# scripts I’ve created so far:
// pixelMap.cs
public class pixelChunk
{
private int aLevel;
private int bLevel;
private int cLevel;
public pixelChunk()
{
this.aLevel = 0;
this.bLevel = 0;
this.cLevel = 0;
}
~pixelChunk()
{
//test
}
public int getA() { return aLevel; }
public int getB() { return bLevel; }
public int getC() { return cLevel; }
};
public class pixelMap {
const int SIZE = 500;
pixelChunk[,] chunkMap = new pixelChunk[SIZE, SIZE];
// Awake function
void Awake() {
for (int i = 0; i <= SIZE; i++)
{
for (int j = 0; j <= SIZE; j++)
{ chunkMap[i, j] = new pixelChunk(); }
}
}
~pixelMap()
{
//test
}
public int aLevel (int x, int y) { return chunkMap[x,y].getA(); }
public int bLevel(int x, int y) { return chunkMap[x, y].getB(); }
public int cLevel(int x, int y) { return chunkMap[x, y].getC(); }
}
And then my draw map object:
// drawPixelsToMap.cs
public class drawPixelsToMap : MonoBehaviour {
public pixelMap theMap;
public RenderTexture renderTexture;
public Renderer renderer;
Texture2D texture;
// init texture
void Start () {
texture = new Texture2D (renderTexture.width, renderTexture.height);
renderer.material.mainTexture = texture;
theMap = new pixelMap();
}
// draws pixels based on where pixels exist in map
// texture dimensions: 0 - 255 pixels
void Update () {
RenderTexture.active = renderTexture;
texture.ReadPixels (new Rect (0, 0, renderTexture.width, renderTexture.height), 0, 0);
for (int j = 0; j < renderTexture.height; j++)
{
for (int i = 0; i < renderTexture.width; i++)
{
if (theMap.aLevel(i, j) != 0)
{
texture.SetPixel(i / 2, j / 2, new Color(1, 0, 0, .5f));
}
}
}
texture.Apply ();
RenderTexture.active = null;
}
}
If I remove the if statement in my drawPixelsToMap class I can correctly draw pixels in a hard coded way with a desired color. But when I go through the debugger I notice that my pixelChunk objects are getting destroyed before I expect and that each pixelChunk is null. I suspect I’m not initializing the pixelChunks correctly. But I’m not certain. Any help on this would be greatly appreciated!