Skip to content

Instantly share code, notes, and snippets.

@umiyuki
Last active September 30, 2015 11:46
Show Gist options
  • Save umiyuki/3f7e9ca836e69707c1dd to your computer and use it in GitHub Desktop.
Save umiyuki/3f7e9ca836e69707c1dd to your computer and use it in GitHub Desktop.
Unity3D:非同期でリソース読み込みサンプル
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