using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using System.IO;
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();
Object[] SelectedAsset = Selection.GetFiltered(typeof(Object), SelectionMode.DeepAssets);
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)
{
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();
}
}