标签:obj name 缩略图 预设 GUILayout ObjPath EditorGUILayout 工具 public
using System.Collections; using System.Collections.Generic; using System.IO; using UnityEditor; using UnityEngine; public class ThumbnaiWindow : EditorWindow { private string outPath = "Assets/TestPrefab"; List<PrefabObj> m_ObjPath = new List<PrefabObj>(); [MenuItem("Tools/Thumbnais")] public static void ShowWindow() { EditorWindow.GetWindow(typeof(ThumbnaiWindow)); Selection.selectionChanged = () => { Debug.Log("change"); }; } private Vector2 scrollTextureView = Vector2.zero; void OnGUI() { scrollTextureView = GUILayout.BeginScrollView(scrollTextureView); EditorGUILayout.BeginHorizontal(); GUILayout.Label("导出路径:"); outPath = GUILayout.TextField(outPath); EditorGUILayout.EndHorizontal(); GUILayout.Label("加入对象:", EditorStyles.boldLabel); for (int i = 0; i < m_ObjPath.Count; ++i) { EditorGUILayout.BeginHorizontal(); string assetPath = m_ObjPath[i].path; var obj = AssetDatabase.LoadAssetAtPath<GameObject>(assetPath); EditorGUI.BeginChangeCheck(); var newPrefab = EditorGUILayout.ObjectField(obj, typeof(GameObject), false); if (EditorGUI.EndChangeCheck()) { var newPath = AssetDatabase.GetAssetPath(newPrefab); m_ObjPath[i].path = newPath; m_ObjPath[i].name = newPrefab.name; } if (GUILayout.Button("删除")) { m_ObjPath.RemoveAt(i); } EditorGUILayout.EndHorizontal(); } EditorGUILayout.BeginHorizontal(); if (GUILayout.Button("添加空物体")) { m_ObjPath.Add(new PrefabObj()); } if (GUILayout.Button("添加所选对象")) { GameObject[] objs = Selection.gameObjects; for (int j = 0; j < objs.Length; j++) { AddPrefabobj(objs[j]); } } EditorGUILayout.EndHorizontal(); EditorGUILayout.BeginHorizontal(); if (GUILayout.Button("导出缩略图")) { ExportThumbnail(); } if (GUILayout.Button("清除所有配置")) { m_ObjPath.Clear(); } EditorGUILayout.EndHorizontal(); GUILayout.EndScrollView(); } //从配置导出缩略图 public void ExportThumbnail() { Editor objeditor = Editor.CreateEditor(GameObject.Find("aa")); for (int i = 0; i < m_ObjPath.Count; i++) { GameObject obj = AssetDatabase.LoadAssetAtPath<GameObject>(m_ObjPath[i].path); AssetPreview.SetPreviewTextureCacheSize(1024); Texture2D tempTexture = AssetPreview.GetAssetPreview(obj); if (tempTexture == null || !tempTexture.isReadable) { continue; } byte[] bytesThumbnail = tempTexture.EncodeToPNG(); if (!Directory.Exists(outPath)) { Directory.CreateDirectory(outPath); } FileStream outputThumbnail = new FileStream(outPath + "/" + Path.GetFileNameWithoutExtension(m_ObjPath[i].path) +"_Icon"+ ".png", FileMode.Create); outputThumbnail.Write(bytesThumbnail, 0, bytesThumbnail.Length); outputThumbnail.Flush(); outputThumbnail.Close(); } AssetDatabase.Refresh(); } public void AddPrefabobj(GameObject obj) { string assetPath = AssetDatabase.GetAssetPath(obj); PrefabObj prefabObj = new PrefabObj(); prefabObj.name = obj.name; prefabObj.path = assetPath; bool isContian = false; for (int i = 0; i < m_ObjPath.Count; i++) { if (m_ObjPath[i].name == prefabObj.name) isContian = true; } for (int i = 0; i < m_ObjPath.Count; i++) { if (m_ObjPath[i].name == null) { m_ObjPath.RemoveAt(i); } } if(!isContian) m_ObjPath.Add(prefabObj); } public class PrefabObj { public string path; public string name; } }
标签:obj,name,缩略图,预设,GUILayout,ObjPath,EditorGUILayout,工具,public
来源: https://www.cnblogs.com/xingyunge/p/10930989.html
本站声明:
1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。