Search Unity

  1. Calling all beginners! Join the FPS Beginners Mods Challenge until December 13.
    Dismiss Notice
  2. It's Cyber Week at the Asset Store!
    Dismiss Notice

Matrix4x4

Discussion in 'Immediate Mode GUI (IMGUI)' started by Fuzzypup, Nov 28, 2019.

  1. Fuzzypup

    Fuzzypup

    Joined:
    Aug 13, 2013
    Posts:
    115
    I've been trying to figure out why this issue is happening. I researched it but found no solutions or the same question asked without a reply.

    I am working in Unity 2017. I can't update higher for the game I developed. I started this game in an older unity version hard coding elements because I generally don't like to use canvas and associated objects. I'd rather read code then memorize putting objects together.

    So here is the problem. I forgot about different screen resolutions. So I have to add a scaler. I am using Matrix4x4 to scale the UI. It works great..... except....... All the UI button functionalities are misaligned. If I hit a gui.button it doesn't work. I press one button in a toolbar and it clicks another instead?!?!?! I have to press it X pixels over to the right for it to work I think that is what is happening. If I scale to 1f it works perfectly. I don't know what I am missing. Everything is hard coded with GUI.Button, GUI.Box., etc, etc.


    Vector3 scale = new Vector3(floatScalerVariable, floatScalerVariable, 1f);
    GUI.matrix = Matrix4x4.TRS(new Vector3(0, 0, 0), Quaternion.identity, scale);
    GUI.skin = gameSkin;
    GUI.depth = 5;

    //FOR EXAMPLE this button simply does not work. I have to press it somewhere else for it to work
    if (GUI.Button(undo50Rect, undo50Content)) UndoMove.RestoreCurrentMove();
     
    Last edited: Nov 28, 2019
  2. Fuzzypup

    Fuzzypup

    Joined:
    Aug 13, 2013
    Posts:
    115
    Also tried this and it didn't work with the same issues where buttons can't be pressed


    float xFactor = Screen.width / 1920f;
    float yFactor = Screen.height / 1080f;
    GUIUtility.ScaleAroundPivot(new Vector2(xFactor, yFactor), Vector2.zero);

    It scales right but the UI is not functional or shifted functional.
     
  3. Fuzzypup

    Fuzzypup

    Joined:
    Aug 13, 2013
    Posts:
    115
    Ok so now I have two single objects that have gui in which it works.
    Two other objects in which the actual button click is off.

    It seems like if I scale to .5 the physical image is correct.
    The button click itself is 0.5 ^ 2. Any scale does that So .25 = 0.25 ^ 2.

    I can't figure it out. Does anything interfere with the Matrix4x4.TRS other than itself? Makes no sense why it works correctly with buttons on one object and not another. I even only put one button on the non-working object and the click was still off.

    Really appreciate some help.
     
  4. Fuzzypup

    Fuzzypup

    Joined:
    Aug 13, 2013
    Posts:
    115
    So for all that do this. I solved the problem.

    Here is the problem. Say you have 4 objects that have a coded OnGUI().
    You can't set it on the 1st object and expect it to affect the others.
    Ok so you think you can set it in each object. Nope it multiplies each scaling.
    You have to set it and clear it in each object that has a coded GUI. Why I don't know.

    float fractionToScale = .5f //for example

    //SETTING IT at the start of the object's OnGUI()
    Vector3 scale = new Vector3(fractionToScale, fractionToScale, 1f);
    GUI.matrix = Matrix4x4.TRS(Vector3.zero, Quaternion.identity, scale);

    //CLEAR IT at the end of the object's OnGui()
    GUI.matrix = Matrix4x4.TRS(Vector3.zero, Quaternion.identity, Vector3.one);
     
    Last edited: Dec 3, 2019 at 4:59 AM