【发布时间】: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);
}
}
【问题讨论】: