- Home /
How do you get a minimap to stay rooted to the top left corner of the screen despite resolution changes?
I am making a TBS game and am currently working on the HUD, I have tried different code types and all were unsuccessful.
For example part of the HUD is the minimap that i want to root to the top left corner of the screen, it works for one resolution but when i switch to a different one it appears in a different location.
I have looked through the forums and either the answer doesn't work or the question is not answered.
What is the best way to fix it?
Answer by sparkzbarca · Nov 21, 2012 at 04:52 AM
use
http://docs.unity3d.com/Documentation/ScriptReference/Camera.ViewportToWorldPoint.html
the top left of the screen is Camera.main.ViewportToWorldPoint(new vector3(0,1,0));
the top left of the screen and 2 meters forward is
Camera.main.ViewportToWorldPoint(new vector3(0,1,2));
the center of the screen is half way along the x axis and half way along the y axis or just FYI an 'f' after a number makes it a float in case you were unaware.
Camera.main.ViewportToWorldPoint(new vector3(.5f,.5f,0));
hopefully that answers your question.
Viewport functions are independant of the resolution.
(0,0) is the bottom left and (1,1) is the top right regardless of resolution.
I can see how this would work but I do not know how to attach the texture to it, is this possible?
Can someone please clarify this answer?
If I have a $$anonymous$$ain Camera and I place a $$anonymous$$ini-$$anonymous$$ap or a Picture-in-Picture camera that I want anchored in the top left (or right) I set the second cameras view-port Rectangle and then I attach a script like the one in the scripting reference:
using UnityEngine;
using System.Collections;
public class Example : $$anonymous$$onoBehaviour {
void OnDrawGizmosSelected() {
Vector3 p = camera.ViewportToWorldPoint(new Vector3(1, 1, camera.nearClipPlane));
Gizmos.color = Color.yellow;
Gizmos.DrawSphere(p, 0.1F);
}
}
using (0,1,camera.nearClipPlane) for left top or (1,1,camera.nearClipPlane) for the top right, I still end up losing the edges of the $$anonymous$$i screen when moving the game window size in free aspect mode.
Any help clarifying this would be greatly appreciated.
I found THIS was much closer to what I needed.
using UnityEngine;
using System.Collections;
public class $$anonymous$$y$$anonymous$$inimapResizer : $$anonymous$$onoBehaviour
{
private float itsWidth;
private float itsHeight;
private Camera its$$anonymous$$inimapCamera = null;
// Use this for initialization
void Start ()
{
its$$anonymous$$inimapCamera = GetComponent();
}
// Update is called once per frame
void Update ()
{
itsHeight = 1.0f/3.0f; //set the $$anonymous$$imap height to 1/3 of the screen
float itsScreenHeightInPixels = Screen.height*itsHeight;
float itsScreenWidthInPixels = (itsScreenHeightInPixels/9.0f)*16.0f;
itsWidth = itsScreenWidthInPixels/Screen.width;
float anX = (1-itsWidth)/2.0f; //centers the $$anonymous$$imap
its$$anonymous$$inimapCamera.rect = new Rect(anX,0.0f,itsWidth,itsHeight);
}
}
Answer by kolmich · Nov 26, 2012 at 10:45 PM
Hi,
Check out the KGFMapSystem, [http://forum.unity3d.com/threads/147121-Minimap-Map-System-RELEASED][1] [1]: http://forum.unity3d.com/threads/147121-Minimap-Map-System-RELEASED
We have already solved tons of other minimap problems here (4 month of developement + bugfixing + content creation). This unity3d minimap works out of the box, is fully customizeable and if you will need an additional feature just make a feature request at kolmich.at/forum.
Michal
Answer by Owen-Reynolds · Jan 30, 2014 at 10:39 PM
The standard solution is to set GUIs/miniMaps/etc... once at the start, and then rerun those calculation whenever the window changes size. Actually, the most common fix is just to never let the user change size -- run only full-screen. Can you even resize iOS/Android apps?
Some systems provide an OnResize() callback, where you can just paste the "setup GUI coords" code. Unity doesn't have that (I assume since not all platforms support it?) So you have to check "did the screen size change" yourself (goggle "Unity resize callback.")
Your answer
Follow this Question
Related Questions
Minimap blib icon error 1 Answer
Problems with Minimap 1 Answer
How to position 3D-GUI-Mesh on change of aspect ratio? 0 Answers
Maintaining Image Aspect Ratios in GUI using Matrix? 1 Answer