Loss of Funtionality

I understand that this lecture is about showing us how to use sub-components but we’re actually losing functionality in order to have the label have a different text colour instead of a background highlight - the level element next to the toggle does not behave as a toggle label - clicking on it doesn’t toggle the checkbox and it doesn’t respond to the :checked style
I tried to reimplement it myself but the label doesn’t appear to have an onclick event

The UI Label does not automatically come with a way to click, but it can be added by adding a Clickable manipulator.
In TaskItem.cs:

        public TaskItem(string taskText)
        {
            VisualTreeAsset original = AssetDatabase.LoadAssetAtPath<VisualTreeAsset>(TaskListEditor.path + "TaskItem.uxml");
            this.Add(original.Instantiate());

            taskToggle = this.Q<Toggle>();
            taskLabel = this.Q<Label>();
            taskLabel.text = taskText;
            Clickable clickable = new Clickable(() =>
            {
                taskToggle.value = !taskToggle.value;
            });
            taskLabel.AddManipulator(clickable);
        }

In this case, we’re assigning a lambda expression to the Clickable that simply toggles the state of the taskToggle, and then adding the Clickable to the label’s list of manipulators.

A further enhancement would be to also make the label bold or normal based on it’s clearance, which can also be handled within our Lambda expression

            Clickable clickable = new Clickable(() =>
            {
                taskToggle.value = !taskToggle.value;
                taskLabel.style.unityFontStyleAndWeight =
                    new StyleEnum<FontStyle>(taskToggle.value ? FontStyle.Bold : FontStyle.Normal);
            });

Awesome, thanks Brian, you should do your own course :wink:

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms