본문 바로가기
프로그래밍/Unity

Unirx - ObservableWWW.LoadFromCacheOrDownload

by neive 2016. 10. 14.
728x90

 


Unirx 로 에셋 번들 다운받기를 써봅시다

네이버 구글 다 뒤져도 없어서 직접 제작.. 이 내용에 대한 튜토리얼은 -_- 전세계 최초라고!


 

예제

 

#if !(UNITY_METRO || UNITY_WP8)

using UnityEngine;
using System.Collections;
using UniRx;

public class TestWWW : MonoBehaviour {

	void Start()
	{
		//StartCoroutine (DownLoadState ());
		StartCoroutine (DownLoadState_Unirx ());
	}

	// unirx 로 다운 받기
	IEnumerator DownLoadState_Unirx()
	{
		while (!Caching.ready)
			yield return null;

		ObservableWWW.LoadFromCacheOrDownload("https://dl.dropboxusercontent.com/u/238284954/Rem.unity3d", 0, null).Subscribe(x => NewObject(x.LoadAsset("TestRem")), ex => Debug.LogException(ex));
	}

	// 기본 다운 받기
	IEnumerator DownLoadState()
	{
		while (!Caching.ready)
			yield return null;

		WWW www = WWW.LoadFromCacheOrDownload ("https://dl.dropboxusercontent.com/u/238284954/Rem.unity3d", 0);

		yield return www;

		if (www.error != null)
			yield break;
		else
			NewObject (www.assetBundle.LoadAsset("TestRem"));
	}

	void NewObject(Object obj)
	{
		GameObject tmp = Instantiate (obj, Vector3.zero, Quaternion.identity) as GameObject;

		foreach(Transform t in tmp.GetComponentsInChildren<Transform>())
			if(t.gameObject.GetComponent<Renderer>() != null)
				t.gameObject.GetComponent<Renderer>().material.shader = Shader.Find(t.gameObject.GetComponent<Renderer>().material.shader.name);

		Debug.Log("obj name " + obj.name);
	}
}

#endif


728x90

댓글