Unity代码打包资源

using UnityEngine;

using System.Collections;

using System.Collections.Generic;

using UnityEditor;

using System.IO;

public class ExportAssetBudle : MonoBehaviour

{

[MenuItem("Build/ExportScene")]

static void ExportScene()

{

string strCombinePath = Path.Combine(Application.dataPath, "ExoportSceneFile");

//读取外部配置文件获取场景名,这里暂时写死

string strPath = EditorUtility.SaveFilePanel("SaveResource",

strCombinePath, "ABTest", "unity3d");

if (0 != strPath.Length)

{

string[] scenes = { "Assets/Scene/ABTest.unity" };

BuildPipeline.BuildPlayer(scenes,

strPath, BuildTarget.StandaloneWindows64, BuildOptions.BuildAdditionalStreamedScenes);

}

}//function

[MenuItem("Build/ExportResource")]

static void ExportResource()

{

//打印版本号

Debug.LogError("BundleVersion:"+PlayerSettings.bundleVersion);

//打印包体标识码

Debug.LogError("BundleIdentifier:" + PlayerSettings.bundleIdentifier);

//强制清空本地缓存,防止版本号未改变,未改变看不到最新更改的效果

Caching.CleanCache();

//提前存储AsssetBundle(AB包)文件全路径

string strCombinePath = Path.Combine(Application.dataPath, "AssetBundle");

#region Unity编辑器设置AssetBundlenName后,引擎自动根据标记打包

//BuildPipeline.BuildAssetBundles(strCombinePath, BuildAssetBundleOptions.None, EditorUserBuildSettings.activeBuildTarget);//

//AssetDatabase.Refresh();

#endregion

//大家需要了解Selection类

//需要了解SelectionMode这个枚举的里每个枚举的意思

//Selection返回一个Objects数组

Object[] objectSelects = Selection.GetFiltered(typeof(Object),

SelectionMode.DeepAssets);

if (0<objectSelects.Length)

{

//构建AB包数据结构

List<AssetBundleBuild> tempAssetBundleBuild = new List<AssetBundleBuild>();

//清空tempAssetBundleBuild

tempAssetBundleBuild.Clear();

//for对刚才选中的文件夹里的所有Unity.Objetc对象进行包内容初始化

for(int i =0;i<objectSelects.Length;i++)

{

AssetBundleBuild[] assetBundleBuild = new AssetBundleBuild[1];

assetBundleBuild[0].assetBundleName = objectSelects[i].name+".assetbundle";

string strAssetPath = AssetDatabase.GetAssetPath(objectSelects[i]);

assetBundleBuild[0].assetNames = new string[] { strAssetPath};

tempAssetBundleBuild.Add(assetBundleBuild[0]);

}//for

#region 代码设置打包

BuildPipeline.BuildAssetBundles(strCombinePath, tempAssetBundleBuild.ToArray(),

BuildAssetBundleOptions.AppendHashToAssetBundleName, BuildTarget.StandaloneWindows64);

//刷线unity资源

AssetDatabase.Refresh();

#endregion

}//if

}//function

[MenuItem("Build/ExportABByCode")]

static void ExportAutoByCode()

{

Debug.LogError("<-------------BundleVersion:" + PlayerSettings.bundleVersion + "------------->");

Debug.LogError("<-------------BundleIdentifer:" + PlayerSettings.bundleIdentifier + "------------->");

Caching.CleanCache();

string strCombinePath = Path.Combine(Application.dataPath, "AssetBundle");

//SelectionMode.Deep(单个选中的Object)

//SelectionMode.DeepAssets(文件夹中所有的Object)

Object[] objectSelects = Selection.GetFiltered(typeof(Object),SelectionMode.DeepAssets);

string strFileName = "Version.txt";

string strFilePath = Application.streamingAssetsPath + "//";

if (File.Exists(strFilePath + strFileName))

File.Delete(strFilePath + strFileName);

FileStream fileStream = File.Create(strFilePath+strFileName);

StreamWriter streamWriter = new StreamWriter(fileStream);

if (0 < objectSelects.Length)

{

List<AssetBundleBuild> tempAssetBundleBuild = new List<AssetBundleBuild>();

tempAssetBundleBuild.Clear();

for (int i = 0; i < objectSelects.Length; i++)

{

AssetBundleBuild[] assetBundleBuild = new AssetBundleBuild[1];

assetBundleBuild[0].assetBundleName = objectSelects[i].name + ".assetbundle";

string strAssetPath = AssetDatabase.GetAssetPath(objectSelects[i]);

Debug.LogError(strAssetPath);

assetBundleBuild[0].assetNames = new string[] { strAssetPath };

//扩展名---标识(在原有扩展名上的标识,例如:obj.ab.ios)

//assetBundleBuild[0].assetBundleVariant = EditorUserBuildSettings.activeBuildTarget.ToString();//在ab上添加当前平台名字

//assetBundleBuild[0].assetBundleVariant = AssetDatabase.AssetPathToGUID(strAssetPath);//在AB上添加asset的GUID

tempAssetBundleBuild.Add(assetBundleBuild[0]);

Hash128 hash = AssetDatabase.GetAssetDependencyHash(strAssetPath);

if (false == hash.isValid)

continue;

streamWriter.WriteLine(AssetDatabase.GetAssetDependencyHash(strAssetPath) + "|" + strAssetPath);

}//for

streamWriter.Flush();

streamWriter.Close();

streamWriter.Dispose();

#region 代码设置打包

BuildPipeline.BuildAssetBundles(strCombinePath, tempAssetBundleBuild.ToArray(),

BuildAssetBundleOptions.UncompressedAssetBundle, EditorUserBuildSettings.activeBuildTarget);

AssetDatabase.Refresh();

#endregion

}//if

}//function

//批量设置Obejct的BundleName

[MenuItem("AutoSetBundleName/SetBundleName")]

static void AutoSetObjectAssetBundleName()

{

Object[] ObjectSelects = Selection.GetFiltered(typeof(Object), SelectionMode.DeepAssets);

if (null == ObjectSelects || ObjectSelects.Length < 1)

return;

for (int i = 0; i < ObjectSelects.Length; i++)

SetAssetBundleName(AssetDatabase.GetAssetPath(ObjectSelects[i]),

ObjectSelects[i].name + ".assetbundle", EditorUserBuildSettings.activeBuildTarget.ToString());

AssetDatabase.Refresh(ImportAssetOptions.ForceUpdate);

}//for

static void SetAssetBundleName(string path, string abName,string variantName)

{

AssetImporter importer = AssetImporter.GetAtPath(path);

if (abName != null)

{

if (importer.assetBundleName != abName)

importer.assetBundleName = abName;

//if (importer.assetBundleVariant != variantName)

// importer.assetBundleVariant = variantName;

}else

{

importer.assetBundleName = null;

}

}//function

}//class

猜你喜欢

转载自blog.csdn.net/zhangqiqi01/article/details/81223505