加载模型资源


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


2.从服务器端加载

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

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

  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. }  


同样的效果:



直接从网上获取资源:

  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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值