Unity C#打包AssetBundle與場(chǎng)景詳解
Unity2018已經(jīng)把打包過(guò)程簡(jiǎn)化很多了
我們只需要關(guān)心兩個(gè)API:
1.BuildPipline.BuildAssetBundles() 打包AssetBundle
2.BuildPipline.BuildPlayer() 打包場(chǎng)景
1.打包AssetBundle
先在資源的Inspector面板最下方 填寫資源所屬的AssetBundle名稱和后綴(后綴可以不填)
再利用BuildPipeline.BuildAssetBundles()進(jìn)行打包
2.打包Scene
利用BuildPipeline.BuildPlayer()進(jìn)行打包
為方便使用 先把要打包的場(chǎng)景放入指定的文件夾 通過(guò)腳本批量打包
3.腳本批量打包
4.打包完畢
5.加載測(cè)試
6.打包和測(cè)試腳本
AssetBundleBuilder.cs
using UnityEngine;
using UnityEditor;
using System.IO;
/// <summary>
/// 資源包打包工具
/// <para>打包AssetBundle和場(chǎng)景(Unity 2018.2.20)</para>
/// <para>ZhangYu 2019-02-26</para>
/// </summary>
public class AssetBundleBuilder
{
[MenuItem("打包/Windows/資源包和場(chǎng)景")]
public static void BuildAbsAndScenesWindows() {
BuildAbsAndScenes(BuildTarget.StandaloneWindows);
}
[MenuItem("打包/Android/資源包和場(chǎng)景")]
public static void BuildAbsAndScenesAndroid() {
BuildAbsAndScenes(BuildTarget.Android);
}
[MenuItem("打包/IOS/資源包和場(chǎng)景")]
public static void BuildAbsAndScenesIOS() {
BuildAbsAndScenes(BuildTarget.iOS);
}
[MenuItem("打包/Windows/資源包")]
public static void BuildAbsWindows() {
BuildAssetBundles(BuildTarget.StandaloneWindows);
}
[MenuItem("打包/Android/資源包")]
public static void BuildAbsAndroid() {
BuildAssetBundles(BuildTarget.Android);
}
[MenuItem("打包/IOS/資源包")]
public static void BuildAbsIOS() {
BuildAssetBundles(BuildTarget.iOS);
}
[MenuItem("打包/Windows/場(chǎng)景")]
public static void BuildScenesWindows() {
BuildScenes(BuildTarget.StandaloneWindows);
}
[MenuItem("打包/Android/場(chǎng)景")]
public static void BuildScenesAndroid() {
BuildScenes(BuildTarget.Android);
}
[MenuItem("打包/IOS/場(chǎng)景")]
public static void BuildScenesIOS() {
BuildScenes(BuildTarget.iOS);
}
// 打包AssetBundle和Scenes
public static void BuildAbsAndScenes(BuildTarget platform) {
BuildAssetBundles(platform);
BuildScenes(platform);
}
// 打包AssetBundles
private static void BuildAssetBundles(BuildTarget platform) {
// 輸出路徑
string outPath = Application.streamingAssetsPath + "/Abs";
if (!Directory.Exists(outPath)) Directory.CreateDirectory(outPath);
EditorUtility.DisplayProgressBar("信息", "打包資源包", 0f);
BuildPipeline.BuildAssetBundles(outPath, BuildAssetBundleOptions.DeterministicAssetBundle, platform);
AssetDatabase.Refresh();
Debug.Log("所有資源包打包完畢");
}
// 打包Scenes
private static void BuildScenes(BuildTarget platform) {
// 指定場(chǎng)景文件夾和輸出路徑
string scenePath = Application.dataPath + "/AbResources/Scenes";
string outPath = Application.streamingAssetsPath + "/Abs/";
if (Directory.Exists(scenePath)) {
// 創(chuàng)建輸出文件夾
if (!Directory.Exists(outPath)) Directory.CreateDirectory(outPath);
// 查找指定目錄下的場(chǎng)景文件
string[] scenes = GetAllFiles(scenePath, "*.unity");
for (int i = 0; i < scenes.Length; i++) {
string url = scenes[i].Replace("\\", "/");
int index = url.LastIndexOf("/");
string scene = url.Substring(index + 1, url.Length - index - 1);
string msg = string.Format("打包場(chǎng)景{0}", scene);
EditorUtility.DisplayProgressBar("信息", msg, 0f);
scene = scene.Replace(".unity", ".scene");
Debug.Log(string.Format("打包場(chǎng)景{0}到{1}", url, outPath + scene));
BuildPipeline.BuildPlayer(scenes, outPath + scene, BuildTarget.StandaloneWindows, BuildOptions.BuildAdditionalStreamedScenes);
AssetDatabase.Refresh();
}
EditorUtility.ClearProgressBar();
Debug.Log("所有場(chǎng)景打包完畢");
}
}
/// <summary> 獲取文件夾和子文件夾下所有指定類型文件 </summary>
private static string[] GetAllFiles(string directory, params string[] types) {
if (!Directory.Exists(directory)) return new string[0];
string searchTypes = (types == null || types.Length == 0) ? "*.*" : string.Join("|", types);
string[] names = Directory.GetFiles(directory, searchTypes, SearchOption.AllDirectories);
return names;
}
}
LoadTest.cs
using UnityEngine;
using UnityEngine.SceneManagement;
public class LoadTest : MonoBehaviour {
private void Start () {
LoadAB();
LoadScene();
}
// 加載資源包
private void LoadAB() {
// 資源包路徑
string path = Application.streamingAssetsPath + "/Abs/test.ab";
// WWW下載
Http http = gameObject.AddComponent<Http>();
http.get(path, OnLoadABComplete);
}
// 加載場(chǎng)景
private void LoadScene() {
// 資源包路徑
string path = Application.streamingAssetsPath + "/Abs/Test.scene";
// WWW下載
Http http = gameObject.AddComponent<Http>();
http.get(path, OnLoadSceneComplete);
}
// 加載AssetBundle完畢
private void OnLoadABComplete(WWW www) {
// 實(shí)例化預(yù)制
AssetBundle ab = www.assetBundle;
Object prefab = ab.LoadAsset("Test");
GameObject instance = (GameObject)Instantiate(prefab);
DontDestroyOnLoad(instance);
}
// 加載場(chǎng)景完畢
private void OnLoadSceneComplete(WWW www) {
// 必須寫www.assetBundle這句 這樣場(chǎng)景才能被讀取到
AssetBundle ab = www.assetBundle;
SceneManager.LoadScene("Test");
}
}
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問(wèn)大家可以留言交流,謝謝大家對(duì)我們的支持。
上一篇:Unity3D Shader實(shí)現(xiàn)貼圖切換效果
欄 目:C#教程
下一篇:c#滾動(dòng)字幕動(dòng)畫窗體制作步驟
本文標(biāo)題:Unity C#打包AssetBundle與場(chǎng)景詳解
本文地址:http://www.jygsgssxh.com/a1/C_jiaocheng/4851.html
您可能感興趣的文章
- 01-10Unity3d獲取系統(tǒng)時(shí)間
- 01-10Unity3D獲取當(dāng)前鍵盤按鍵及Unity3D鼠標(biāo)、鍵盤的基本操作
- 01-10Unity UGUI教程之實(shí)現(xiàn)滑頁(yè)效果
- 01-10C#使用Protocol Buffer(ProtoBuf)進(jìn)行Unity中的Socket通信
- 01-10C#在Unity游戲開(kāi)發(fā)中進(jìn)行多線程編程的方法
- 01-10VS2012 程序打包部署圖文詳解
- 01-10C#中Socket與Unity相結(jié)合示例代碼
- 01-10c#打包文件解壓縮的實(shí)例
- 01-10C#實(shí)現(xiàn)JSON解析器MojoUnityJson功能(簡(jiǎn)單且高效)
- 01-10Unity中C#和Java的相互調(diào)用實(shí)例代碼


閱讀排行
- 1C語(yǔ)言 while語(yǔ)句的用法詳解
- 2java 實(shí)現(xiàn)簡(jiǎn)單圣誕樹(shù)的示例代碼(圣誕
- 3利用C語(yǔ)言實(shí)現(xiàn)“百馬百擔(dān)”問(wèn)題方法
- 4C語(yǔ)言中計(jì)算正弦的相關(guān)函數(shù)總結(jié)
- 5c語(yǔ)言計(jì)算三角形面積代碼
- 6什么是 WSH(腳本宿主)的詳細(xì)解釋
- 7C++ 中隨機(jī)函數(shù)random函數(shù)的使用方法
- 8正則表達(dá)式匹配各種特殊字符
- 9C語(yǔ)言十進(jìn)制轉(zhuǎn)二進(jìn)制代碼實(shí)例
- 10C語(yǔ)言查找數(shù)組里數(shù)字重復(fù)次數(shù)的方法
本欄相關(guān)
- 01-10C#通過(guò)反射獲取當(dāng)前工程中所有窗體并
- 01-10關(guān)于ASP網(wǎng)頁(yè)無(wú)法打開(kāi)的解決方案
- 01-10WinForm限制窗體不能移到屏幕外的方法
- 01-10WinForm繪制圓角的方法
- 01-10C#實(shí)現(xiàn)txt定位指定行完整實(shí)例
- 01-10WinForm實(shí)現(xiàn)仿視頻播放器左下角滾動(dòng)新
- 01-10C#停止線程的方法
- 01-10C#實(shí)現(xiàn)清空回收站的方法
- 01-10C#通過(guò)重寫Panel改變邊框顏色與寬度的
- 01-10C#實(shí)現(xiàn)讀取注冊(cè)表監(jiān)控當(dāng)前操作系統(tǒng)已
隨機(jī)閱讀
- 01-11Mac OSX 打開(kāi)原生自帶讀寫NTFS功能(圖文
- 01-10使用C語(yǔ)言求解撲克牌的順子及n個(gè)骰子
- 08-05織夢(mèng)dedecms什么時(shí)候用欄目交叉功能?
- 08-05dedecms(織夢(mèng))副欄目數(shù)量限制代碼修改
- 01-10SublimeText編譯C開(kāi)發(fā)環(huán)境設(shè)置
- 01-10delphi制作wav文件的方法
- 01-11ajax實(shí)現(xiàn)頁(yè)面的局部加載
- 01-10C#中split用法實(shí)例總結(jié)
- 04-02jquery與jsp,用jquery
- 08-05DEDE織夢(mèng)data目錄下的sessions文件夾有什


