属性检查插件

检查器dock支持自定义插件来创建自己的编辑属性的小部件.本教程以创建自定义值编辑器为例,解释了如何使用 :ref:`class_EditorInspectorPlugin`和 :ref:`class_EditorProperty`类来编写此类插件.

场景布置

就像 制作插件 一样,我们先制作一个新的插件,得到一个 plugin.cfg 文件,然后从我们的 EditorPlugin 开始. 但是,我们不使用 add_custom_nodeadd_control_to_dock ,而是使用 add_inspector_plugin .

GDScript # MyEditorPlugin.gd tool extends EditorPlugin

  1. var plugin
  2. func _enter_tree():
  3. # EditorInspectorPlugin is a resource, so we use `new()` instead of `instance()`.
  4. plugin = preload("res://addons/MyPlugin/MyInspectorPlugin.gd").new()
  5. add_inspector_plugin(plugin)
  6. func _exit_tree():
  7. remove_inspector_plugin(plugin)

EditorInspectorPlugin(编辑器属性面板插件)

为了实际连接到检查器,我们创建一个 :ref:`class_EditorInspectorPlugin`类.这个脚本为检查器提供了 “钩子”.多亏了这个类,编辑器在为检查器构建UI的过程中,会调用EditorInspectorPlugin中的函数.该脚本用于检查我们是否应该为当前在检查器中的任何 :ref:`class_Object`(包括任何 :ref:`class_Resource`的嵌入!)启用自己.

一旦启用,EditorInspectorPlugin方法允许添加 :ref:`class_EditorProperty`节点或只是自定义 :ref:`class_Control`节点到该 :ref:`class_Object`的检查器的开头和结尾,或者覆盖或改变现有的属性编辑器.

GDScript

  1. # MyInspectorPlugin.gd
  2. extends EditorInspectorPlugin
  3. func can_handle(object):
  4. # Here you can specify which object types (classes) should be handled by
  5. # this plugin. For example if the plugin is specific to your player
  6. # class defined with `class_name MyPlayer`, you can do:
  7. # `return object is MyPlayer`
  8. # In this example we'll support all objects, so:
  9. return true
  10. func parse_property(object, type, path, hint, hint_text, usage):
  11. # We will handle properties of type integer.
  12. if type == TYPE_INT:
  13. # Register *an instance* of the custom property editor that we'll define next.
  14. add_property_editor(path, MyIntEditor.new())
  15. # We return `true` to notify the inspector that we'll be handling
  16. # this integer property, so it doesn't need to parse other plugins
  17. # (including built-in ones) for an appropriate editor.
  18. return true
  19. else:
  20. return false

EditorProperty(编辑器属性)

接下来,我们定义实际的 class_EditorProperty`自定义值编辑器,我们希望实例化它来编辑整数.这是一个自定义的 :ref:`class_Control,我们可以添加任何类型的附加节点来制作高级部件以嵌入到检查器中.

GDScript

  1. # MyIntEditor.gd
  2. extends EditorProperty
  3. class_name MyIntEditor
  4. var updating = false
  5. var spin = EditorSpinSlider.new()
  6. func _init():
  7. # We'll add an EditorSpinSlider control, which is the same that the
  8. # inspector already uses for integer and float edition.
  9. # If you want to put the editor below the property name, use:
  10. # `set_bottom_editor(spin)`
  11. # Otherwise to put it inline with the property name use:
  12. add_child(spin)
  13. # To remember focus when selected back:
  14. add_focusable(spin)
  15. # Setup the EditorSpinSlider
  16. spin.set_min(0)
  17. spin.set_max(1000)
  18. spin.connect("value_changed", self, "_spin_changed")
  19. func _spin_changed(value):
  20. if (updating):
  21. return
  22. emit_changed(get_edited_property(), value)
  23. func update_property():
  24. var new_value = get_edited_object()[get_edited_property()]
  25. updating = true
  26. spin.set_value(new_value)
  27. updating = false