Godot Engine:属性面板(检视面板)插件(Inspector plugins)

参考官方文档《Inspector plugins》
在Godot中属性面板(检视面板)插件(Inspector plugins)是一种特殊编辑器插件

创建步骤

      • 1. 创建plugin.cfg
      • 2. EditorPlugin脚本
      • 3. EditorInspectorPlugin脚本
      • 4. EditorProperty脚本

1. 创建plugin.cfg

这一步和普通的编辑器插件一样,请见《Godot Engine:编辑器插件(Editor Plugin)开发 之 Hello World》

2. EditorPlugin脚本

tool
extends EditorPlugin
var plugin

func _enter_tree():
    plugin = preload("res://addons/MyPlugin/MyInspectorPlugin.gd").new()
    add_inspector_plugin(plugin)
    
func _exit_tree():
    remove_inspector_plugin(plugin)

注意:

  • EditorInspectorPlugin继承于Reference所以不能用它创建场景并用instance()类实例化,而需要直接加载脚本资源并用new()来实例化。
  • 通过add_inspector_plugin\remove_inspector_plugin把插件放置到Inspector上。

3. EditorInspectorPlugin脚本

# MyInspectorPlugin.gd
extends EditorInspectorPlugin


func can_handle(object):
    return true


func parse_property(object, type, path, hint, hint_text, usage):
    if type == TYPE_INT:
        add_property_editor(path, MyIntEditor.new())
        return true
    else:
        return false

EditorInspectorPlugin主要做两件事

  • 通过can_handle来过滤目标类型
    比如:
func can_handle(object):
	return object is GridMap
  • 通过parse_property来过滤目标属性,返回true即不显示默认的属性编辑器;返回false则在默认属性编辑器前插入自定义编辑器

type的类型

enum Variant.Type:
   TYPE_NIL = 0 --- Variable is null.
   TYPE_BOOL = 1 --- Variable is of type bool.
   TYPE_INT = 2 --- Variable is of type int.
   TYPE_REAL = 3 --- Variable is of type float (real).
   TYPE_STRING = 4 --- Variable is of type String.
   TYPE_VECTOR2 = 5 --- Variable is of type Vector2.
   TYPE_VECTOR2I = 6 --- Variable is of type Vector2i.
   TYPE_RECT2 = 7 --- Variable is of type Rect2.
   TYPE_RECT2I = 8 --- Variable is of type Rect2i.
   TYPE_VECTOR3 = 9 --- Variable is of type Vector3.
   TYPE_VECTOR3I = 10 --- Variable is of type Vector3i.
   TYPE_TRANSFORM2D = 11 --- Variable is of type Transform2D.
   TYPE_PLANE = 12 --- Variable is of type Plane.
   TYPE_QUAT = 13 --- Variable is of type Quat.
   TYPE_AABB = 14 --- Variable is of type AABB.
   TYPE_BASIS = 15 --- Variable is of type Basis.
   TYPE_TRANSFORM = 16 --- Variable is of type Transform.
   TYPE_COLOR = 17 --- Variable is of type Color.
   TYPE_STRING_NAME = 18 --- Variable is of type StringName.
   TYPE_NODE_PATH = 19 --- Variable is of type NodePath.
   TYPE_RID = 20 --- Variable is of type RID.
   TYPE_OBJECT = 21 --- Variable is of type Object.
   TYPE_CALLABLE = 22 --- Variable is of type Callable.
   TYPE_SIGNAL = 23 --- Variable is of type Signal.
   TYPE_DICTIONARY = 24 --- Variable is of type Dictionary.
   TYPE_ARRAY = 25 --- Variable is of type Array.
   TYPE_RAW_ARRAY = 26 --- Variable is of type PackedByteArray.
   TYPE_INT32_ARRAY = 27 --- Variable is of type PackedInt32Array.
   TYPE_INT64_ARRAY = 28 --- Variable is of type PackedInt64Array.
   TYPE_FLOAT32_ARRAY = 29 --- Variable is of type PackedFloat32Array.
   TYPE_FLOAT64_ARRAY = 30 --- Variable is of type PackedFloat64Array.
   TYPE_STRING_ARRAY = 31 --- Variable is of type PackedStringArray.
   TYPE_VECTOR2_ARRAY = 32 --- Variable is of type PackedVector2Array.
   TYPE_VECTOR3_ARRAY = 33 --- Variable is of type PackedVector3Array.
   TYPE_COLOR_ARRAY = 34 --- Variable is of type PackedColorArray.
   TYPE_MAX = 35 --- Represents the size of the Variant.Type enum.

4. EditorProperty脚本

# MyIntEditor.gd
extends EditorProperty
class_name MyIntEditor


var updating = false
var spin = EditorSpinSlider.new()
func _init():
   add_child(spin)
   add_focusable(spin)
   spin.set_min(0)
   spin.set_max(1000)
   spin.connect("value_changed", self, "_spin_changed")


func _spin_changed(value):
    if (updating):
        return
    emit_changed(get_edited_property(), value)

func update_property():
    var new_value = get_edited_object()[get_edited_property()]
    updating = true
    spin.set_value(new_value)
    updating = false

这一步就是定义特定属性的编辑器了

你可能感兴趣的:(Godot笔记,#,Godot,实践,游戏开发,Godot,Engine)