Aset Bundleテスト


効果
AssetBundle测试_第1张图片
コードは以下の通りです
using UnityEngine;
using System.Collections;

public class LoadTest : MonoBehaviour {

    public GameObject man;
    public bool state = true;
    public string url = "http://192.168.2.105:8080/Test/AssetBundle/Cube.unity3d";
    GameObject cube;

	// Use this for initialization
	void Start () {
        man = GameObject.FindGameObjectWithTag("Player");
	}
	
	// Update is called once per frame
	void Update () {

        if (state ==true && Vector3.Distance(man.transform.position, transform.position) < 2f)
        {
           StartCoroutine( LoadCube());
           state = false;
        }


        if (cube!=null && Vector3.Distance(man.transform.position, cube.transform.position) > 2f)
        {
            Destroy(cube);
            state = true;
        }
	}

    IEnumerator LoadCube()
    {
        // Start a download of the given URL
        //          
        WWW www = WWW.LoadFromCacheOrDownload(url, 1);
        // Wait for download to complete
        //       
        yield return www;
        if (www.error != null)
        {

            Debug.Log(www.error);

            yield return null;

        }

        cube = Instantiate(www.assetBundle.mainAsset, transform.position, Quaternion.identity)as GameObject;
        www.assetBundle.Unload(true);
        
    }
}