Hello, it is even possible to create my own component without need to programatically define its uxml structure like
root.Add(new Label())
but create an uss, an uxml, script that extending VisualElement and put it together?
Hello, it is even possible to create my own component without need to programatically define its uxml structure like
root.Add(new Label())
but create an uss, an uxml, script that extending VisualElement and put it together?
sort of. You’ll need to do some scripting to define the component and bring all the elements and functionality together.
Examples are everywhere on how to do this.
These are the critical lines that bring in the uxml and the css.
var asset = AssetDatabase.LoadAssetAtPath<VisualTreeAsset>("Assets/Examples/Editor/Bindings/ListViewBinding.uxml");
root.styleSheets.Add(AssetDatabase.LoadAssetAtPath<StyleSheet>("Assets/Examples/Editor/Bindings/ListViewBinding.uss"));
Then you have to grab a reference to the element using css like selectors. Once you have a reference you can then apply values to the elements. or add child elements. Which you’ll more than likely need to do.
var status = root.Q<Label>("status");
var serializedObject = new SerializedObject(m_BindingsTestObject);
if (serializedObject == null)
{
status.text = "Unable to create SerializedObject!";
return;
}
root.Bind(serializedObject);
Quite often though you might want to just render using the standard inspector layout style. check this thread out and look for rendering default editor.
pirho_luke s post
using UnityEngine;
using UnityEditor;
using UnityEngine.UIElements;
using UnityEditor.UIElements;
[CustomEditor(typeof(Object), true, isFallback = true)]
public class DefaultEditor : UnityEditor.Editor
{
public override VisualElement CreateInspectorGUI()
{
var container = new VisualElement();
var iterator = serializedObject.GetIterator();
if (iterator.NextVisible(true))
{
do
{
var propertyField = new PropertyField(iterator.Copy()) { name = "PropertyField:" + iterator.propertyPath };
if (iterator.propertyPath == "m_Script" && serializedObject.targetObject != null)
propertyField.SetEnabled(value: false);
container.Add(propertyField);
}
while (iterator.NextVisible(false));
}
return container;
}
}