将模型资源加载到场景中显示
1.从本地加载
void OnGUI() { if (GUILayout.Button("click")) { StartCoroutine(getmodel()); } } IEnumerator getmodel() { WWW w = new WWW("file://"+Application.dataPath+"/ok.unity3d"); yield return w; Instantiate(w.assetBundle.mainAsset); }
2.从服务器端加载
这里我已经将资源模型加载到百度云盘上,然后获取一个url下载地址:(直接模型就是ok.unity3d格式的)
中间是搭建的sqlserver服务器和asp.net服务器:
using UnityEngine; using System.Collections; using System.Text; public class NewBehaviourScript : MonoBehaviour { string s; private GameObject obj; private WWW www; private string url = @"http://192.168.1.6/plusFile/Test.aspx"; private string url1 = @"http://114.92.247.6/xiaowei/ok.unity3d"; void Start() { } private bool isCompleted = false; void Update() { if (www == null) { return; } if (!isCompleted && www.isDone) { print("Download completed"); isCompleted = true; print("6"); obj = GameObject.Instantiate(www.assetBundle.mainAsset) as GameObject; obj.transform.position = new Vector3(0, 1, 20); print("7"); } } void OnGUI() { if (GUI.Button(new Rect(20, 20, 100, 40), "加载")) { StartCoroutine(getHtml(url)); print("2"); } } IEnumerator getHtml(string url) { print("3"); WWW web = new WWW(url); yield return web; print("1"); //Encoding e1 = new ASCIIEncoding(); //this.s = e1.GetString(web.bytes); //print("web" + e1.GetString(web.bytes)); this.s = web.text; StartCoroutine(getModel(s)); } IEnumerator getModel(string str) { print("s:" + str); this.www = new WWW(str); yield return www; print("5"); } }
同样的效果:
直接从网上获取资源:
using UnityEngine; using System.Collections; public class NewBehaviourScript : MonoBehaviour { string url1 = @"http://114.92.247.6/xiaowei/ok.unity3d"; GameObject obj; void Start () { } void Update () { } void OnGUI() { if (GUI.Button(new Rect(20, 20, 100, 40), "加载")) { StartCoroutine(getHtml(this.url1)); print("2"); } } IEnumerator getHtml(string url) { print("3"); WWW web = new WWW(url); yield return web; obj = Instantiate(web.assetBundle.mainAsset) as GameObject; obj.transform.position = new Vector3(0,1,20); print("1"); } }
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。