[unity3d]加载模型资源

将模型资源加载到场景中显示

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");              } } 



你可能感兴趣的:([unity3d]加载模型资源)