- Home /
Problem with resolution independent game interface
Hi all,
I'm using the following code to create a wayfinder in my game interface that rotates to indicate the direction of the next objective. Works perfectly.
// texture for the wayfinder var compTexture: Texture2D = null; // positioning inset from top and right edges of the screen var compInsetX : int = 150; var compInsetY : int = 100; // in-world point (empty GO) to allow us to calculate heading var pointer; // the location of the current objective var wp; // player character GO var PC : GameObject;
// angle to the current objective, matrix to hold the GUI object, // pivot point of the wayfinder // and rectangle co-ordinates to draw on screen private var heading ; private var matrixBackup:Matrix4x4; private var compPos:Vector2 ; private var compRect:Rect;
function Start(){ // PC = GameObject.Find("wp_pch_01"); pointer = GameObject.Find("objective_pointer"); wp = GameObject.Find("objective1"); }
function Update () {
// point the empty GO at the position current objective
pointer.transform.LookAt(wp.transform.position);
// store a reference to the heading calculated by subtracting the rotation
// angle of the player
// from that of the pointer
heading = (pointer.transform.eulerAngles.y) - (PC.transform.eulerAngles.y);
}
function OnGUI () { // This function handles drawing the GUI object on screen
// store the width of the wayfinder texture and height
var compassWidth : int = compTexture.width;
var compassHeight : int = compTexture.height;
// get the position of texture on screen by subtracting the X inset value
// from the left edge of the screen and subtracting half the texture width
// from this
var ScreenX : int = ((Screen.width - compInsetX) - (compassWidth * 0.5));
var ScreenY : int = compInsetY;
// a matrix to contain the GUI object to be rotated
var matrixBackup:Matrix4x4 = GUI.matrix;
// the pivot point of the graphic
var compPos:Vector2 = Vector2(ScreenX+(compassWidth*0.5),
ScreenY+(compassHeight*0.5));
// rotate the graphic around its pivot and towards the heading
GUIUtility.RotateAroundPivot(heading, compPos);
// rectangle co-ordinates to draw on screen
var compRect:Rect = Rect(ScreenX,ScreenY,compassWidth,compassHeight);
// draw the wayfinder texture in the rectangle defined
GUI.DrawTexture(compRect, compTexture);
// end of the matrix
GUI.matrix = matrixBackup;
}
I'm now trying to implement to resolution independence code from the 3D Platformer tutorial so that the game interface will rescale for different resolutions. The graphics are designed for a 1024 x 768 resolution and adding the lines
GUI.matrix = Matrix4x4.TRS (Vector3(0, 0, 0), Quaternion.identity, Vector3(Screen.height / nativeVerticalResolution, Screen.height / nativeVerticalResolution, 1));
var scaledResolutionWidth = nativeVerticalResolution / Screen.height * Screen.width;
where nativeVerticalResolution is set to 1024, and scaledResolutionWidth replaces Screen.width in my previous code, successfully rescales the other graphic in the interface, a health indicator (in a different script).
However, with the wayfinder, it's pivot point is wrong and I get a null reference exception error, regardless of which resolution I pick in the Game window. Actually, I get the same error when changing resolutions without the resolution independence code.
I found a post on the forums regarding problems with the mouse position where the answer was that the mouse's co-ordinates didn't match those of the new screen resolution, and the following lines were provided as a solution. Is this appropriate in my case?
var transMouse = GUI.matrix.inverse.MultiplyPoint3x4(Vector3(ix, Screen.height - iy, 1));
GUI.Button(Rect(transMouse.x, transMouse.y, 32, 32), "");
Answer by cncguy · Jul 06, 2010 at 03:24 AM
I'm wondering if part of your problem is that you are scaling your gui in both X and Y by Screen.height / nativeVerticalResolution. Also you seem to have Vertical and horizontal resolution switched.
Why don't you try something like:
var oldMatrix : Matrix4x4; var tMatrix : Matrix4x4; var width : int =1024; var height : int =768;
oldMatrix = GUI.matrix; tMatrix = Matrix4x4.TRS(Vector3.zero, Quaternion.identity, Vector(1.0*Screen.width/width, 1.0*Screen.height/height, 1.0)); GUI.matrix = tMatrix;
I have used this succesfully for (part of) my GUI. This way you are scaling both X and Y by appropriate proportions. I think in this case you would not use your scaledResolutionWidth.
For a fixed aspect ratio, the code is the same (since Screen.width/width == Screen.height/height).
Your answer
Follow this Question
Related Questions
GUI Matrix Upscaling 0 Answers
IOS Resolution Scaling 0 Answers
resizing GUI elements through GUI matrix 1 Answer
Resolution for assets on Android 1 Answer
Anti Alliasing is affecting an part of my GUI negatively 0 Answers