Unity 中的 UnityScript 到 C#

UnityScript to C# in Unity

我用 UnityScript 编写了这段代码。我需要用 C# 编写它。当我使用在线转换器时,似乎出现了编译器错误。有什么问题?

它将用于在 UI 文本中显示 3 秒的文本。我是Unity的新手,所以我可能没有解释好。

UnityScript代码:

private var timer: float;
private var showTime: float;
private var activeTimer: boolean;
private var message: String;
private var uiText: UI.Text;

function startTimer()
{
    timer = 0.0f;
    showTime = 3.0f;
    uiText.text = message;
    activeTimer = true;
}

function Start()
{
    uiText = GetComponent(UI.Text);
}

function Update()
{
    if (activeTimer)
    {
        timer += Time.deltaTime;

        if (timer > showTime)
        {
            activeTimer = false;
            uiText.text = "";
        }
    }
}

function showText(m: String)
{
    message = m;
    startTimer();
}

C# 转换器的输出似乎有问题:

private float timer;
private float showTime;
private bool  activeTimer;
private string message;
private UI.Text uiText;

void startTimer()
{
    timer = 0.0ff;
    showTime = 3.0ff;
    uiText.text = message;
    activeTimer = true;
}

void Start()
{
    uiText = GetComponent<UI.Text>();
}

void Update()
{
    if (activeTimer)
    {
        timer += Time.deltaTime;

        if (timer > showTime)
        {
            activeTimer = false;
            uiText.text = "";
        }
    }
}

void showText(string m)
{
    message = m;
    startTimer();
}

在 C# 脚本中,您必须从 MonoBehaviour 派生。下面的脚本将起作用 =)

using UnityEngine;
using UnityEngine.UI;

/// <summary>
/// Display a text for 3 seconds in UI Text.
/// </summary>
class DisplayText : MonoBehaviour
{
    private float timer;
    private float showTime;
    private bool activeTimer;
    private string message;
    private Text uiText;

    void Start()
    {
        uiText = GetComponent<Text>();
    }

    void Update()
    {
        if (activeTimer)
        {
            timer += Time.deltaTime;
            if (timer > showTime)
            {
                activeTimer = false;
                uiText.text = "";
            }
        }
    }

    void startTimer()
    {
        timer = 0.0f;
        showTime = 3.0f;
        uiText.text = message;
        activeTimer = true;
    }

    void showText(string m)
    {
        message = m;
        startTimer();
    }
}