UnityEditor中实现拖拽Prefab时,在Scene窗口下显示Prefab的样子,可以通过以下步骤实现:

  1. 创建一个Editor脚本,并将其命名为'PrefabPreview.cs'。

  2. 在脚本中添加以下代码:

using UnityEngine;
using UnityEditor;

[CustomEditor(typeof(GameObject))]
public class PrefabPreview : Editor
{
    public override void OnPreviewGUI(Rect r, GUIStyle background)
    {
        GameObject go = (GameObject)target;
        if (PrefabUtility.GetPrefabType(go) == PrefabType.Prefab)
        {
            // 获取Prefab的预览图像
            Texture2D preview = AssetPreview.GetAssetPreview(target);
            if (preview != null)
            {
                // 在Scene窗口的左下角显示Prefab的预览图像
                GUI.DrawTexture(new Rect(r.x, r.y + r.height - 64, 64, 64), preview, ScaleMode.ScaleToFit);
            }
        }
    }
}
  1. 在UnityEditor中,选择需要显示预览的Prefab,然后将该Prefab拖拽到Scene窗口下。

  2. 在Scene窗口下,可以看到拖拽的Prefab的样子和预览图像。

注意:上述代码中,只有当拖拽的对象是Prefab时,才会显示预览图像。对于其他类型的对象,不会显示预览图像。

UnityEditor: 拖拽Prefab时在Scene窗口显示预览图像

原文地址: https://www.cveoy.top/t/topic/lNAt 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录