unity3d 为什么GUI组件过时了?

rdrgkggo  于 2023-01-31  发布在  其他
关注(0)|答案(2)|浏览(462)

我目前正在Unity3D中的恐怖大厅的翻拍.我得到了代码,但我得到2错误:

Assets\Standard Assets\Utility\ForcedReset.cs(6,27): error CS0619: 'GUITexture' is obsolete: 'GUITexture has been removed. Use UI.Image instead.'
Assets\Standard Assets\Utility\SimpleActivatorMenu.cs(10,16): error CS0619: 'GUIText' is obsolete: 'GUIText has been removed. Use UI.Text instead.'

我尝试用错误中提供给我的项目替换它们,但仍然收到错误消息,说名称空间名称UI未找到。我仍然不习惯Unity,因此需要一些帮助。
下面是ForcedReset.cs的代码:

using System;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityStandardAssets.CrossPlatformInput;

[RequireComponent(typeof (GUITexture))]
public class ForcedReset : MonoBehaviour
{
    private void Update()
    {
        // if we have forced a reset ...
        if (CrossPlatformInputManager.GetButtonDown("ResetObject"))
        {
            //... reload the scene
            SceneManager.LoadScene(SceneManager.GetSceneAt(0).name);
        }
    }
}

下面是SimpleActivatorMenu.cs的代码:

using System;
using UnityEngine;

namespace UnityStandardAssets.Utility
{
    public class SimpleActivatorMenu : MonoBehaviour
    {
        // An incredibly simple menu which, when given references
        // to gameobjects in the scene
        public GUIText camSwitchButton;
        public GameObject[] objects;

        private int m_CurrentActiveObject;

        private void OnEnable()
        {
            // active object starts from first in array
            m_CurrentActiveObject = 0;
            camSwitchButton.text = objects[m_CurrentActiveObject].name;
        }

        public void NextCamera()
        {
            int nextactiveobject = m_CurrentActiveObject + 1 >= objects.Length ? 0 : m_CurrentActiveObject + 1;

            for (int i = 0; i < objects.Length; i++)
            {
                objects[i].SetActive(i == nextactiveobject);
            }

            m_CurrentActiveObject = nextactiveobject;
            camSwitchButton.text = objects[m_CurrentActiveObject].name;
        }
    }
}

有什么问题吗?

x6yk4ghg

x6yk4ghg1#

GUIText和其他元素已经从Unity的新版本中删除,原因是可合并性和其他一些问题!

    • 修复GUI文本**

确保在代码中导入以下语句行:-

using UnityEngine.UI;

然后,只需从

public GUIText camSwitchButton;

public Text camSwitchButton;
    • 已修复GUI纹理**

这个修复给了我一些关于其他文件的警告,但是工作正常!它可能会在即将到来的Unity更新中修复!
确保在代码中导入以下语句行:-

using UnityEngine.UI;

然后,从此代码行进行更改

[RequireComponent(typeof (GUITexture))]

到,

[RequireComponent(typeof(Texture))]

希望这有帮助!我已经检查过了,它工作正常,但只是给了我一些警告,我不在乎!
但它会很好,并将在一些即将到来的版本的统一修复!

ryevplcw

ryevplcw2#

固定

1.对于ForcedReset.cs文件,使用以下代码更改其内容:

using System;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
using UnityStandardAssets.CrossPlatformInput;

[RequireComponent(typeof(Texture))]
public class ForcedReset : MonoBehaviour
{
    private void Update()
    {
        // if we have forced a reset ...
        if (CrossPlatformInputManager.GetButtonDown("ResetObject"))
        {
            //... reload the scene
            SceneManager.LoadScene(SceneManager.GetSceneAt(0).name);
        }
    }
}

1.对于SimpleActivatorMenu.cs文件,使用以下命令更改其内容:

using System;
using UnityEngine;
using UnityEngine.UI;

namespace UnityStandardAssets.Utility
{
    public class SimpleActivatorMenu : MonoBehaviour
    {
        // An incredibly simple menu which, when given references
        // to gameobjects in the scene
        public Text camSwitchButton;
        public GameObject[] objects;

        private int m_CurrentActiveObject;

        private void OnEnable()
        {
            // active object starts from first in array
            m_CurrentActiveObject = 0;
            camSwitchButton.text = objects[m_CurrentActiveObject].name;
        }

        public void NextCamera()
        {
            int nextactiveobject = m_CurrentActiveObject + 1 >= objects.Length ? 0 : m_CurrentActiveObject + 1;

            for (int i = 0; i < objects.Length; i++)
            {
                objects[i].SetActive(i == nextactiveobject);
            }

            m_CurrentActiveObject = nextactiveobject;
            camSwitchButton.text = objects[m_CurrentActiveObject].name;
        }
    }
}

相关问题