加载模型资源

来源:互联网 发布:深圳 原画 培训 知乎 编辑:程序博客网 时间:2024/04/27 18:22


转自:http://blog.csdn.net/dingxiaowei2013/article/details/13615497


2.从服务器端加载

这里我已经将资源模型加载到百度云盘上,然后获取一个url下载地址:(直接模型就是ok.unity3d格式的)

中间是搭建的sqlserver服务器和asp.net服务器:

[csharp] view plaincopyprint?
  1. using UnityEngine;  
  2. using System.Collections;  
  3. using System.Text;  
  4.   
  5. public class NewBehaviourScript : MonoBehaviour  
  6. {  
  7.   
  8.     string s;  
  9.     private GameObject obj;  
  10.     private WWW www;  
  11.     private string url = @"http://192.168.1.6/plusFile/Test.aspx";  
  12.     private string url1 = @"http://114.92.247.6/xiaowei/ok.unity3d";  
  13.   
  14.     void Start()  
  15.     {  
  16.   
  17.     }  
  18.   
  19.     private bool isCompleted = false;  
  20.   
  21.     void Update()  
  22.     {  
  23.   
  24.         if (www == null)  
  25.         {  
  26.             return;  
  27.         }  
  28.         if (!isCompleted && www.isDone)  
  29.         {  
  30.             print("Download completed");  
  31.             isCompleted = true;  
  32.             print("6");  
  33.             obj = GameObject.Instantiate(www.assetBundle.mainAsset) as GameObject;  
  34.             obj.transform.position = new Vector3(0, 1, 20);  
  35.             print("7");  
  36.         }  
  37.   
  38.   
  39.     }  
  40.   
  41.     void OnGUI()  
  42.     {  
  43.         if (GUI.Button(new Rect(20, 20, 100, 40), "加载"))  
  44.         {  
  45.             StartCoroutine(getHtml(url));  
  46.             print("2");  
  47.         }  
  48.     }  
  49.   
  50.     IEnumerator getHtml(string url)  
  51.     {  
  52.         print("3");  
  53.         WWW web = new WWW(url);  
  54.         yield return web;  
  55.         print("1");  
  56.         //Encoding e1 = new ASCIIEncoding();    
  57.         //this.s = e1.GetString(web.bytes);    
  58.         //print("web" + e1.GetString(web.bytes));    
  59.         this.s = web.text;  
  60.         StartCoroutine(getModel(s));  
  61.     }  
  62.   
  63.   
  64.     IEnumerator getModel(string str)  
  65.     {  
  66.         print("s:" + str);  
  67.         this.www = new WWW(str);  
  68.         yield return www;  
  69.         print("5");  
  70.     }  
  71. }  


同样的效果:



直接从网上获取资源:

[csharp] view plaincopyprint?
  1. using UnityEngine;  
  2. using System.Collections;  
  3.   
  4. public class NewBehaviourScript : MonoBehaviour {  
  5.   
  6.     string url1 = @"http://114.92.247.6/xiaowei/ok.unity3d";  
  7.     GameObject obj;  
  8.     void Start () {  
  9.       
  10.     }  
  11.       
  12.     void Update () {  
  13.       
  14.     }  
  15.   
  16.     void OnGUI()  
  17.     {  
  18.         if (GUI.Button(new Rect(20, 20, 100, 40), "加载"))  
  19.         {  
  20.             StartCoroutine(getHtml(this.url1));  
  21.             print("2");  
  22.         }  
  23.     }  
  24.   
  25.     IEnumerator getHtml(string url)  
  26.     {  
  27.         print("3");  
  28.         WWW web = new WWW(url);  
  29.         yield return web;  
  30.         obj = Instantiate(web.assetBundle.mainAsset) as GameObject;  
  31.         obj.transform.position = new Vector3(0,1,20);  
  32.         print("1");  
  33.           
  34.     }  
  35. }  

0 0
原创粉丝点击