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
0
Question by gardian06 · Aug 26, 2013 at 09:03 PM · guiresolutionmatrix

resizing GUI elements through GUI matrix

I am currently looking into rescaling my GUI systems based on screen size (considering that in web-player with things like zoom factor, and such building to a resolution is rather difficult), but have been unlucky so far. the most promissing thing I have found was here, but I don't know how to arrive at the position vector, and the quaternion rotation.

using this code:

 using UnityEngine;
 using System.Collections;
 
 public class GUIMatrixTest : MonoBehaviour {
 
     private Rect box0 = new Rect(0,0,0,0);
     private int tWidth;
     private int tHeight;
     
     public string msg = 
         "Here is a text box for testing against." + '\n' 
             + "It has text in it, and allows us the" + '\n'
             + "see results.";
     // Use this for initialization
     void Start () {
             tWidth = Screen.width;
             tHeight = Screen.height;
     }
     
     // Update is called once per frame
     void Update () {
         if(tWidth != Screen.width || tHeight != Screen.height){
             Matrix4x4 mat = GUI.matrix;
             Vector4 tempV = Vector4.zero;
             
             Debug.Log("before: " + mat.ToString());
             
             Vector3 vec0 = Vector3.zero;
             tempV = mat.GetRow(0);
             vec0.x = tempV.x; vec0.y = tempV.y; vec0.z = tempV.z;
             
             Quaternion vec1 = Quaternion.identity;
             tempV = mat.GetRow(1);
             vec1.x = tempV.x; vec1.y = tempV.y; vec1.z = tempV.z; vec1.w = tempV.w;
             
             Vector3 vec2 = Vector3.zero;
             vec2.x = 800/Screen.width; vec2.y = 600/Screen.height; vec2.y = 1;
             mat.SetTRS(vec0, vec1, vec2);
             
             Debug.Log("after: " + mat.ToString());
             
             GUI.matrix = mat;
             
             tWidth = Screen.width;
             tHeight = Screen.height;
         }
     }
     
     public void OnGUI(){
         box0.width = 280; box0.height = 100;
         GUI.Box(box0, msg);
     }
 }
 

I get the error "Ignoring invalid matrix assigned to GUI.matrix - the matrix needs to be invertible. Did you scale by 0 on z-axis?"

any help in either correcting this, or a different solution would be appreciated.

Comment
Add comment · Show 1
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 gardian06 · Aug 27, 2013 at 02:31 PM 0
Share

edited code to better draw data from original matrix.

currently before any work is done I get:

 1000
 0100
 0010
 0001

which is simply the identity of a 4x4, but after I do the work I get

 -1001
  0100
  0010
  0001

So I can understand where I get the error of not being invertible, but I am drawing the position from the GUI.matrix position, and even working it out by hand I don't see how row reduction results in that.

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by gardian06 · Sep 06, 2013 at 02:49 PM

the best solution for this is to not modify the GUI.matrix directly, but instead to use GUIUtility.ScaleAroundPivot though this can look quite clunky when sitting in multiple places in you OnGUI(), and the scaling is always relative to the pivot

(for example if the pivot is screen center, and Vector is (2,2) then everything will be shifted/scaled by a factor of 2 from the screen center)

the best suggestion that I can make is do your scaling per item this way you can get cleaner results in terms of placement, and have you pivot point relative the rect you working with. since it is rather difficult to extend the GUI class it would be best to just have a static extension class do the work. though this might either require you to create sudo-overloads per GUI function, or to have this helper simply maintain the original Matrix, scale the matrix, and then be able to reset it.

the first implementation would look something like this:

     //...
     public static void Label(Rect rect, string text, Vector2 scale, TextAnchor pivot){
         Label(rect, new GUIContent(text), "label", scale, pivot);
     }
     public static void Label(Rect rect, string text, GUIStyle style, Vector2 scale, TextAnchor pivot){
         Label(rect, new GUIContent(text), style, scale, pivot);
     }
     
     public static void Label(Rect rect, Texture texture, Vector2 scale, TextAnchor pivot){
         Label(rect, new GUIContent(texture), "label", scale, pivot);
     }
     public static void Label(Rect rect, Texture texture, GUIStyle style, Vector2 scale, TextAnchor pivot){
         Label(rect, new GUIContent(texture), style, scale, pivot);
     }
 
     public static void Label(Rect rect, GUIContent content, Vector2 scale, TextAnchor pivot){
         Label(rect, content, "label", scale, pivot);
     }
     public static void Label(Rect rect, GUIContent content, GUIStyle style, Vector2 scale, TextAnchor pivot){
         Matrix4x4 backupMat = GUI.matrix;
         Vector2 pivotPoint = PivotVector(ref rect, pivot);
         GUIUtility.ScaleAroundPivot(scale, pivotPoint);
         GUI.Label(rect, content, style);
         GUI.matrix = backupMat;
     }
     //...
     public static Vector2 PivotVector(ref Rect rect, TextAnchor pivot){
         Vector2 pivotPoint = Vector2.zero;
         //set to the top left corner of the thing to be scaled
         pivotPoint.x = rect.x; pivotPoint.y = rect.y;
         switch(pivot){
         case TextAnchor.UpperCenter:    pivotPoint.x += rect.width/2;                                    break;
         case TextAnchor.UpperRight:        pivotPoint.x += rect.width;                                        break;
         case TextAnchor.MiddleLeft:                                        pivotPoint.y += rect.height/2;    break;
         case TextAnchor.MiddleCenter:    pivotPoint.x += rect.width/2;    pivotPoint.y += rect.height/2;    break;
         case TextAnchor.MiddleRight:    pivotPoint.x += rect.width;        pivotPoint.y += rect.height/2;    break;
         case TextAnchor.LowerLeft:                                        pivotPoint.y += rect.height;    break;
         case TextAnchor.LowerCenter:    pivotPoint.x += rect.width/2;    pivotPoint.y += rect.height;    break;
         case TextAnchor.LowerRight:        pivotPoint.x += rect.width;        pivotPoint.y += rect.height;    break;
         }
         return pivotPoint;
     }
     //...

I have not written the other implementation, but it should be readily practical to utilize.

the above code is functional, but there is a direct exception of not behaving between of a GUI.BeginGroup(), and GUI.EndGroup(), but this can be superseded by creating a BeginGroup(), but don't expect to be able to use the ScaleAroundPivot effectively, but GUILayout is still a viable option inside of the group, but this uses full space filling, and you may have to play with padding directly.

Comment
Add comment · 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

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

15 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Problem with resolution independent game interface 1 Answer

GUI Matrix Upscaling 0 Answers

IOS Resolution Scaling 0 Answers

Menu screen for different resolutions 1 Answer

GUI clipping with 3D matrix 1 Answer


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