Hello, I have a panel in my UI canvas. I’d like to draw multiple coloured square on that panel (to make a simple minimap). I tried to draw a texture in the function OnGui at the position of the panel, but it doesn’t work, the position doesn’t match. I read there are differences between an old GUI system and the new UI system, I’m using unity 5.5 but I have no clue what the difference is between both.
Is there a way to draw coloured squares and lines on a panel in a canvas ?
The GUI methods were the UI approach pre-Unity ~4.6. It’s a real pain to work with past simple user interfaces, and doesn’t make animation, layout, 3D interfaces, very easy. The new UI system integrates these systems a lot better, and I would recommend you focus on it (despite the harder learning curve) because it’s going to make your life way better. I mean like, I think I shed a tear when it first came out. Learn game development w/ Unity | Courses & tutorials in game design, VR, AR, & Real-time 3D | Unity Learn
Here’s an approach to creating a mipmap using the new UI system:
In this screenshot, I have a 3d object on a custom layer I called “UIModelRender”. I set a camera to only render things on the UIModelRender layer. I then created a render texture and set it as the target texture of the camera, and then put the same texture in a RawImage UI Panel.
For your minimap, you would set things up in a similar way. You could would your camera far above the play area and instantiate your squares and lines where the players actually are. Making sure those squares and lines are on a custom layer your players won’t see.
The plus side is this is easier to extend; e.g. if you wanted to add 3D objects or new symbols without touching too much code. However, it’s a more performance intensive vs. an optimized 2D minimap. But to be honest, I would go with this regardless. (This also isn’t just me, there are several tutorials out on youtube with this same approach.)
I found a way to draw on canvas using only the UI System reading Unity’s tutorials.
It’s actually pretty easy :
-Place a panel in your scene named “Minimap” and access it in a script using
GameObject minimap = GameObject.Find ("Minimap");
-Instantiate new panels and set the minimap as the parent
GameObject panel = Instantiate (Panel); panel.transform.SetParent (minimap.transform, false);
-Modify the panel’s size to draw a square or a line, modify it’s colour. You can also modify its position using its rect transform.