unity自动设置Assetbundle包名
2017-09-05 09:27
323 查看
因为是针对自己项目写的,所以有些地方需要读者自己修改
上代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using System.IO;
public class CreateAllPrefab : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
[MenuItem("AssetBundle/Create All Prefab")]
static void CreateAllPrefabr()
{
if (EditorUtility.DisplayDialog("包名", "将会把Resources/AB/FBX目录下的fbx文件制成prefab并添加ab包名,包名即是名字是否确认?", "确定"))
{
DirectoryInfo raw = new DirectoryInfo(Application.dataPath + "/Resources/AB/FBX/");
//清除多余的ab名
ClearAssetBundlesName();
//为其返回子目录文件夹名称的数组的路径
foreach (DirectoryInfo dictorys in raw.GetDirectories())
{
string name = @dictorys.ToString().Replace(raw.ToString().Replace("/", @"\"), "");
//检查是否存在prefab
if (!File.Exists(dictorys.ToString() + @"\" + name + ".prefab"))
{
BuildPrefab(dictorys, name);
}
DirectoryInfo dif = new DirectoryInfo(dictorys + @"\");
FileInfo[] fileinfo = dif.GetFiles();
foreach (FileInfo finf in fileinfo)
{
if (finf.Extension.Equals(".prefab"))
{
//ab包名
string AbName = name.Substring(0, name.IndexOf("_")) + "/" + name;
//文件路径
string _assetPath = "Assets" + finf.ToString().Replace(@"\", "/").Substring(Application.dataPath.Length);
AssetImporter assetImporter = AssetImporter.GetAtPath(_assetPath);
if (assetImporter != null)
{
assetImporter.assetBundleName = AbName;
}
else
{
Debug.LogError(AbName + ".prefab 不存在,请确认是否存在该预制");
}
break;
}
}
}
Debug.Log("设置完成");
}
}
/// <summary>
/// 制作预制并且添加换装需要的资源
/// </summary>
/// <param name="dictorys"></param>
/// <param name="name"></param>
static void BuildPrefab(DirectoryInfo dictorys, string name)
{
//文件路径
string path = dictorys.ToString().Replace(@"\", "/").Substring(Application.dataPath.Length).Replace("/Resources/", "");
GameObject go = Resources.Load(path + "/" + name) as GameObject;
if (go != null)
{
go.name = name;
}
else
{
Debug.Log("资源名和文件夹名不对应,加载失败");
return;
}
Texture[] m_texture = Resources.LoadAll<Texture>(path);
List<Material> m_Material = new List<Material>();
//生成材质球
for (int j = 0; j < m_texture.Length - 1; j++)
{
Material mater = new Material(Shader.Find("Standard"));
mater.mainTexture = m_texture[j];
m_Material.Add(mater);
AssetDatabase.CreateAsset(mater, "Assets/Resources/" + path.ToString() + "/Materials/" + m_texture[j].name + ".mat");
}
//添加材质球
SkinnedMeshRenderer[] SMR = go.GetComponentsInChildren<SkinnedMeshRenderer>();
for (int i = 0; i < SMR.Length; i++)
{
if (SMR[i].transform.name == name)
{
Mesh mesh = SMR[i].sharedMesh;
Material[] arrmate = m_Material.ToArray();
Transform[] Bones = SMR[i].bones;
SMR[i].sharedMesh = mesh;
SMR[i].materials = arrmate;
SMR[i].bones = Bones;
}
}
//生成prefab的路径
string PrefabPath = "Assets/Resources/" + path.ToString() + "/" + name + ".prefab";
PrefabUtility.CreatePrefab(PrefabPath, go);
Debug.Log("创建了文件:" + name);
}
/// <summary>
/// 清除之前设置过的AssetBundleName,避免产生不必要的资源也打包
/// </summary>
static void ClearAssetBundlesName()
{
int length = AssetDatabase.GetAllAssetBundleNames().Length;
string[] oldAssetBundleNames = new string[length];
for (int i = 0; i < length; i++)
{
oldAssetBundleNames[i] = AssetDatabase.GetAllAssetBundleNames()[i];
}
for (int j = 0; j < oldAssetBundleNames.Length; j++)
{
AssetDatabase.RemoveAssetBundleName(oldAssetBundleNames[j], true);
}
length = AssetDatabase.GetAllAssetBundleNames().Length;
}
}
上代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using System.IO;
public class CreateAllPrefab : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
[MenuItem("AssetBundle/Create All Prefab")]
static void CreateAllPrefabr()
{
if (EditorUtility.DisplayDialog("包名", "将会把Resources/AB/FBX目录下的fbx文件制成prefab并添加ab包名,包名即是名字是否确认?", "确定"))
{
DirectoryInfo raw = new DirectoryInfo(Application.dataPath + "/Resources/AB/FBX/");
//清除多余的ab名
ClearAssetBundlesName();
//为其返回子目录文件夹名称的数组的路径
foreach (DirectoryInfo dictorys in raw.GetDirectories())
{
string name = @dictorys.ToString().Replace(raw.ToString().Replace("/", @"\"), "");
//检查是否存在prefab
if (!File.Exists(dictorys.ToString() + @"\" + name + ".prefab"))
{
BuildPrefab(dictorys, name);
}
DirectoryInfo dif = new DirectoryInfo(dictorys + @"\");
FileInfo[] fileinfo = dif.GetFiles();
foreach (FileInfo finf in fileinfo)
{
if (finf.Extension.Equals(".prefab"))
{
//ab包名
string AbName = name.Substring(0, name.IndexOf("_")) + "/" + name;
//文件路径
string _assetPath = "Assets" + finf.ToString().Replace(@"\", "/").Substring(Application.dataPath.Length);
AssetImporter assetImporter = AssetImporter.GetAtPath(_assetPath);
if (assetImporter != null)
{
assetImporter.assetBundleName = AbName;
}
else
{
Debug.LogError(AbName + ".prefab 不存在,请确认是否存在该预制");
}
break;
}
}
}
Debug.Log("设置完成");
}
}
/// <summary>
/// 制作预制并且添加换装需要的资源
/// </summary>
/// <param name="dictorys"></param>
/// <param name="name"></param>
static void BuildPrefab(DirectoryInfo dictorys, string name)
{
//文件路径
string path = dictorys.ToString().Replace(@"\", "/").Substring(Application.dataPath.Length).Replace("/Resources/", "");
GameObject go = Resources.Load(path + "/" + name) as GameObject;
if (go != null)
{
go.name = name;
}
else
{
Debug.Log("资源名和文件夹名不对应,加载失败");
return;
}
Texture[] m_texture = Resources.LoadAll<Texture>(path);
List<Material> m_Material = new List<Material>();
//生成材质球
for (int j = 0; j < m_texture.Length - 1; j++)
{
Material mater = new Material(Shader.Find("Standard"));
mater.mainTexture = m_texture[j];
m_Material.Add(mater);
AssetDatabase.CreateAsset(mater, "Assets/Resources/" + path.ToString() + "/Materials/" + m_texture[j].name + ".mat");
}
//添加材质球
SkinnedMeshRenderer[] SMR = go.GetComponentsInChildren<SkinnedMeshRenderer>();
for (int i = 0; i < SMR.Length; i++)
{
if (SMR[i].transform.name == name)
{
Mesh mesh = SMR[i].sharedMesh;
Material[] arrmate = m_Material.ToArray();
Transform[] Bones = SMR[i].bones;
SMR[i].sharedMesh = mesh;
SMR[i].materials = arrmate;
SMR[i].bones = Bones;
}
}
//生成prefab的路径
string PrefabPath = "Assets/Resources/" + path.ToString() + "/" + name + ".prefab";
PrefabUtility.CreatePrefab(PrefabPath, go);
Debug.Log("创建了文件:" + name);
}
/// <summary>
/// 清除之前设置过的AssetBundleName,避免产生不必要的资源也打包
/// </summary>
static void ClearAssetBundlesName()
{
int length = AssetDatabase.GetAllAssetBundleNames().Length;
string[] oldAssetBundleNames = new string[length];
for (int i = 0; i < length; i++)
{
oldAssetBundleNames[i] = AssetDatabase.GetAllAssetBundleNames()[i];
}
for (int j = 0; j < oldAssetBundleNames.Length; j++)
{
AssetDatabase.RemoveAssetBundleName(oldAssetBundleNames[j], true);
}
length = AssetDatabase.GetAllAssetBundleNames().Length;
}
}
相关文章推荐
- Unity5自动命名Assetbundle并打包
- Unity之AssetBundle自动打包工具
- Unity AssetBundle 设置名称优化
- Unity热更新系列之AssetBundle(1) ——详述如何设置AssetBundle Name
- unity 代码设置要打包的AssetBundle
- Unity自动更新、AssetBundle整理
- Unity打包AssetBundle自动分析资源依赖关系(包括UGUI图集打包)
- Unity5自动命名Assetbundle并打包
- Unity资源解决方案之AssetBundle
- Unity 5.3 Assetbundle热更资源
- [Unity基础]导入贴图、模型等资源文件时自动设置参数
- Unity中AssetBundle资源打包
- Unity5.4 Assetbundles官方说明七(在AssetBundle中存储和加载二进制数据)
- Unity 5 官方打包管理工具 Asset Bundle Manager
- UnityAssetbundle知识点
- AssetBundle自动标签、打包
- unity5 AssetBundleBuild用法
- <Unity> AssetBundle
- 浅尝Unity 3D的Asset Bundle知识(七)-----依赖的利用
- unity3d里面prefab、assetbundle、unitypackage等包的使用