Unity Resources资源打包以及加载(一)
using UnityEngine;
using System.Collections;
using UnityEditor;
using System.IO;
using System.Collections.Generic;
public class CreateAssetBundle : Editor
{
static void FindDependencies(string strPath , List strPathList)
{
if (strPathList.Contains(strPath))
{
return;
}
else
{
strPathList.Add(strPath);
string[] strArrayDependencies = AssetDatabase.GetDependencies(strPathList[strPathList.Count - 1]);
foreach (var m in strArrayDependencies)
{
FindDependencies(m, strPathList);
}
return;
}
}
[MenuItem("Custom Editor/Create AssetBunldes Main")]
static void CreateAssetBunldesMain()
{
WriteVersion();
Caching.CleanCache();
List strPathList = new List();
//获取在Project视图中选择的所有游戏对象
Object[] SelectedAsset = Selection.GetFiltered(typeof(Object), SelectionMode.DeepAssets);
//var game = Selection.gameObjects;
foreach (var n in SelectedAsset)
{
Debug.Log(AssetDatabase.GetAssetOrScenePath(n));
FindDependencies(AssetDatabase.GetAssetOrScenePath(n),strPathList);
}
string targetPath = Application.dataPath + "/StreamingAssets";
if (!Directory.Exists(targetPath))
{
Directory.CreateDirectory(targetPath);
}
AssetBundleBuild[] abBuilds = new AssetBundleBuild[6];
List strPrefab = new List();
List strModel = new List();
List strImage = new List();
List strShader = new List();
List strOther = new List();
List strMaterials = new List();
foreach (var p in strPathList)
{
//strArraty[i] = AssetDatabase.GetAssetOrScenePath(SelectedAsset[i]);
string strCase = "null";
if (p.Contains("."))
{
strCase = p.Split('.')[1];
}
switch (strCase.ToLower())
{
case "prefab":
case "cs":
strPrefab.Add(p);
break;
case "fbx":
strModel.Add(p);
break;
case "shader":
strShader.Add(p);
break;
case "mat":
strMaterials.Add(p);
break;
case "png":
case "jpg":
case "tga":
case "psd":
strImage.Add(p);
break;
default:
strOther.Add(p);
break;
}
}
abBuilds[0].assetNames = strPrefab.ToArray();
abBuilds[0].assetBundleName = "assetBundlePrefab";
abBuilds[1].assetNames = strModel.ToArray();
abBuilds[1].assetBundleName = "assetBundleModel";
abBuilds[2].assetNames = strImage.ToArray();
abBuilds[2].assetBundleName = "assetBundleImage";
abBuilds[3].assetNames = strShader.ToArray();
abBuilds[3].assetBundleName = "assetBundleShader";
abBuilds[4].assetNames = strMaterials.ToArray();
abBuilds[4].assetBundleName = "assetBundleMaterials";
abBuilds[5].assetNames = strOther.ToArray();
abBuilds[5].assetBundleName = "assetBundleOther";
BuildPipeline.BuildAssetBundles(targetPath, abBuilds, BuildAssetBundleOptions.CollectDependencies, BuildTarget.Android);
////刷新编辑器
AssetDatabase.Refresh();
}
static void WriteVersion()
{
StreamWriter sw = new StreamWriter(Application.streamingAssetsPath+"/version.txt");
string w = "1";
sw.Write(w);
sw.Close();
}