鸟语天空
UGUI—EditorGUILayout.HelpBox
post by:追风剑情 2022-10-20 13:27

示例:给资源加描述信息

1111.png

点击【Edit】可对信息进行编辑

2222.png

AssetHelpBox.cs

using UnityEngine;
/// <summary>
/// 显示资源帮助信息
/// </summary>
public class AssetHelpBox : MonoBehaviour
{
    [SerializeField]
    [HideInInspector]
    public string message;
}


AssetHelpBoxInspector.cs

using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
// 将此脚本放到 Editor 目录下
[CustomEditor(typeof(AssetHelpBox))]
public class AssetHelpBoxInspector : Editor
{
    protected AssetHelpBox mAsset;
    private bool isEditing;

    protected virtual void OnEnable()
    {
        mAsset = target as AssetHelpBox;
    }

    public override void OnInspectorGUI()
    {
        base.OnInspectorGUI();//保留Unity自动生成的Inspector

        if (isEditing)
        {
            mAsset.message = GUILayout.TextArea(mAsset.message);
            if (GUILayout.Button("OK", GUILayout.Width(50)))
            {
                isEditing = false;
                string path = AssetDatabase.GetAssetPath(mAsset);
                List<string> list = new List<string>();
                list.Add(path);
                AssetDatabase.ForceReserializeAssets(list);
            }
        }
        else
        {
            string msg = mAsset.message;
            if (!string.IsNullOrEmpty(msg))
                EditorGUILayout.HelpBox(msg, MessageType.Info);

            if (GUILayout.Button("Edit", GUILayout.Width(50)))
            {
                isEditing = true;
            }
        }
    }
}


评论:
发表评论:
昵称

邮件地址 (选填)

个人主页 (选填)

内容