Screen.orientation 未根据需要在 Unity 中将 UI 元素位置从横向更改为纵向,但在重新启动游戏时会这样做

Screen.orientation not changing the UI elements placements as desired from Landscape to portrait in Unity on the fly but does on restarting the Game

我在使用 Screen.orientation 时遇到了一个相当奇怪的问题。

我正在开发数独游戏。我在设置中设计了一个切换单选选项,可以将屏幕方向从纵向更改为横向,副versa.I我能够实现这一点。

客户要求 UI 中的一些元素,如撤消按钮、擦除按钮、注释和数字键盘(请参考下面的屏幕截图)在我从纵向更改为横向时应更改位置.在更改设置中的选项时,屏幕可以从纵向更改为横向,但我根本无法更改 UI 元素的位置(尽管我已经编写了代码)。此外,当我关闭应用程序并重新启动它时,可以看到所需的位置。

我尝试 google 这个但是我找不到任何令人满意的结果,关于为什么在重新启动时它可以工作但在更改设置时它不能即时工作的部分。

因此在图片上方,而数独谜题可以在纵向模式下看到

在上图中,当我从纵向更改为横向时,屏幕会旋转,但各种 UI 元素(如数字键盘、撤消按钮等)不会根据需要更改其位置(这可以看到下图)

上图代表了我希望各种 UI 元素(如拼图、撤消按钮、擦除按钮、数字键盘等)缩放和重新定位的方式。这只有在我重新启动设备时才有可能。下面我将给出我用来实现相同目的的代码。

代码如下

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);
    }
}

嗯,整个问题是我试图在 MonoBehaviour 上工作。我将其更改为 UI 行为并且一切正常 actually.Here 是修改后的代码段

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);
    }
}