No type in ObjectField created in uxml (UIElements)
When i create an ObjectField inside the uxml file it appears in the inspector with no type.
uxml:
<uie:ObjectField name="Texture2D" binding-path="icone" label="Object" allow-scene-objects="false"/>
What I did to work around this problem is to change the type via code:
root.Query<ObjectField>("Texture2D").ForEach((obj) => {
obj.objectType = typeof(Texture2D);
});
In the documentation does not have any use example .
How can i add the type in a ObjectField by uxml?
Answer by LeXuSMalus · Feb 21, 2020 at 04:11 PM
Just use full assembly name.
<uie:ObjectField name="Texture2D" type="UnityEngine.Texture2D, UnityEngine.CoreModule" binding-path="icone" label="Object" allow-scene-objects="false"/>
Future readers: Note that this functionality was added in Unity 2020.1b, as seen in the "Release notes" section of this page:
UI Elements: Added type attribute to ObjectField UX$$anonymous$$L attributes. This allows you to specify a type and associated Assembly. For example "[sic]. If you are unsure of the type string to use, you can use
typeof($$anonymous$$yType).AssemblyQualifiedName
to get the full string.
I have this UX$$anonymous$$L element: <editor:ObjectField type="UnityEngine.TextAsset, UnityEngine.Core$$anonymous$$odule, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null" label="File"/> I have also tried: <editor:ObjectField objectType="UnityEngine.TextAsset, UnityEngine.Core$$anonymous$$odule, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null" label="File"/> They both produce the same thing the OP has.
Answer by merpheus · Jul 14, 2019 at 12:49 AM
You can't. You need to poke it through C#. Such as follows:
var uxmlField = container.Q<ObjectField>("the-uxml-field");
uxmlField.type = typeof(YourType);
The documentation seriously needs to be updated to reflect this.
This is not true, at least not any more. You can specify the type for it using the assembly qualified name of the type.
essentially using the full string you'll get when doing
typeof(Texture2D).AssemblyQualifiedName
$$anonymous$$y reccomendation is to just do
Debug.Log(typeof(Texture2D).AssemblyQualifiedName);
and copy the printout into your uxml ObjectField, in it's type-attribute.
I have the same issue with TextAsset. Tried "UnityEngine.TextAsset", "UnityEngine.TextAsset, UnityEngine.Core$$anonymous$$odule" and "UnityEngine.TextAsset, UnityEngine.Core$$anonymous$$odule, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null" (i.e. output of typeof(TextAsset).AssemblyQualifiedName). I tried all of them with both "type" and "objectType" atrributes and none of it works.
Answer by DarkGate · Aug 01, 2020 at 08:38 PM
Seems like the field has been changed from type
to objectType
.