unity自动设置Assetbundle包名
来源:互联网 发布:矩阵的雅可比矩阵 编辑:程序博客网 时间:2024/06/05 10:54
因为是针对自己项目写的,所以有些地方需要读者自己修改
上代码
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;
}
}
- unity自动设置Assetbundle包名
- Unity AssetBundle 设置名称优化
- unity 生成webgl读取assetBundle包
- Unity之AssetBundle自动打包工具
- unity 代码设置要打包的AssetBundle
- unity AssetBundle
- unity-AssetBundle
- <Unity> AssetBundle
- Unity AssetBundle
- unity设置包最小
- as自动导入包名
- Unity 资源打包Assetbundle
- Unity AssetBundle的使用
- Unity中AssetBundle二
- unity AssetBundle的资源管理
- Unity 资源打包Assetbundle
- Unity自动更新、AssetBundle整理
- Unity 资源打包Assetbundle
- Android开发笔记:Socket通讯之Tcp客户端
- volatile
- Activiti进阶(二)——部署流程资源的三种方式
- mysql 5.7.7社区免安装版 初始化
- butternife使用
- unity自动设置Assetbundle包名
- 使用递归和非递归方式反转单向链表
- Sqoop常见错误及解决方式
- h5中多选框+通过身份证号做出性别年龄出生日期+用if点击切换颜色+加减乘除的应用
- 剑指offer66
- Kotlin设计模式-状态
- Invalid HTTP Connection : ADS in adobe forms
- Unity3D 5.6无法新建项目
- 文件上传与下载