Unity Prefab 缩略图全黑无法预览

在Unity开发中经常遇到一种情况:就是在Unity编辑器中Project窗口Prefab无法预览,缩略图是一张全黑的图片,这其实非常不友好,尤其是对美术的小伙伴。虽然系统自带刷新,重新导入之类的功能,但是偶尔有效偶尔无效。大大降低了工作效率,浪费了大把时间在找资源上。
这里做了个小工具,反正我把自己项目里的所有缩略图不能预览的prefab都解决了,其他的也没多做测试。可能能帮到你,也可能浪费你感情。莫怪。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;

public class ProjectController
{
    static Texture m_sPrefabIcon;
    static Texture m_spPriviewIcon;
    static string m_activeObjPath;
    static string m_name;
    [InitializeOnLoadMethod]
    static void InitializeOnLoadMethod()
    {
        EditorApplication.projectWindowItemOnGUI = delegate (string guid, Rect selectionRect)
        {
            m_activeObjPath = AssetDatabase.GetAssetPath(Selection.activeObject);
            if (Selection.activeObject &&
                guid == AssetDatabase.AssetPathToGUID(m_activeObjPath))
            {                    
                if(m_activeObjPath.EndsWith(".prefab") )
                {
                    OnGUI(selectionRect, Selection.activeObject);
                }
                else if(m_activeObjPath.EndsWith(".mat"))
                {
                    OnGUI(selectionRect, Selection.activeObject);
                }
                else
                {
                    if(m_activeObjPath.LastIndexOf('.') > 0)
                    {
                        m_name = m_activeObjPath.Substring(m_activeObjPath.LastIndexOf('.'));
                    }
         
                    if (GUI.Button(selectionRect, m_name))
                    {
                        Debug.LogFormat("click:{0}", Selection.activeObject.name + "  Path:" + m_activeObjPath + "  " + m_name);
                    }
                }
            }
        };
    }

    private static void OnGUI(Rect rect , Object obj)
    {
        //设置按钮区域
        float width = 50f;
        rect.x += rect.width * 0.5f;
        rect.y += 2f;
        rect.width = width;
        rect.height -= 20;
        //m_sPrefabIcon = PrefabUtility.GetIconForGameObject((GameObject)Selection.activeObject);
        m_spPriviewIcon = AssetPreview.GetAssetPreview(obj) as Texture; 

        if (GUI.Button(rect, m_spPriviewIcon))
        {
            Debug.LogFormat("click:{0}", obj.name);
            if (m_spPriviewIcon == null)
                Debug.LogFormat("click:{0}", "没有priview");
        }
        Rect freshRect = rect;
        freshRect.y = rect.y + rect.height;
        freshRect.height = 20;

        if (GUI.Button(freshRect, "刷新"))
        {
            Fresh(Selection.activeObject);
        }
    }

    private static void Fresh(Object obj)
    {            
        EditorUtility.SetDirty(obj);
        AssetDatabase.SaveAssets();
        AssetDatabase.Refresh(ImportAssetOptions.ForceSynchronousImport);
    }
}

你可能感兴趣的:(Unity Prefab 缩略图全黑无法预览)