找回密码
 立即注册
查看: 497|回复: 0

Unity Prefab 缩略图全黑无法预览

[复制链接]
发表于 2021-4-14 08:43 | 显示全部楼层 |阅读模式
在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开发者联盟 ( 粤ICP备20003399号 )

GMT+8, 2024-9-20 22:41 , Processed in 0.088373 second(s), 26 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表