批量生成圖片預製躰,并且保留當前文件目錄
来源:互联网 发布:知乎注册不了为什么 编辑:程序博客网 时间:2024/06/05 08:10
因爲項目中要用到一個批量生成預製躰的工具給策劃用,所以就把這個工作交給我了,不多說了,直接上代碼,第一次寫工具,也是第一次寫博客,寫的不好,請多多指教!
public class BatchCreatePrefab : EditorWindow
{
[MenuItem(“Tool/Common/生成预制”)]
private static void PrefabWrapTool()
{
GetWindow().Show();
}
private int m_index = 0; private static string[] s_outputDirectories; private void OnEnable() { Selection.selectionChanged += Repaint; } private void OnDisable() { Selection.selectionChanged -= Repaint; } /// <summary> /// 刷新目录 /// </summary> private void UpdateDirectory() { s_outputDirectories = Directory.GetDirectories("Assets\\Resources", "*", SearchOption.AllDirectories); System.Array.Sort(s_outputDirectories); Repaint(); } private static string RemoveDirectory = "Assets"; private void OnGUI() { if (s_outputDirectories == null) { UpdateDirectory(); } EditorGUILayout.BeginHorizontal(); float defaultLabelWidth = EditorGUIUtility.labelWidth; EditorGUIUtility.labelWidth = 60; EditorGUILayout.PrefixLabel("输出目录"); m_index = EditorGUILayout.Popup(m_index, s_outputDirectories); EditorGUIUtility.labelWidth = defaultLabelWidth; if (GUILayout.Button("刷新", GUILayout.MaxWidth(40))) { UpdateDirectory(); } EditorGUILayout.EndHorizontal(); //忽略层级目录 EditorGUILayout.BeginHorizontal(); EditorGUIUtility.labelWidth = 100; EditorGUILayout.PrefixLabel("忽略层级目录:"); RemoveDirectory = EditorGUILayout.TextField(RemoveDirectory); EditorGUILayout.EndHorizontal(); EditorGUILayout.Space(); //选中生成数量 int selectionCount = Selection.GetFiltered(typeof(Texture2D), SelectionMode.Assets).Length; EditorGUILayout.BeginHorizontal(); EditorGUILayout.Space(); //绘制一个生成按钮 if (GUILayout.Button("生成(" + selectionCount + ")", GUILayout.Height(20), GUILayout.MaxWidth(100))) { CreateAllSelectionPrefab(s_outputDirectories[m_index]); } EditorGUILayout.EndHorizontal(); } private static void CreateAllSelectionPrefab(string outputPath) { Object[] allSelectedTextuers = Selection.GetFiltered(typeof(Texture2D), SelectionMode.Assets); if (allSelectedTextuers.Length == 0) { Debug.LogWarning("未选中物体"); return; } GameObject spriteObject = new GameObject(); SpriteRenderer spriteRenderer = spriteObject.AddComponent<SpriteRenderer>(); for (int i = 0; i < allSelectedTextuers.Length; i++) { string path = AssetDatabase.GetAssetPath(allSelectedTextuers[i]).Replace("/", "\\"); string replaceString = path.Replace("/", "\\"); if (!Directory.Exists(RemoveDirectory)) { Object.DestroyImmediate(spriteObject); Debug.LogError("忽略层级目录错误"); return; } string replaceDirectory = replaceString.Replace(RemoveDirectory, null);//替换字符串 int lastCharIndex = replaceDirectory.LastIndexOf("\\");//得到物体的下标 string removeString = replaceDirectory.Remove(lastCharIndex, replaceDirectory.Length - lastCharIndex); string finalOutputPath = outputPath + removeString; Sprite sprite = AssetDatabase.LoadAssetAtPath<Sprite>(path); spriteRenderer.sprite = sprite; spriteObject.name = allSelectedTextuers[i].name; CreateSpritePrefab(spriteObject, finalOutputPath); } Object.DestroyImmediate(spriteObject); Debug.Log("生成成功"); } private static void CreateSpritePrefab(GameObject spriteObject, string outputPath) { if (Directory.Exists(outputPath) == false) { Directory.CreateDirectory(outputPath); AssetDatabase.Refresh(); } outputPath = outputPath.Replace("\\", "/"); PrefabUtility.CreatePrefab(outputPath + "/" + spriteObject.name + ".prefab", spriteObject); AssetDatabase.Refresh(); }}
下一篇
批量重命名
0 0
- 批量生成圖片預製躰,并且保留當前文件目錄
- 前文
- javascript批量生成并且保存为txt文件
- python+shell 批量获取服务器信息并且生成excel表
- forfiles命令批量删除N天前文件
- forfiles命令批量删除N天前文件
- mvn 批量上传jar到私服 工具 python实现 (并且生成 dependencies 文件)
- Mysql定时执行任务,执行批量的sql语句 并且查看已经生成的定时事件
- 数据库 从 临时表 批量插入 并且 生成id 的问题
- Mac下顯示當前文件目錄(配置open cv on ios)
- 博客前文
- 前文提要
- 批量更改文件名称并且标号
- 二维码内置图片,并且把二维码放在图片指定位置上,画字,设置字本地样式,二维码批量生成
- PHP_保留两位小数并且四舍五入_保留两位小数并且不四舍五入
- DHCP保留地址批量导入导出
- 读取本地文件名并且批量导入数据库
- linux 批量查找文件并且重命名
- mysql多个left join
- SpringWebService
- java中类的初始化顺序
- web应用程序状态管理
- Unity性能优化—声音优化
- 批量生成圖片預製躰,并且保留當前文件目錄
- 【目标检测:Hough Forest】hough forest目标检测原理(一)
- 解决WordExcel启动提示向程序发送命令出现问题
- 文章标题
- 【JZOJ4821】打膈膜
- LeetCode 66 - Plus One
- MemCached add命令的用法详解
- UGUI之Inputfield在安卓中设置默认调用中文输入法和记录输入法
- HI3520环境搭建