Unity Editor编辑器拓展

Editor GUI

InspectorGUI

Screen Size
  • Screen.width // 当前InspectorGUI大小,并不是SceneView大小
  • Screen.height
属性变化
  • DrawDefaultInspector() // Editor更方便的检测方法
  • BeginChangeCheck() // 检测属性UI值变化
  • EndChangeCheck() // 若有改变则为true
  • OnValidate() // Mono属性变化时调用,用来限定属性
    1
    2
    3
    4
    5
    6
    7
    public override void OnInspectorGUI()
    {
    if (DrawDefaultInspector())
    {
    terrainMesh.CreateTerrain();
    }
    }
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    public override void OnInspectorGUI()
    {
    EditorGUI.BeginChangeCheck();
    base.OnInspectorGUI();

    if (EditorGUI.EndChangeCheck())
    {
    terrainMesh.CreateTerrain();
    }
    }

Attribute

  • SerializeField
  • HideInInspector
  • MenuItem // 自定义编辑器菜单
  • CustomEditor // 自定义对象属性GUI
  • ExecuteInEditMode
  • RequireComponent
  • ImageEffectAllowedInSceneView

Shortcut

  • EditorTool
  • ToolManager.SetActiveTool(EditorTools.EditorTool tool)
    1
    2
    3
    4
    5
    6
    using UnityEditor.ShortcutManagement;
    [Shortcut("Activate Paint Tool", typeof(SceneView), KeyCode.U)]
    static void Shortcut()
    {
    ToolManager.SetActiveTool<EditorTools>();
    }

Shader GUI || Material Editor

CustomEditor || CustomPropertyDrawer

UIElements

Undo

Reference

Tool | 工具类

EditorUtility

  • RevealInFinder // 打开文件夹
  • OpenFilePanel
  • OpenFolderPanel
  • OpenFilePanelWithFilters

EditorGUIUtility

  • PingObject //浏览、选中资产
    • Selection.activeObject = data

AssetDatabase

  • LoadAssetAtPath
  • AssetPathToGUID
  • Contains
  • CopyAsset
  • CreateAsset
  • ExtractAsset
  • ImportAsset
  • FindAssets
  • GetAssetPath
  • CreateFolder
  • ExportPackage

PrefabUtility

  • CreatePrefab
  • CreateEmptyPrefab

Project Setting

  • UnityEngine.QualitySettings
    • renderPipeline
  • UnityEngine.Rendering.GraphicsSettings
    • renderPipelineAsset
  • UnityEditor.EditorSettings
  • UnityEditor.PlayerSettings
  • UnityEditor.EditorBuildSettings
  • UnityEditor.BuildPipeline //build game

Unity技巧

  • 方法属性加Button
  • Unity小窍门100条!!!(上)
  • FormerlySerializedAs()
    • 重命名显示
  • RuntimeInitializeOnLoadMethod
    • [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.AfterSceneLoad)]
      • 无需创建空物体就能执行代码
      • 在运行游戏(Play)就会立即执行该函数,而不需要继承MonoBehavior并挂接在物体身上。

Unity Editor编辑器拓展
https://automask.github.io/wild/2021/10/26/log/P_Unity_Editor/
作者
Kyle Zhou
发布于
2021年10月26日
许可协议