Thumbnail tool to obtain the default body

 

 

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 well ()
         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 ( "Delete")) 
             { 
                 m_ObjPath.RemoveAt (I); 
             } 
             EditorGUILayout.EndHorizontal (); 
         } 

         EditorGUILayout.BeginHorizontal (); 
         IF (GUILayout.Button ( "add an empty object")) 
         { 
             m_ObjPath.Add (new new PrefabObj ()); 

         } 

         IF (GUILayout .button ( "Add the selected object")) 
         {
             The GameObject [] = Selection.gameObjects OBJS;
             for (int J = 0; J <objs.Length; J ++) 
             { 
                 AddPrefabobj (OBJS [J]); 
             } 
         } 

         EditorGUILayout.EndHorizontal (); 

         EditorGUILayout.BeginHorizontal (); 
         IF (GUILayout.Button ( "Export thumbnails") ) 
         { 
             ExportThumbnail (); 
         } 

         IF (GUILayout.Button ( "clear all configuration")) 
         { 
             m_ObjPath.Clear (); 
         } 

         EditorGUILayout.EndHorizontal (); 
         GUILayout.EndScrollView (); 
     } 



     // thumbnail configuration derived from the 
    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;
    }

}

  

Guess you like

Origin www.cnblogs.com/xingyunge/p/10930989.html