UNITY之AssetBundle

本文介绍如何在Unity中实现资源的热更新,包括构建AssetBundles、使用脚本进行资源的下载与加载,并解决了中文路径加载的问题。

1:在Assets目录下新建两个文件夹>Editor,AssetBundles

2:在Editor中新建脚本

using UnityEngine;
using System.Collections;
using UnityEditor;
public class ImportAsset : MonoBehaviour {

    [@MenuItem("Test/BuildAssetBundles")]
    static void BuildAssetBundles(){
        BuildPipeline.BuildAssetBundles ("Assets/AssetBundles");
    }
    // Use this for initialization
    void Start () {
    }    
    // Update is called once per frame
    void Update () {
    }
}

3:设置预设体,材质,纹理,的AssetBundle

4:运行Test/BuildAssetBundles,则此AssetBundles文件夹里会有相应的AssetBundle文件

5:开启本地服务器:python -m SimpleHTTPServer 8080

6:能后将AssetBundles文件夹里的AssetBundle文件放到本地文稿里

7:从本地服务器上获取相应的预设体,材质,纹理

using UnityEngine;
using System.Collections;
public class DownLoadAsset : MonoBehaviour {
    public GameObject goCube;//掩饰修改纹理
    public GameObject goSphere;//展示修改材质
    public Transform newPos;//展示根据预设体创建对象

    //url=IP+文件名
    private string url1 = "http://127.0.0.1:8080/Documents/haha.assetbundle";
    private string url2 = "http://127.0.0.1:8080/Documents/hi.assetbundle";
    private string url3 = "http://127.0.0.1:8080/Documents/cube.assetbundle";
    // Use this for initialization
    void Start () {
    
        StartCoroutine (download_Texture(url1));
        StartCoroutine (download_Material(url2));
        StartCoroutine (download_prefab(url3));

    }
    // Update is called once per frame
    void Update () {
    }
    IEnumerator download_Texture(string url){
        WWW downAsset = new WWW (url);
        yield return downAsset;
        goCube.GetComponent<Renderer> ().material.mainTexture = (Texture)downAsset.assetBundle.LoadAsset ("haha");
        downAsset.assetBundle.Unload (false);
    }
    IEnumerator download_Material(string url){
        WWW downAsset = new WWW (url);
        yield return downAsset;
        goSphere.GetComponent<Renderer> ().material = (Material)downAsset.assetBundle.LoadAsset ("hi");
        downAsset.assetBundle.Unload (false);
    }
//    IEnumerator download_prefab(string url){
//        WWW downAsset = new WWW (url);
//        yield return downAsset;
//        GameObject obj = (GameObject)downAsset.assetBundle.LoadAsset ("Cube");
//        GameObject.Instantiate (obj,newPos.position,newPos.rotation);
//        downAsset.assetBundle.Unload (false);
//    }
    IEnumerator download_prefab(string url){
        WWW downloadAsset = new WWW (url);
        //同步加载资源
//        yield return downloadAsset;
//        GameObject gpPrefab = (GameObject)Instantiate (downloadAsset.assetBundle.LoadAsset("Cube"));
//        gpPrefab.GetComponent<Renderer> ().material.color = Color.red;
//        gpPrefab.transform.position = newPos.transform.position;
//        downloadAsset.assetBundle.Unload (false);
//        downloadAsset.Dispose ();

        //异步加载资源
        yield return downloadAsset;//直到downloadAsset下载完才会运行下面代码
        AssetBundle bundle = downloadAsset.assetBundle;
        AssetBundleRequest request = bundle.LoadAssetAsync ("Cube",typeof(GameObject));
        yield return request;
        GameObject gpPrefab = (GameObject)Instantiate (request.asset);
        gpPrefab.GetComponent<Renderer> ().material.color = Color.red;
        gpPrefab.transform.position = newPos.transform.position;
        downloadAsset.assetBundle.Unload (false);
        downloadAsset.Dispose ();
    }
}

8:从网上或file获取

using UnityEngine;
using System.Collections;
public class DownLoadScript : MonoBehaviour {
    public GameObject Cube;
    public GameObject Sphere;
    // Use this for initialization
    void Start () {
//        StartCoroutine (LoadByHttp());
//        StartCoroutine (LoadByFile());
//        StartCoroutine (HttpWwwForm());
    }
    
    // Update is called once per frame
    void Update () {
    }
    IEnumerator LoadByHttp(){
        WWW texture = new WWW ("http://pic36.nipic.com/20131117/5848817_155724710182_2.jpg");
        yield return texture;
        Sphere.GetComponent<MeshRenderer> ().material.mainTexture = texture.texture;
        texture.Dispose ();
    }
    IEnumerator LoadByFile(){
        WWW www = new WWW ("file:////Users/students/Desktop/NiHao.png");
        yield return 0;
        Cube.GetComponent<MeshRenderer> ().material.mainTexture = www.texture;
        www.Dispose ();
    }
    //向网络提交表单
    IEnumerator HttpWwwForm(){
        WWWForm wwwForm = new WWWForm ();
        wwwForm.AddField ("user_name","nihaokekeyi");
        wwwForm.AddField ("user_password","123456");
        //get
        //WWW www = new WWW ("http://172.16.10.2:8080/service/registeruser?user_name=nihaokekeyi&user_password=123456");
        //post
        WWW www = new WWW ("http://172.16.10.2:8080/service/registeruser",wwwForm);
        yield return www;
        print (www.text);

    }
}


因为FileStream是允许中文路径的,可以先通过文件流把AssetBundle读取到内存,在通过CreateFromMemory 创建AssetBundle资源,这样的话就避开了WWW不能加载中文路径的问题。

[csharp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. using UnityEngine;  
  2. using System.Collections;  
  3. using System.IO;  
  4. public class LoadAsset : MonoBehaviour {  
  5.     
  6.     AssetBundleCreateRequest asset;//定义一个资源包创建请求  
  7.  IEnumerator LoadAssetBundle()  
  8.     {  
  9.            
  10.         FileStream AssetIO = new FileStream(Application.dataPath + @"/StreamingAssets/好孩子/001.dat", FileMode.Open, FileAccess.ReadWrite); //创建文件流(对象现含有中文)      
  11.         byte[] assetbytes = new byte[AssetIO.Length];  
  12.         AssetIO.Read(assetbytes, 0, (int)AssetIO.Length);  
  13.         AssetIO.Close();  
  14.     
  15.         asset = AssetBundle.CreateFromMemory(assetbytes);//从内存中创建资源          
  16.         yield return asset;  
  17.            
  18.         AssetBundle LoadAsset = asset.assetBundle;//这样就能得到我们需要的资源包了  
  19.         if (asset.isDone)  
  20.           {  
  21.           Instantiate(LoadAsset.Load("Test_001"));        
  22.           }  
  23.     }  

内容概要:本文系统介绍了算术优化算法(AOA)的基本原理、核心思想及Python实现方法,并通过图像分割的实际案例展示了其应用价值。AOA是一种基于种群的元启发式算法,其核心思想来源于四则运算,利用乘除运算进行全局勘探,加减运算进行局部开发,通过数学优化器加速函数(MOA)和数学优化概率(MOP)动态控制搜索过程,在全局探索与局部开发之间实现平衡。文章详细解析了算法的初始化、勘探与开发阶段的更新策略,并提供了完整的Python代码实现,结合Rastrigin函数进行测试验证。进一步地,以Flask框架搭建前后端分离系统,将AOA应用于图像分割任务,展示了其在实际工程中的可行性与高效性。最后,通过收敛速度、寻优精度等指标评估算法性能,并提出自适应参数调整、模型优化和并行计算等改进策略。; 适合人群:具备一定Python编程基础和优化算法基础知识的高校学生、科研人员及工程技术人员,尤其适合从事人工智能、图像处理、智能优化等领域的从业者;; 使用场景及目标:①理解元启发式算法的设计思想与实现机制;②掌握AOA在函数优化、图像分割等实际问题中的建模与求解方法;③学习如何将优化算法集成到Web系统中实现工程化应用;④为算法性能评估与改进提供实践参考; 阅读建议:建议读者结合代码逐行调试,深入理解算法流程中MOA与MOP的作用机制,尝试在不同测试函数上运行算法以观察性能差异,并可进一步扩展图像分割模块,引入更复杂的预处理或后处理技术以提升分割效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值