[unity3d]加载模型资源

简介: 将模型资源加载到场景中显示 1.从本地加载 void OnGUI() { if (GUILayout.Button("click")) { StartCoroutine(getmodel(...

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

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



相关文章
|
4月前
|
算法 图形学 UED
【Unity 3D】AssetBundle工作流程、打包策略详解(超详细必看)
【Unity 3D】AssetBundle工作流程、打包策略详解(超详细必看)
67 0
|
图形学 索引
|
存储 缓存 API
5.0版本之后的AssetBundle资源的打包和解析加载(Unity3D)
这几天在研究AssetBundle资源打包盒解析加载,也踩过很多坑,参考过很多人的文章 发现很多人关于AssetBundle的文章不是API过时了不能用,就是有点乱 也不是有点乱,就是摸不着头脑,让人不能快速的get到这个东西如何使用 所以我特意在踩过坑之后把我这个学到的经验分享给大家。
|
存储 缓存
Flutter中更快地加载您的图像资源
本文主要介绍在Flutter中更快地加载您的图像资源 我们可以将图像放在我们的资产文件夹中,但如何更快地加载它们?这是 Flutter 中的一个秘密函数,可以帮助我们做到这一点 — precacheImage()
127 0
|
缓存
在Flutter中更快地加载您的图像资源
我们可以将图像放在我们的资产文件夹中,但如何更快地加载它们?这是 Flutter 中的一个秘密函数,可以帮助我们做到这一点 — precacheImage()
177 0
在Flutter中更快地加载您的图像资源
|
图形学 UED
Unity打包/读取AssetBundle资源全教程
Unity 资源AssetBundle打包 本文提供全流程,中文翻译。 Chinar 坚持将简单的生活方式,带给世人!(拥有更好的阅读体验 —— 高分辨率用户请根据需求调整网页缩放比例) Chi...
2140 0
|
测试技术 图形学
Unity读取AssetBundle资源全教程(所有读取方式)
读取/加载 AssetBundle 资源的多种方式 本文提供全流程,中文翻译。 Chinar 坚持将简单的生活方式,带给世人!(拥有更好的阅读体验 —— 高分辨率用户请根据需求调整网页缩放比例) ...
2281 0
|
测试技术 图形学 Android开发

相关实验场景

更多