Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Illogical Logic?

Discussion in 'Scripting' started by Omega, Oct 26, 2007.

  1. Omega

    Omega

    Joined:
    Jul 31, 2007
    Posts:
    125
    I have a script I've been working on to make GUI's a tad more manageable. After much tweaking and some thinking about how a GUI should behave, I have this script working nicely. The only problem is I seem to have gotten two variables crossed. Here's the weird part: it works. Furthermore, when I try to fix the "problem", everything breaks because the result is flipped. :?
    This makes no sense, but Unity seems to like it. Can anyone figure out what is going on? I'd appreciate it.
    Here's the script in queston:
    Code (csharp):
    1. //This is a script designed to make GUI's resolution-independent.
    2. //It takes a rect with the top and the left side in VIEWPORT coordinates,
    3. //and the height and the width in PIXELS
    4. //and converts it into a "proper" rect that is completely pixel-based.
    5. //
    6. //Syntax: scriptName.CalculateRect (input : Rect) : Rect
    7. //
    8.  
    9.  
    10. static function CalculateRect (windowSize : Rect)
    11. {
    12.     var topSide = windowSize.x;
    13.     var leftSide = windowSize.y;
    14.     var texWidth = windowSize.width;
    15.     var texHeight = windowSize.height;
    16.  
    17.    
    18.     var store = Camera.main.ViewportToScreenPoint (Vector3 (leftSide,topSide,0));
    19.     topSide = store.x - (texWidth/2);
    20.     leftSide = store.y - (texHeight/2);
    21.    
    22.     //store = Camera.main.ViewportToScreenPoint (Vector3 (texWidth,texHeight,0));
    23.     //texWidth = store.x;
    24.     //texHeight = store.y;
    25.    
    26.    
    27.     return Rect(topSide,leftSide,texWidth,texHeight);
    28. }
    29.  
    I actually modified this script from doing everything to only calculating the position because it was pulling the GUI apart with the scaling, hence the commented block. I still have a few touches to make (like adding a function(s) to change the anchor point from center), but everything else should be stable.
    Again, any help is appreciated.
     
  2. Ryuuguu

    Ryuuguu

    Joined:
    Apr 14, 2007
    Posts:
    391
    I don't see the solution off hand but working through the problem by setting up a test scene and running your code with some Debug.Log()statements will help you understand what is happening. I would do 4 test cases one at a time
    - long thin window acrosss the top
    - long thin window acrosss the bottom
    - long thin window acrosss the right
    - long thin window acrosss the left
    although not exciting when you finish you'll have a good understanding of what your trying to do.

    Code (csharp):
    1.  
    2. static function CalculateRect (windowSize : Rect)
    3. {
    4.    var topSide = windowSize.x;
    5.    var leftSide = windowSize.y;
    6.    var texWidth = windowSize.width;
    7.    var texHeight = windowSize.height;
    8.    
    9.    Debug.Log("------------"
    10.    Debug.Log(windowSize);
    11.    Debug.Log(Vector3 (leftSide,topSide,0));
    12.    
    13.    var store = Camera.main.ViewportToScreenPoint (Vector3 (leftSide,topSide,0));
    14.    Debug.Log(store);
    15.  
    16.    topSide = store.x - (texWidth/2);
    17.    leftSide = store.y - (texHeight/2);
    18.    Debug.Log(Rect(topSide,leftSide,texWidth,texHeight));
    19.    
    20.    return Rect(topSide,leftSide,texWidth,texHeight);
    21. }
    22.  
    (As someone who does Test Driven Developement I would rwrite the test before writing the code and automate them, but that is a whole 'nother religion :) )
     
  3. Omega

    Omega

    Joined:
    Jul 31, 2007
    Posts:
    125
    I think I found the problem. Thinking about what I put in vs. what comes out, I noticed something slightly out of place:
    Code (csharp):
    1. return Rect(topSide,leftSide,texWidth,texHeight);
    That's x, y, width, and height. Since when is x vertical?
    No... wait........ Hmmmmmm.....
    (I stopped writhing here to go look at the code and draw it out with the GIMP)
    After some fussing, it does look like I got the x/y thing crossed in the input. The script was flipping what was already flipped, so it ended up correct...
    I think...
    Well, I rearranged all of the variables into a more sensible arrangement.
    Now it works and makes sense :D
    For the records, here's the finished product (or the working product at least):
    Code (csharp):
    1. //This is a script designed to make GUI's resolution-independent.
    2. //It takes a rect with the left side and the top in VIEWPORT coordinates,
    3. //and the height and the width in PIXELS
    4. //and converts it into a "proper" rect that is completely pixel-based.
    5. //
    6. //Syntax: scriptName.CalculateRect (input : Rect) : Rect
    7. //
    8.  
    9.  
    10. static function CalculateRect (windowSize : Rect)
    11. {
    12.     var leftSide = windowSize.x;
    13.     var topSide = windowSize.y;
    14.     var texWidth = windowSize.width;
    15.     var texHeight = windowSize.height;
    16.  
    17.    
    18.     var store = Camera.main.ViewportToScreenPoint (Vector3 (leftSide,topSide,0));
    19.     leftSide = store.x - (texWidth/2);
    20.     topSide = store.y - (texHeight/2);
    21.    
    22.     //store = Camera.main.ViewportToScreenPoint (Vector3 (texWidth,texHeight,0));
    23.     //texWidth = store.x;
    24.     //texHeight = store.y;
    25.    
    26.    
    27.     return Rect(leftSide,topSide,texWidth,texHeight);
    28. }
    29.