Unity3d动态生成和组合模型
WWW下载模型并且下载不同类型资源添加给模型:
using UnityEngine;
using System.Collections;
public class DownloadAssets : MonoBehaviour {
public RuntimeAnimatorController animator;
private string path = "http://127.0.0.1/res/role/npc_nanzhanglao/fbx/" + "npc_nanzhanglao.assetbundle";
private string animpath = "http://127.0.0.1/res/role/npc_nanzhanglao/animator/" + "npc_nanzhanglao.assetbundle";// Use this for initialization
void Start ()
{Caching.CleanCache();
StartCoroutine(load());
}
IEnumerator load()
{WWW www = WWW.LoadFromCacheOrDownload(path, 1);
yield return www;
if (www.isDone)
{
AssetBundle asset = www.assetBundle;
GameObject go = (GameObject)asset.mainAsset;
asset.Unload(false);Caching.CleanCache();StartCoroutine(loadAnimator(go));}}IEnumerator loadAnimator(GameObject go){WWW www = WWW.LoadFromCacheOrDownload(animpath, 1);
yield return www;
if (www.isDone)
{
//下载模型的动画资源,因为动画无法连带资源一起打包到模型上,所以动画需要单独打包(暂时只是Animator),但是动画单独打包的时候也需要连带资源打包,要不然动画没有作用。
//所以分开打包,另外,验证的时候,注意清除缓存(Caching.CleanCache(); )
//在资源添加模型之后实例化,要不然没有作用(对材质没有动画的貌似可以先实例化)
//该语法,也针对其他资源的格式转化。
RuntimeAnimatorController run = www.assetBundle.Load("npc_nanzhanglao", typeof(RuntimeAnimatorController)) as RuntimeAnimatorController;
Debug.Log("资源2:" + run.name);
go.GetComponent<Animator>().runtimeAnimatorController = run;
Instantiate(go);
www.assetBundle.Unload(false);
}
}
IEnumerator LoadAssetBundlesWithMaterial(string url, string materName, GameObject go)
{
WWW www = WWW.LoadFromCacheOrDownload(url, version);yield return www;if (www.isDone){
//这里可以先实例化,然后再添加材质资源
Debug.Log("开始把贴图资源贴载并且实例化到场景的游戏对象");Material mater = www.assetBundle.Load(materName, typeof(Material)) as Material;go.renderer.sharedMaterial = mater;www.assetBundle.Unload(false);
}
转载请注明:少狼 – 敬畏知识的顽皮狗 » Unity3d动态生成和组合模型
还没有人抢沙发呢~