I am facing rather a strange problem while using Screen.orientation.
I am working on a Sudoku game .I have designed in the settings a toggle radio option to change the screen orientation from Portrait to LandScape and vice-versa.I am able to achieve this.
The client requirements is some elements in the UI such as the Undo button,erase button, notes and the number-pad(please refer screenshots below) should change the placements when I change from Portrait to LandScape. The screen is able to change from Portrait to LandScape on changing the option in the settings,on the fly,but I am unable to change the placement of the UI elements (though I have written the code)at all. Furthermore when I close the app and restart it, the desired placements can be seen.
I tried to google this but I couldnt find any satisfactory results with the part where as to why on restarting it is working but while changing in the settings it is not working on the fly.
So above the Pic while the Sudoku puzzle can be see in Portrait mode
Here in the above picture ,settings when I change from Portrait to LandScape the screen rotates but the various UI elements like the number pad,undo button,etc doesn't change their placement as desired (which can be seen the the Pic below)
The above pic represents the way I wanted the various UI elements like the puzzle,the UNdo button,the erase button, the number pad ,etc to scale and reposition. This is possible only when I restart the device. Below I will giving the code I am using to achieve the same.
The code is as follows
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine;
using UnityEngine.UI;
using YoYoGames.Sudoku;
public class OrientationManager : MonoBehaviour
{
public Toggle PortraitToggle, LandscapeToggle;
public GameObject PuzzleBoardObject;
public GameObject mistakesText;
public GameObject TimerText;
public GameObject modeText;
public GameObject ButtonsContainer;
private void Start()
{
if (Application.platform == RuntimePlatform.Android && DeviceDiagonalSizeInInches() < 6.5f)//mobile
{
if (!PlayerPrefs.HasKey("OrientationOptionToggle"))
PlayerPrefs.SetInt("OrientationOptionToggle", 0);
}
else
{ //tablet
if (!PlayerPrefs.HasKey("OrientationOptionToggle"))
PlayerPrefs.SetInt("OrientationOptionToggle", 1);
}
if (PlayerPrefs.GetInt("OrientationOptionToggle") == 0)
{
PortraitToggle.isOn = true;
LandscapeToggle.isOn = false;
Screen.orientation = ScreenOrientation.Portrait;
PortraitSetUp();
}
else
{
PortraitToggle.isOn = false;
LandscapeToggle.isOn = true;
Screen.orientation = ScreenOrientation.Landscape;
LandScapeSetUp();
}
}
public void PortraitToggleSelected()
{
PlayerPrefs.SetInt("OrientationOptionToggle", 0);
PortraitSetUp();
Screen.orientation = ScreenOrientation.Portrait;
}
public void LandscapeToggleSelected()
{
PlayerPrefs.SetInt("OrientationOptionToggle", 1);
LandScapeSetUp();
Screen.orientation = ScreenOrientation.Landscape;
}
public void PortraitSetUp()
{
if (PuzzleBoardObject != null)
{
SetGameObjectPosition(PuzzleBoardObject, 7f, 149f);
SetGameObjectSize(PuzzleBoardObject, 1f, 1f);
}
if (TimerText != null)
{
SetGameObjectPosition(TimerText, 135f, 19f);
SetGameObjectSize(TimerText, 1f, 1f);
}
if (mistakesText != null)
{
SetGameObjectPosition(mistakesText, 0f, 19f);
SetGameObjectSize(mistakesText, 1f, 1f);
}
if (modeText != null)
{
SetGameObjectPosition(modeText, -50f, 19f);
SetGameObjectSize(modeText, 1f, 1f);
}
if (ButtonsContainer != null)
{
SetGameObjectPosition(ButtonsContainer, 25f, 50f);
SetGameObjectSize(ButtonsContainer, 1f, 1f);
}
}
public void LandScapeSetUp()
{
if (PuzzleBoardObject != null)
{
SetGameObjectPosition(PuzzleBoardObject, -816f, -41f);
SetGameObjectSize(PuzzleBoardObject, 1.3f, 1.3f);
}
if (TimerText != null)
{
SetGameObjectPosition(TimerText, -785f, 19f);
SetGameObjectSize(TimerText, 2f, 2f);
}
if (mistakesText != null)
{
SetGameObjectPosition(mistakesText, -27f, 19f);
SetGameObjectSize(mistakesText, 2f, 2f);
}
if (modeText != null)
{
SetGameObjectPosition(modeText, 623f, 19f);
SetGameObjectSize(modeText, 2f, 2f);
}
if (ButtonsContainer != null)
{
SetGameObjectPosition(ButtonsContainer, 834f, 634f);
SetGameObjectSize(ButtonsContainer, 1.17f, 1.49f);
}
}
public static float DeviceDiagonalSizeInInches()
{
float screenWidth = Screen.width / Screen.dpi;
float screenHeight = Screen.height / Screen.dpi;
float diagonalInches = Mathf.Sqrt(Mathf.Pow(screenWidth, 2) + Mathf.Pow(screenHeight, 2));
return diagonalInches;
}
void SetGameObjectPosition(GameObject uiObject, float XPosition, float YPosition)
{
RectTransform uitransform = uiObject.GetComponent<RectTransform>();
uitransform.anchoredPosition = new Vector2(XPosition, YPosition);
}
void SetGameObjectSize(GameObject uiObject, float width, float height)
{
RectTransform uitransform = uiObject.GetComponent<RectTransform>();
uitransform.transform.localScale = new Vector3(width, height, 1f);
}
}
Well the whole problem was I was trying to work on MonoBehaviour. I changed it to UI behaviour and things worked out pretty decently actually.Here is the modified snippet
public class UiEvent : UIBehaviour
{
public Toggle PortraitToggle, LandscapeToggle;
public GameObject PuzzleBoardObject;
public GameObject mistakesText;
public GameObject TimerText;
public GameObject modeText;
public GameObject ButtonsContainer;
private void Start()
{
if (Application.platform == RuntimePlatform.Android && DeviceDiagonalSizeInInches() < 6.5f)//mobile
{
if (!PlayerPrefs.HasKey("OrientationOptionToggle"))
PlayerPrefs.SetInt("OrientationOptionToggle", 0);
}
else
{ //tablet
if (!PlayerPrefs.HasKey("OrientationOptionToggle"))
PlayerPrefs.SetInt("OrientationOptionToggle", 1);
}
if (PlayerPrefs.GetInt("OrientationOptionToggle") == 0)
{
PortraitToggle.isOn = true;
LandscapeToggle.isOn = false;
Screen.orientation = ScreenOrientation.Portrait;
PortraitSetUp();
}
else
{
PortraitToggle.isOn = false;
LandscapeToggle.isOn = true;
Screen.orientation = ScreenOrientation.Landscape;
LandScapeSetUp();
}
}
public void PortraitToggleSelected()
{
PlayerPrefs.SetInt("OrientationOptionToggle", 0);
Screen.orientation = ScreenOrientation.Portrait;
PortraitSetUp();
}
public void LandscapeToggleSelected()
{
PlayerPrefs.SetInt("OrientationOptionToggle", 1);
Screen.orientation = ScreenOrientation.Landscape;
LandScapeSetUp();
}
protected override void OnRectTransformDimensionsChange()
{
base.OnRectTransformDimensionsChange();
Debug.Log("On rect transform called.........");
}
public void PortraitSetUp()
{
if (PuzzleBoardObject != null)
{
SetGameObjectPosition(PuzzleBoardObject, 7f, 149f);
SetGameObjectSize(PuzzleBoardObject, 1f, 1f);
}
if (TimerText != null)
{
SetGameObjectPosition(TimerText, 135f, 19f);
SetGameObjectSize(TimerText, 1f, 1f);
}
if (mistakesText != null)
{
SetGameObjectPosition(mistakesText, 0f, 19f);
SetGameObjectSize(mistakesText, 1f, 1f);
}
if (modeText != null)
{
SetGameObjectPosition(modeText, -50f, 19f);
SetGameObjectSize(modeText, 1f, 1f);
}
if (ButtonsContainer != null)
{
SetGameObjectPosition(ButtonsContainer, 25f, 50f);
SetGameObjectSize(ButtonsContainer, 1f, 1f);
}
}
public void LandScapeSetUp()
{
if (PuzzleBoardObject != null)
{
SetGameObjectPosition(PuzzleBoardObject, -816f, -41f);
SetGameObjectSize(PuzzleBoardObject, 1.3f, 1.3f);
}
if (TimerText != null)
{
SetGameObjectPosition(TimerText, -785f, 19f);
SetGameObjectSize(TimerText, 2f, 2f);
}
if (mistakesText != null)
{
SetGameObjectPosition(mistakesText, -27f, 19f);
SetGameObjectSize(mistakesText, 2f, 2f);
}
if (modeText != null)
{
SetGameObjectPosition(modeText, 623f, 19f);
SetGameObjectSize(modeText, 2f, 2f);
}
if (ButtonsContainer != null)
{
SetGameObjectPosition(ButtonsContainer, 834f, 634f);
SetGameObjectSize(ButtonsContainer, 1.17f, 1.49f);
}
}
public static float DeviceDiagonalSizeInInches()
{
float screenWidth = Screen.width / Screen.dpi;
float screenHeight = Screen.height / Screen.dpi;
float diagonalInches = Mathf.Sqrt(Mathf.Pow(screenWidth, 2) + Mathf.Pow(screenHeight, 2));
return diagonalInches;
}
void SetGameObjectPosition(GameObject uiObject, float XPosition, float YPosition)
{
RectTransform uitransform = uiObject.GetComponent<RectTransform>();
uitransform.anchoredPosition = new Vector2(XPosition, YPosition);
}
void SetGameObjectSize(GameObject uiObject, float width, float height)
{
RectTransform uitransform = uiObject.GetComponent<RectTransform>();
uitransform.transform.localScale = new Vector3(width, height, 1f);
}
}