- Home /
Creating minimap best performance
I want to create a Minimap from an int[][] array. A "pixel" on the Minimap has 3 possible states:
dark brown
brown
light brown
I see two options:
create the Minimap from small sprites, and dynamically change their SpriteRenderer's sprite on each frame.
dynamically create a texture on each frame, and apply this texture to the Minimap's Sprite.
Which is better if we consider performance?
If the second one, please provide me some optimized code.
isn't possible to use classic way and just use camera with culling mask and render it to texture? you can use shader to get the colors you want
the sprite flipping option sounds pretty weird to me and the texture generation is pretty expensive
not possible since it's a tile based game and the whole map is never visible
Answer by Baste · May 07, 2015 at 01:40 PM
Use one Texture2D that takes the entire space of the minimap, and use SetPixel to write the color to the pixel coordinates that match your array's index. Remember to Apply after you've changed all of the pixels you have changed.
This is essentially option 2, but without creating a new texture every frame, which would create memory garbage really, really fast.
Could you provide some code please? I've tried this, but I got !hasError error, what I really don't know what is.
Code:
using UnityEngine;
using System.Collections;
public class $$anonymous$$ini$$anonymous$$ap : $$anonymous$$onoBehaviour {
public SpriteRenderer myRenderer;
Texture2D texture;
Sprite sp;
void Start() {
texture = new Texture2D(100, 100);
}
void Update() {
for(int i = 0; i < 100; ++i) {
for(int j = 0; j < 100; ++j) {
texture.SetPixel(i, j, new Color(1, 0, 0, 0));
}
}
// Apply all SetPixel calls
texture.Apply();
sp = Sprite.Create(texture, new Rect(100, 100, 100, 100), new Vector2(0, 0));
myRenderer.sprite = sp;
}
}
Full error message:
UnityEngine.Sprite:Create(Texture2D, Rect, Vector2) $$anonymous$$ini$$anonymous$$ap:Update() (at Assets/Scripts/GameUI/$$anonymous$$ini$$anonymous$$ap.cs:24)
ok it turns out you have to use normalized values in Sprite.Create, now I did it like this:
sp = Sprite.Create(texture, new Rect(0, 0, 0.5f, 1f), new Vector2(0.0f, 0.0f), 0.1f);
I still have problems with the result, but that's an other topic
Your answer