Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
7
Question by straydogstrut · May 18, 2010 at 09:53 PM · guiresolutionmatrix

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), "");

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Waz · Feb 19, 2011 at 10:35 AM 0
Share

For a fixed aspect ratio, the code is the same (since Screen.width/width == Screen.height/height).

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges