- Home /
Draw minimap on panel
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 ?
Answer by tourdetour · Jan 29, 2017 at 07:38 PM
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.
Answer by FlyingHighUp · Jan 23, 2017 at 02:22 AM
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. https://unity3d.com/learn/tutorials/topics/user-interface-ui
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.)
@FlyingHighUp Thank you very much, that would work !
However, I have already too much objects in my scene (10^4 cubes acting as walls for a maze), and it's already laggy, so adding twice as much as objects for the $$anonymous$$i-map, that would be too much for older computers I think.
$$anonymous$$y maze is stored in a two dimensional array so it would be easy to draw a 2D $$anonymous$$i-map. I'm more used to the openGL kind of rendering, and I still don't really know how to draw something using the new UI system, appart from using objects.
Thanks for you link, I'm going to read "Creating a Tic-Tac-Toe game using only the UI", they seem to draw lines and stuff using the UI, I could use that maybe.
Your answer
Follow this Question
Related Questions
Canvas with GUI elements in prefab act strangely 0 Answers
UI not scaling correctly with screen resolution 1 Answer
[4.6 GUI] Displaying my pause menu when ESC is pressed 2 Answers
Use Unity UI For 2D Games Or Custom Objects Instead? 2 Answers
World space buttons and preventing UI touch passthrough 0 Answers