Search Unity

WorldToScreenPoint results in different results on iOS than UnityEditor

Discussion in 'Editor & General Support' started by RedVonix, Jan 6, 2014.

  1. RedVonix

    RedVonix

    Joined:
    Dec 13, 2011
    Posts:
    422
    NOTE: I've cross-posted this here and in the uWebKit forums at http://forum.unity3d.com/threads/105821-uWebKit-Web-Technology-for-Unity/page10 as I'm not sure if this is a uWebKit issue or a Unity issue.

    I'm working on a project where we are showing a uWebKit view on iOS. Being iOS, we of course have to use a WebGUI rather than a WebTexture, which leads to one major problem - position and size. The view is in a different position and size on iOS than it is in the Unity editor. To address this, I am attempting to position the GUI onto the same position as a 2D plane. This works wonderfully in the Editor, and moves and resizes with the plane in real time... however on iOS, it still is in the wrong position and size.

    Here is the code that I am using to convert from the world space to to screen space:

    Code (csharp):
    1.  
    2.     void Update()
    3.     {
    4.         // The Mesh Plane is used to position the Web GUI  
    5.         Bounds bounds = renderer.bounds;
    6.        
    7.         Vector3 origin = camera.WorldToScreenPoint(new Vector3(bounds.min.x, bounds.max.y, 0.0f));
    8.         Vector3 extents = camera.WorldToScreenPoint(new Vector3(bounds.max.x, bounds.min.y, 0.0f));    
    9.        
    10.         Rect newRect = new Rect(origin.x, Screen.height - origin.y, extents.x - origin.x, origin.y - extents.y);
    11.        
    12.         posX = (int)newRect.x;
    13.         posY = (int)newRect.y;
    14.         Width = (int)newRect.width;
    15.         Height = (int)newRect.height;
    16.     }
    17.  
    18.     void OnGUI ()
    19.     {
    20.         if(View != null)
    21.             View.OnWebGUI(posX, posY, Width, Height, 100f);
    22.     }
    23.  
    Again, in the Unity Editor, this works excellent. But load it up on iOS, and the WebGUI view is in the wrong position, and way too big.

    Any help would be very appreciated - thanks in advance!
     
  2. RedVonix

    RedVonix

    Joined:
    Dec 13, 2011
    Posts:
    422
    After doing some additional testing I can prove that this is absolutely a Unity issue, and not a uWebKit issue. So that leads me to 2 questions...

    1.) Does WorldToScreenPoint have known problems on iOS devices?
    2.) Is there a function that should be used in place of WorldToScreenPoint when working with iOS devices?
     
  3. RedVonix

    RedVonix

    Joined:
    Dec 13, 2011
    Posts:
    422
    Bump - haven't found a fix for this yet. Becoming very certain it's a Unity bug. Will be reporting it shortly to Unity, but could still use any advice, including hacks, to get around the problem.
     
  4. RedVonix

    RedVonix

    Joined:
    Dec 13, 2011
    Posts:
    422
    I've found a partial fix, but it's pretty ugly. I've determined that if, on iOS, I check if the device is widescreen or not, I can then do 2 different modifications to the positions supplied by WorldToScreenPoint to get it 'close' to the correct ones. They're still off, but it's closer than the erroneous points returned by WorldToScreenPoint.

    Latest code:

    Code (csharp):
    1.  
    2.         void Update()
    3.         {
    4.             // The Mesh Plane is used to position the Web GUI  
    5.             Bounds bounds = renderer.bounds;
    6.            
    7.             Vector3 origin = camera.WorldToScreenPoint(new Vector3(bounds.min.x, bounds.max.y, 0.0f));
    8.             Vector3 extents = camera.WorldToScreenPoint(new Vector3(bounds.max.x, bounds.min.y, 0.0f));    
    9.            
    10.        Rect newRect = new Rect();
    11.        if(isWidescreen())
    12.         newRect = new Rect(origin.x, Screen.height - origin.y, extents.x / 2, Screen.height - extents.y);
    13.        else
    14.         newRect = new Rect(origin.x / 2, Screen.height - origin.y, extents.x, Screen.height / 2  - extents.y / 2);
    15.            
    16.             posX = (int)newRect.x;
    17.             posY = (int)newRect.y;
    18.             Width = (int)newRect.width;
    19.             Height = (int)newRect.height;
    20.         }
    21.      
    22.         void OnGUI ()
    23.         {
    24.             if(View != null)
    25.                 View.OnWebGUI(posX, posY, Width, Height, 100f);
    26.         }
    27.  
    28.  
     
  5. trojanfoe_

    trojanfoe_

    Joined:
    May 20, 2014
    Posts:
    53
    I am having issues with WorldToScreenPoint on iOS devices being different to that of the Unity Editor. Did you find a solution to your issue?