Reputation: 1240
I am making a GUI for my android game project on Unity using the UIToolkit. The Sample Scenes (eg Kitchen Sink) works fine on the Unity Editor. But when I run the exact same sample scenes on the android device , it doesnt work. The Button Texture is slightly shifted from the actual Touch Region. I have tried different Android Devices, but the result is the same.
I tried accessing the Sprite Locations using a function like
public float getYpos(this IPositionable sprite )
{
if(sprite == null)
return ;
float toRet = sprite.position.y;
return toRet;
}
But it the code returns an no Value!
I even tried using the "IPositionablePositioningExtensions" and returning the Sprite location. But no Success. Although I am not sure how to use the 'IPositionablePositioningExtensions', I have just started a beginner in this field.
My Button Creation Looks something like this :
playButton = UIButton.create("playUp.png", "playDown.png",0,0);
playButton.positionCenter();
playButton.scale = new Vector3(0.4f,0.4f,0);
Here is a screenshot of how the sample scene looks on an android device. The Bounding box is the region where the Touch Works, while the Button texture is being rendered somewhere else.
The Button texture should have been in the center position, although it moves to some other position while the touch region remains where it should have been.
Upvotes: 1
Views: 1994
Reputation: 504
From the guide on the develoeper's site: (...) Do not try to create your UI in Awake as the UI script may not be done initializing yet. Use Start instead.
edit: I thought you were creating ui in the awake method, but i tried myself and I have the same problem of yours even if I create it in the start method as suggested in the guide. Now I will test your solution. In fact If i change scene and reload the UI scene without restarting the application everything works right.
Upvotes: 0
Reputation: 1240
Found the solution. The onAwake method returns a different screen size than the actual screen size.
So added the following code to the uiCamera in ui.cs file
public static void setSize()
{
_uiCamera.orthographicSize = Screen.height / 2;
// Set the camera position based on the screenResolution/orientation
_uiCamera.transform.position = new Vector3( Screen.width / 2, -Screen.height / 2, -10.0f );
}
This is called once the screen has been set up.
Upvotes: 1