工作遇到需要等StartCoroutine 跑完後 取得變數,如截圖後取得圖檔路徑等

方法如下:

1、將要存取的變數存在static 變數中

2、於IEnumerator 方法,傳入一個 delegate 當執行完時呼叫,執行IEnumerator 方法時 修改 static 變數

3、於呼叫 delegate 方法時 取出static 變數

 

範例如下:

 


public class ScreenSystem {


    public static string SHOTPATH = "";
    public delegate void callback();

    public static IEnumerator GetScreenShot(string ShotName, bool UseCompress, callback CallBack) {


    yield return new WaitForEndOfFrame();
    ...
   SHOTPATH = EditorShareWithScreenshot(tex.EncodeToPNG(), ShotName);

        if (CallBack != null) { 
            CallBack();
        }
    }

    private static string EditorShareWithScreenshot(byte[] ScreenShot, string ShotName) {
    ...
    return filePath;
    }
}

呼叫範例
void returnCallBack() {
    Debug.Log("returnCallBack");
    Debug.Log("path:"+KTool.Image.ScreenSystem.SHOTPATH);
}

void TestScreenShot() {
    KTool.Image.ScreenSystem.SDPROJECT = "test";
    KTool.Image.ScreenSystem.SDSCREENSHOTDIR = "Shots";
    Debug.Log("path:"+KTool.Image.ScreenSystem.SHOTPATH);


    StartCoroutine(KTool.Image.ScreenSystem.GetScreenShot("1111", false, returnCallBack));
}



arrow
arrow

    狼翔月影 發表在 痞客邦 留言(0) 人氣()