其他分享
首页 > 其他分享> > 获得预设体缩略图工具

获得预设体缩略图工具

作者:互联网

 

 

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