动态加载模型
1、 从 Resources 文件夹下加载prefab物体
GameObject obj;
//prefabname: 不需要后缀名
obj = (GameObject)Instantiate(Resources.Load(prefabname));
修改obj的参数
obj .transform.parent= obj .transform.position = 世界坐标
obj.name = id;
2、 动态加载对象,动态对象包放在exe同目录的Object文件夹下
GameObject obj; if (!prefabname.Contains(".unity3d")) prefabname = prefabname + ".unity3d"; string exepath = System.IO.Directory.GetCurrentDirectory(); string url = exepath + "/Object/" + prefabname;
DLAsset(url);
public IEnumerator DLAsset(string url)
{ WWW www = new WWW(url); yield return www; obj = (GameObject)Instantiate(www.assetBundle.mainAsset);
}
3、 动态加载对象,对象包在web服务器某目录下 string url; if (!modelName.Contains(".unity3d")) fullModName = modelName + ".unity3d"; if (Application.platform == RuntimePlatform.WindowsWebPlayer)
{ url = Application.dataPath + "/model/" + fullModName;
} // 静态加载对象,预制件必须在Resources文件夹下 else if (Application.platform == RuntimePlatform.WindowsEditor)
{ url = "file://" + Application.dataPath + "/" + fullModName; } else// 动态加载对象,动态对象包放在exe同目录的Object文件夹下
{ url = Application.dataPath + "/Object/" + fullModName;
} url = devinforservip+"/jsp/web/model/" + fullModName; WWW wwwgl = new WWW(url); yield return wwwgl; wwwObj.Add(wwwnub, wwwgl); try
{ objt = wwwgl.assetBundle.mainAsset; ModObj.Add(modelName, objt);
} catch
{ LogInfo = " 获取模型 " + url+" 出错,或者模型不存在!" ;
} obj = (GameObject)Instantiate(objt); obj.transform.position = new Vector3(x, y, z); obj.transform.localEulerAngles = new Vector3(rx, ry, 0); obj.name = deviceid; obj.transform.parent = GameObject.FindWithTag(parentid).transform;
CabDicHS .Add(deviceid, obj);