简体   繁体   English

UnityScript 到 Unity 中的 C#

[英]UnityScript to C# in Unity

I have this code written in UnityScript.我有这个用 UnityScript 编写的代码。 I need to write it in C#.我需要把它写在 C# 中。 When I use an online converter there seems to be a compiler error.当我使用在线转换器时,似乎存在编译器错误。 What is the problem?问题是什么?

It is going to be used to display a text for 3 seconds in UI Text.它将用于在 UI 文本中显示 3 秒的文本。 I am new to Unity, so I might not have explained well.我是 Unity 的新手,所以我可能解释得不好。

UnityScript code:统一脚本代码:

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

The output from C# Converter that seems to have some problem:来自 C# 转换器的 output 似乎有一些问题:

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

In a C# script you have to derive from MonoBehaviour.在 C# 脚本中,您必须从 MonoBehaviour 派生。 The script below will work =)下面的脚本将起作用=)

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM