버전:2021.3+
바인딩 경로 대신 BindProperty()를 호출하여 요소를 SerializedProperty 오브젝트에 직접 바인딩할 수 있습니다.이 예제에서는 BindProperty()를 사용하여 바인딩하는 방법을 보여 줍니다.
이 예제에서는 게임 오브젝트의 이름을 변경하는 커스텀 에디터 창을 생성합니다.
이 예제에서 생성한 완성된 파일은 다음 GitHub 저장소에서 확인할 수 있습니다.
이 가이드는 Unity 에디터, UI 툴킷, C# 스크립팅에 익숙한 개발자용입니다.시작하기 전에 먼저 다음을 숙지하십시오.
BindProperty()로 바인드TextField를 사용하여 C#에서 커스텀 에디터 창을 만듭니다.게임 오브젝트의 이름 프로퍼티를 찾아 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();
            }
        }
    }
}