找不到类型或命名空间名称 'UI'。由 C# Unity3d 编写

Type or namespace name 'UI' could not be found. c# Unity3d

我是 c# 的新手,遇到这个问题已经有一段时间了,但我仍然没有运气。控制台说了一些事情,但**问题的主要要点是找不到 类型或命名空间名称 'UI'。

using System;
using UnityEngine;
using UnityEngine.UI;

namespace UnityStandardAssets.Utility
{
    [@RequireComponent(typeof(Text))]
    public class FPSCounter : MonoBehaviour
    {
        const float fpsMeasurePeriod = 0.5f;
        private int m_FpsAccumulator = 0;
        private float m_FpsNextPeriod = 0;
        private int m_CurrentFps;
        const string display = "{0} FPS";
        private Text m_UIText;


        private void Start()
        {
            m_FpsNextPeriod = Time.realtimeSinceStartup + fpsMeasurePeriod;
            m_UI.Text = GetComponent<UI.text>();
        }


        private void Update()
        {
            // measure average frames per second
            m_FpsAccumulator++;
            if (Time.realtimeSinceStartup > m_FpsNextPeriod)
            {
                m_CurrentFps = (int) (m_FpsAccumulator/fpsMeasurePeriod);
                m_FpsAccumulator = 0;
                m_FpsNextPeriod += fpsMeasurePeriod;
                m_UI.text.text = string.Format(display, m_CurrentFps);
            }
        }
    }
}

Errors From Console

当您添加 using UnityEngine.UI; 时,您是在告诉脚本您将要使用的名称空间,因此您无需担心使用 UI.Text,只需使用 Text

您还有一个变量 m_UIText,但是当您尝试在 Start 函数中分配它时,您将其称为 m_UI.Text

只需将行替换为 m_UIText = GetComponent<Text>();