버전: 2021.3+
바인딩 경로를 사용하는 대신 요소를 SerializedProperty 오브젝트에 직접 바인딩하려면 BindProperty()를 호출할 수 있습니다. 다음 예시는 BindProperty()와 바인딩하는 방법을 보여 줍니다.
이 예제에서는 게임 오브젝트의 이름을 변경하는 커스텀 에디터 창을 생성합니다.
이 예시에서 만든 완성된 파일은 이 GitHub 저장소에서 찾을 수 있습니다.
이 가이드는 Unity 에디터, UI 툴킷, C# 스크립팅에 익숙한 개발자를 위한 가이드입니다. 시작하기 전에 먼저 다음을 숙지하십시오.
BindProperty()와 바인딩TextField를 사용하여 C#으로 커스텀 에디터 창을 생성합니다. 게임 오브젝트의 name 프로퍼티를 찾아 BindProperty() 메서드를 사용하여 프로퍼티에 직접 바인딩합니다.
프로젝트 창에서 파일을 저장하기 위한 bind-without-binding-path라는 이름의 폴더를 만듭니다.
bind-without-binding-path 폴더에 Editor라는 이름의 폴더를 만듭니다.
Editor 폴더에서 SimpleBindingPropertyExample.cs라는 이름의 C# 스크립트를 생성하고 해당 내용을 다음과 같이 바꿉니다.
using UnityEditor;
using UnityEngine;
using UnityEditor.UIElements;
using UnityEngine.UIElements;
namespace UIToolkitExamples
{
public class SimpleBindingPropertyExample : EditorWindow
{
TextField m_ObjectNameBinding;
[MenuItem("Window/UIToolkitExamples/Simple Binding Property Example")]
public static void ShowDefaultWindow()
{
var wnd = GetWindow<SimpleBindingPropertyExample>();
wnd.titleContent = new GUIContent("Simple Binding Property");
}
public void CreateGUI()
{
m_ObjectNameBinding = new TextField("Object Name Binding");
rootVisualElement.Add(m_ObjectNameBinding);
OnSelectionChange();
}
public void OnSelectionChange()
{
GameObject selectedObject = Selection.activeObject as GameObject;
if (selectedObject != null)
{
// Create the SerializedObject from the current selection
SerializedObject so = new SerializedObject(selectedObject);
// Note: the "name" property of a GameObject is actually named "m_Name" in serialization.
SerializedProperty property = so.FindProperty("m_Name");
// Bind the property to the field directly
m_ObjectNameBinding.BindProperty(property);
}
else
{
// Unbind any binding from the field
m_ObjectNameBinding.Unbind();
}
}
}
}