0
\$\begingroup\$

I am making a custom UI with the UI Builder for my scriptable object that holds some data for the AI Player. It all worked fine until I added a third slider for a new value I needed; now the object won't preview in the inspector and I get an InvalidCastException for no reason. The sliders are for floats, and the values in the scriptable object are also floats, and the custom objects I am using for a list was working too, and I didn't change it at all. I tried debugging it by removing all data bindings in the builder so that it just renders the UI without any data, but it still gives the same error even though no data is being bound...

[CustomEditor(typeof(AiBrainData))]
    public class AiControllerDataEditor : Editor
    {
        private const string path = "UI/AI/ai_brain_data";

        public override VisualElement CreateInspectorGUI()
        {
            VisualTreeAsset vta = (VisualTreeAsset)Resources.Load(path);
            VisualElement root = new();
            vta.CloneTree(root);

            return root;
        }
    }

[CreateAssetMenu(menuName = "AI/Brain")]
    public class AiBrainData : ScriptableObject
    {
        [SerializeField] private BrainFeature[] _features;
        public BrainFeature[] BrainFeatures => _features;

        [SerializeField, Range(1f, 0f)] private float _reductionFactor = 0.5f;
        public float ReductionFactor => 1f - _reductionFactor;

        [SerializeField, Range(0f, 10f)] private float _increasionAmount = 0.5f;
        public float IncreasionAmount => _increasionAmount;

        [SerializeField, Range(0f, 5f)] private float _normalizationStep = 2.5f;
        public float NormalizationStep => _normalizationStep;
    }

<ui:UXML xmlns:ui="UnityEngine.UIElements" xmlns:uie="UnityEditor.UIElements" xsi="http://www.w3.org/2001/XMLSchema-instance" engine="UnityEngine.UIElements" editor="UnityEditor.UIElements" noNamespaceSchemaLocation="../../../../UIElementsSchema/UIElements.xsd" editor-extension-mode="True">
    <Style src="project://database/Assets/Resources/UI/AI/ai_brain_data.uss?fileID=7433441132597879392&amp;guid=65086b2200eb7e14eb6c4612fdf51cb5&amp;type=3#ai_brain_data" />
    <ui:VisualElement name="root">
        <ui:Label text="Brain Features" name="title" style="margin-left: 4px; margin-right: 4px; margin-top: 4px; margin-bottom: 4px; -unity-font-style: normal; font-size: 14px; -unity-text-align: upper-center;" />
        <ui:ListView focusable="false" name="items" picking-mode="Ignore" header-title="Brain Features" show-bound-collection-size="false" virtualization-method="DynamicHeight" binding-path="_features" view-data-key="_features" style="margin-bottom: 4px; background-color: rgb(202, 202, 202); border-left-width: 1px; border-right-width: 1px; border-top-width: 1px; border-bottom-width: 1px; border-left-color: rgb(176, 176, 176); border-right-color: rgb(176, 176, 176); border-top-color: rgb(176, 176, 176); border-bottom-color: rgb(176, 176, 176); padding-top: 4px; padding-right: 8px; padding-left: 2px; padding-bottom: 2px;" />
        <ui:Slider picking-mode="Ignore" label="Reduction" value="0.5" high-value="0" name="reduction" show-input-field="true" tooltip="The reduction factor for a feature outcome." low-value="1" binding-path="_reductionFactor" class="slider_brain" />
        <ui:Slider picking-mode="Ignore" label="Increasion" value="5" high-value="10" name="increasion" show-input-field="true" focusable="true" binding-path="_increasionAmount" class="slider_brain" />
        <ui:Slider picking-mode="Ignore" label="Normal Step" value="2.5" high-value="5" name="normal" tooltip="The step at which a reduced feature is normalized to it&apos;s weight." show-input-field="true" focusable="true" binding-path="_normalizationStep" class="slider_brain" />
    </ui:VisualElement>
</ui:UXML>

The error just points to the CreateInspectorGUI method in the AiControllerDataEditor class.

\$\endgroup\$

1 Answer 1

0
\$\begingroup\$

The problem was that I created a style sheet with the same name, and the script was trying to cast it to a VisualTreeAsset which is supposed to be a uxml file...

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .