效果
代码如下:
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);
}
}