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



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值