將模型資源加載到場景中顯示
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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。