NGUI resizes fine in engine but not on actual devices

Hi guys,
I have a GUI, part of it is NGUI (round buttons and bars in the corners) and part is a rect() (the middle one).

What I want to do is resize the entire guy according to the screen size. So I multiply all the dimensions of NGUI bars and rect bar with Screen.width.

here’s my code for them:

  • for rect

     {
    
     	width = Screen.width * 0.5f;
     float currentWidth = width * _widthCoef * 0.715f;
     guiTexture.pixelInset = new Rect(-currentWidth * 0.5f, Screen.height * 0.5f - offsetY * Screen.height, currentWidth, height * Screen.height);
    

    }

  • for NGUI bars

     {
    
     	batteryBG.width = Mathf.FloorToInt(Screen.width * 0.25f);
     scoreBG.width = Mathf.FloorToInt(Screen.width * 0.25f);
    

    }

When I test in Unity in game window with different resolutions, it shows fine, here are the pictures:

But when I actually build the game on an iPad or iPhone the rect remains as it should (this means my code is correct), but the NGUI elements behave strangely. Here’s an approximate picture of how it looks when I build it on iPad

Any ideas of what might be causing this? At first I though that maybe screen.width is not the correct method and I need something like a resolution or pixel size but the rect works as expected so I guess this is a speciality of NGUI to react like this? What am I doing wrong?

Thanks in advance!

In ngui 3 layout with multiples resolutions are very easy to do using unified system without any code.

In ngui you never use Screen.width and height if you are using UIRoot to scale your GUI. If your code is using it, probably, it is the problema. The reason that it only happens on mobile is because your UIRoot was configured to FixedSizeOnMobile

You should be able to correct this by multiplying the screen width by the ratio between your fixed NGUI size and the screen size. Try using:

float width = Screen.width * ((float)uiRoot.activeHeight / Screen.height);

instead of Screen.width in your calculations (you’ll need a to set the uiRoot reference to the UIRoot object in your scene).