Screen.orientation 不会根据需要将 UI 元素的位置从 Unity 中的横向变为纵向,但会在重新启动游戏时进行

问题描述

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

我正在开发一个数独游戏。我在设置中设计了一个切换单选选项,用于将屏幕方向从纵向更改为横向,反之亦然。我能够实现这一点。

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

我试图用谷歌搜索这个,但我找不到任何令人满意的结果,为什么重新启动它可以正常工作,但在更改设置时它不能即时工作。

Portrait

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

LandScape Undesired

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

LandScape Desired

上图代表了我希望各种 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);
        }


        if (mistakesText != null)
        {
            SetGameObjectPosition(mistakesText,0f,19f);
            SetGameobjectsize(mistakesText,1f);
        }


        if (modeText != null)
        {
            SetGameObjectPosition(modeText,-50f,19f);
            SetGameobjectsize(modeText,1f);
        }


        if (ButtonsContainer != null)
        {
            SetGameObjectPosition(ButtonsContainer,25f,50f);
            SetGameobjectsize(ButtonsContainer,1f);
        }

    }

    public void LandScapeSetUp()
    {
        if (PuzzleBoardobject != null)
        {
            SetGameObjectPosition(PuzzleBoardobject,-816f,-41f);
            SetGameobjectsize(PuzzleBoardobject,1.3f,1.3f);
        }


        if (TimerText != null)
        {
            SetGameObjectPosition(TimerText,-785f,2f,2f);
        }


        if (mistakesText != null)
        {
            SetGameObjectPosition(mistakesText,-27f,2f);
        }


        if (modeText != null)
        {
            SetGameObjectPosition(modeText,623f,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 行为,实际上结果相当不错。这是修改后的代码段

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


        if (mistakesText != null)
        {
            SetGameObjectPosition(mistakesText,0f,19f);
            SetGameObjectSize(mistakesText,1f);
        }


        if (modeText != null)
        {
            SetGameObjectPosition(modeText,-50f,19f);
            SetGameObjectSize(modeText,1f);
        }


        if (ButtonsContainer != null)
        {
            SetGameObjectPosition(ButtonsContainer,25f,50f);
            SetGameObjectSize(ButtonsContainer,1f);
        }

    }
  
    public void LandScapeSetUp()
    {
        if (PuzzleBoardObject != null)
        {
            SetGameObjectPosition(PuzzleBoardObject,-816f,-41f);
            SetGameObjectSize(PuzzleBoardObject,1.3f,1.3f);
        }


        if (TimerText != null)
        {
            SetGameObjectPosition(TimerText,-785f,2f,2f);
        }


        if (mistakesText != null)
        {
            SetGameObjectPosition(mistakesText,-27f,2f);
        }


        if (modeText != null)
        {
            SetGameObjectPosition(modeText,623f,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);
    }
}