【问题标题】:Having both IEnumerator Start() and void Start() in a single script在一个脚本中同时拥有 IEnumerator Start() 和 void Start()
【发布时间】:2017-10-05 14:46:13
【问题描述】:

我有一个来自 Unity 文档页面的示例程序,其中包含一个 IEnumerator Start(),如下所示,但我想知道如何在同一个脚本中也有一个普通的 void Start()

我也尝试添加void Start(),但它引发了错误。然后,我尝试在IEnumerator 函数中包含我的代码(它只是写入控制台应用程序的数据路径),尽管使用0f 作为延迟参数立即执行它,但它不会打印出任何东西......

我错过了什么?对于必须有IEnumerator Start() 但还需要执行起始代码的这种情况,通常的解决方案是什么?

/// Saves screenshots as PNG files.
public class PNGers : MonoBehaviour
{

    // Take a shot immediately.
    IEnumerator Start()
    {
        yield return UploadPNG();
        yield return ConsoleMSG();
    }

    IEnumerator UploadPNG()
    {
        // We should only read the screen buffer after frame rendering is complete.
        yield return new WaitForEndOfFrame();

        // Create a texture the size of the screen, with RGB24 format.
        int width = Screen.width;
        int height = Screen.height;
        Texture2D tex = new Texture2D(width, height, TextureFormat.RGB24, false);

        // Read the screen contents into the texture.
        tex.ReadPixels( new Rect(0, 0, width, height), 0, 0 );
        tex.Apply();

        // Encode the texture into PNG format.
        byte[] bytes = tex.EncodeToPNG();
        Object.Destroy(tex);

        // For testing purposes, also write to a file in the project folder:
        File.WriteAllBytes(Application.dataPath + "/../SavedScreen.png", bytes);

    }

    IEnumerator ConsoleMSG()
    {
        yield return new WaitForSeconds(0f);
        Debug.Log(Application.dataPath);
    }
}

【问题讨论】:

    标签: c# unity3d


    【解决方案1】:

    我有一个来自 Unity 文档页面的示例程序,其中包含 一个 IEnumerator Start() 如下所示,但我想知道我怎么能有 一个普通的 void Start() 在同一个脚本中?

    不能

    这是因为你不能有两个同名的函数。例外情况是函数具有不同的参数类型。我知道一个Start 函数是void 返回类型,而另一个是IEnumerator 返回类型。这在 C# 中并不重要。重要的是两个函数的参数。

    在这种情况下,它们都接受任何参数,因此您不能重载它们。您可以阅读有关此here 的更多信息。

    即使你让void Start 函数带参数,而IEnumerator Start 函数不带参数,它也不起作用。例如,

    void Start(int i)
    {
        Debug.Log("Hello Log 1");
    }
    

    IEnumerator Start()
    {
        yield return null;
        Debug.Log("Hello Log 2");
    }
    

    Unity 会同时抛出编译(编辑器)和运行时异常:

    Script error (<ScriptName>): Start() can not take parameters.


    如果你切换它并让void Start 没有任何参数但IEnumerator 有一个参数,它将编译而你不会得到任何错误,但在您运行/玩游戏时不会调用 IEnumerator Start 函数。

    void Start()
    {
        Debug.Log("Hello Log 1");
    }
    

    IEnumerator Start(int i)
    {
        yield return null;
        Debug.Log("Hello Log 2");
    }
    

    对于这种情况,您必须拥有的通常解决方案是什么? 一个 IEnumerator Start() 但你还需要执行起始代码?

    在任何其他代码之前在IEnumerator Start 函数中运行您的起始代码。

    IEnumerator Start()
    {
        //Run your Starting code
        startingCode();
    
        //Run Other coroutine functions
        yield return UploadPNG();
        yield return ConsoleMSG();
    }
    
    IEnumerator UploadPNG()
    {
    
    }
    
    IEnumerator ConsoleMSG()
    {
        yield return new WaitForSeconds(0f);
    }
    
    void startingCode()
    {
        //Yourstarting code
    }
    

    您也可以在void Awake()void Enable()函数中执行起始代码。

    【讨论】:

    • 非常感谢您提供如此详细和翔实的答案。
    猜你喜欢
    • 2017-10-27
    • 1970-01-01
    • 2018-11-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多