Renaming class with UxmlElementAttribute - Breaks it

So I have a class in a namespace marked with [UxmlElementAttribute], which basically puts it available into UI builder.

Now we need to refactor namespace of this class. Issue is, when I change namespace, unity breaks and dozen of errors and warnings are thrown, that original class on original namespace was not found (since it was renamed).

Any way how to retain the reference?

I tried to use Unity’s [MovedFrom] attribute, but it doesn’t seem to work for this case.

Hi,
We do support using the MovedFromAttribute. We support changing the element name and its namespace.
The UxmlElement feature is new and came in during 2023.2 however features such as the MovedFrom support came later so its possible you are using an older version that does not support this. MovedFrom support landed in 2023.3.0a18.

If this is not the case can you provide an example uxml file, class and the errors so I can take a closer look?

2 Likes

Hi Karl, hmm its very strange, after renaming Unity throws me dozens of errors and warnings, but it seems it works anyway, even in playmode and in UI builder.

Also I may have incorrectly used [MoveFrom] attribute, which does not seem to have documentation though


So this should really be documented and added

What errors does it throw?
I also noticed it was missing documentation and have asked the team to update it.

1 Like

(when I rename and recompile codes)

Can you show what the class looks like and all the arguments you are passing to MovedFrom?

using Unity.Properties;
using UnityEngine.Scripting.APIUpdating;
using UnityEngine.UIElements;

namespace Darfall.ScenarioEditorUI.Core
{
    [MovedFrom(false, "Darfall.MapEditor.UI", null, "ToggleButton")]
    [UxmlElement]
    // ReSharper disable once PartialTypeWithSinglePart
    public partial class ToggleButton : Button
    {
        private bool value;

        [CreateProperty]
        public bool Value
        {
            get => value;
            set
            {
                if (this.value == value)
                {
                    return;
                }

                this.value = value;
                Repaint();
            }
        }

        public ToggleButton()
        {
            Repaint();
            clicked += () =>
            {
                if (isToggle)
                {
                    Value = !Value;
                }
                else
                {
                    Value = true;
                }
            };
        }

        /// <summary>
        ///     If true, the button will toggle between on and off states.
        ///     If false, clicking the button will always set the value to true.
        /// </summary>
        public bool isToggle;

        private void Repaint()
        {
            if (value)
            {
                AddToClassList(ScenarioStyles.ToggleButtonOn);
            }
            else
            {
                RemoveFromClassList(ScenarioStyles.ToggleButtonOn);
            }
        }
    }
}

Nothing stands out. Can you share the UXML file?