I’m trying to get a parentPropertyDrawer to capture events dispatched from a childPropertyDrawer.
public class MissionTaskDrawer : PropertyDrawer
{
public override VisualElement CreatePropertyGUI(SerializedProperty property)
{
var subtask = property.FindPropertyRelative("subTask");
VisualElement container = new VisualElement(){ name="root-task"};
var subTaskField = new PropertyField(subTask);
subTaskField.userData = new TaskDrawerUserData(){index = idx};
container.Add(subTaskField);
container.RegisterCallback<AmendTaskEvent>(amendEvent =>
{
Debug.Log("taskHeader received change callback."+ amendEvent.action);
});
}
}
In the child custom property draw i call the below custom event when the user clicks a button.
using (AmendTaskEvent deleteItemEvent = AmendTaskEvent.GetPooled("delete-item"))
{
Debug.Log("Delete Icon pressed");
//deleteItemEvent.target = _propertyField.parent.parent.parent;
_propertyField.SendEvent(deleteItemEvent);
}
Using the deleteItemEvent.target i can specify the parent property, but its seems rediculous that i’m passing parent.parent.parent.parent and in fact whilst i can get this to work to some extent, but i believe i should be able to just dispatch an event without having to specify the exact node that needs to receive it. which is especially difficult when the target is a parent of a parent of a parent.
I assume that if i can get the AmendTaskEvent to bubble, all parent viewelements should be able to register for this event and deal with it appropriately. but Rider completes about
Cannot access internal enum 'EventPropagation' here
in the AmendTaskEvent class below.
I’m totally stumped and not even convinced this is the right approach, but with a lack of clear documentation on dispatching events to child views i’ve not yet found what the correct way of doing this is.
namespace UnityEngine.UIElements {
public class AmendTaskEvent : EventBase<AmendTaskEvent>
{
public string action { get; protected set; }
public static AmendTaskEvent GetPooled(string action)
{
AmendTaskEvent pooled = EventBase<AmendTaskEvent>.GetPooled();
pooled.action = action;
return pooled;
}
private void LocalInit()
{
//this.propagation = EventBase.EventPropagation.Bubbles | EventBase.EventPropagation.TricklesDown;
this.action = default (string);
}
public AmendTaskEvent() => this.LocalInit();
}
}