Last active
September 30, 2015 11:46
-
-
Save umiyuki/3f7e9ca836e69707c1dd to your computer and use it in GitHub Desktop.
Unity3D:非同期でリソース読み込みサンプル
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void sample() | |
{ | |
//テクスチャ読み込みの場合 | |
StartCoroutine(LoadAsyncAsset("texturePath", (object asset) => | |
{ | |
_MySprite.mainTexture = asset as Texture; | |
})); | |
//Prefabを読み込んでInstantiateの場合 | |
StartCoroutine(LoadAsyncAsset("prefabPath", (object asset) => | |
{ | |
GameObject obj = Instantiate(asset as GameObject); | |
})); | |
} | |
IEnumerator LoadAsyncAsset( string path, System.Action<object> callback ) | |
{ | |
ResourceRequest resReq = Resources.LoadAsync(path); | |
while (resReq.isDone == false) | |
{ | |
yield return 0; | |
} | |
if (callback != null) | |
{ | |
callback(resReq.asset); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment