unityassetbundle 打包存放百度云加载

本文介绍了一个使用Unity进行AssetBundle资源打包的方法,并提供了一个简单的AssetBundle加载示例。通过提供的C#脚本,可以实现对Unity资源的AssetBundle格式打包,支持WebPlayer和Android平台,并展示了如何在运行时加载这些AssetBundles。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >



打包assetbundle程序 



   using UnityEngine;  
    using System.Collections;  
    using UnityEditor;  
      
    public class AssetBundleTest : Editor  
    {  
      
        [MenuItem("Custom Editor/WebPlayer")]  
        static void WebPlayer()  
        {  
            Object[] SelectedAsset = Selection.GetFiltered(typeof(Object), SelectionMode.DeepAssets);  
            foreach (Object obj in SelectedAsset)  
            {  
                string targetPath = Application.dataPath + "/Asset/" + obj.name + ".assetbundle";  
                if (BuildPipeline.BuildAssetBundle(obj, null, targetPath, BuildAssetBundleOptions.CollectDependencies, BuildTarget.WebPlayer))  
                {  
                    Debug.Log(obj.name + "资源打包成功");  
                }  
                else  
                {  
                    Debug.Log(obj.name + "资源打包失败");  
                }  
            }  
            //刷新编辑器  
            AssetDatabase.Refresh();  
        }  
      
        [MenuItem("Custom Editor/Android")]  
        static void Android()  
        {  
            Object[] SelectedAsset = Selection.GetFiltered(typeof(Object), SelectionMode.DeepAssets);  
            foreach (Object obj in SelectedAsset)  
            {  
                string targetPath = Application.dataPath + "/Asset/" + obj.name + ".assetbundle";  
                if (BuildPipeline.BuildAssetBundle(obj, null, targetPath, BuildAssetBundleOptions.CollectDependencies, BuildTarget.Android))  
                {  
                    Debug.Log(obj.name + "资源打包成功");  
                }  
                else  
                {  
                    Debug.Log(obj.name + "资源打包失败");  
                }  
            }  
            //刷新编辑器  
            AssetDatabase.Refresh();  
        }  
      
         
    }  








加载程序

using UnityEngine; 
using System.Collections; 
   
   
public class loadModel : MonoBehaviour 
   
        publicstringRUL = null
        //"file://F:\\1.unity3d"; 
         WWW www; 
        boolisDownLoad =false
        publicGameObject fatherObject; 
        voidStart() 
        
        StartCoroutine(LoadAssetbundle(RUL)); 
        
         IEnumerator LoadAssetbundle(stringurl)  
         
         // Start a download of the given URL 
        // 开始从指定路径下载 
         www = WWW.LoadFromCacheOrDownload(url, 1); 
        // Wait for download to complete 
        // 等待下载完成 
        yieldreturnwww; 
         if(!www.isDone) 
         
         print("123"); 
         
        if(www.error !=null
        
        Debug.Log (www.error); 
   
        yieldreturnnull
        
// Instantiate([url]www.assetBundle.mainAsset[/url]); 
 GameObject gameObj = GameObject.Instantiate([url]www.assetBundle.mainAsset[/url], transform.position, Quaternion.identity)asGameObject; 
 gameObj.transform.parent = fatherObject.transform; 
 
 voidUpdate()  
 
 if(!www.isDone) 
 
 isDownLoad =false
 
 else 
 
 isDownLoad =true
 
   
   
 
 voidOnGUI() 
 
 if(!isDownLoad) 
 
 GUI.Label(newRect(720f / 1627.0f * Screen.width, 300f / 915.0f * Screen.height, 200f / 1627.0f * Screen.width, 50f / 915.0f * Screen.height), "正在下载请等待); 
 
   
 
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值