/// <summary> /// Validates target object is appropriate for the activation type. Null is considered valid. /// </summary> /// <param name="property"></param> /// <returns>Returns false if invalid</returns> public static bool ValidateTriggerTargetProperty(SerializedProperty property) { if (property == null) { return(false); } var targProp = property.FindPropertyRelative(EventTriggerTargetPropertyDrawer.PROP_TRIGGERABLETARG); if (targProp.objectReferenceValue == null) { return(true); } var actProp = property.FindPropertyRelative(EventTriggerTargetPropertyDrawer.PROP_ACTIVATIONTYPE); var act = actProp.GetEnumValue <TriggerActivationType>(); if (!EventTriggerTarget.IsValidTriggerTarget(targProp.objectReferenceValue, act)) { targProp.objectReferenceValue = null; return(false); } else { return(true); } }
public override bool Trigger(object sender, object arg) { if (!this.CanTrigger) { return(false); } var targ = this.GetTarget(arg); if (targ == null) { return(false); } switch (_triggerAction) { case TriggerActivationType.TriggerAllOnTarget: EventTriggerTarget.TriggerAllOnTarget(targ, sender, arg); return(true); case TriggerActivationType.TriggerSelectedTarget: EventTriggerTarget.TriggerSelectedTarget(targ, sender, arg); return(true); case TriggerActivationType.DestroyTarget: EventTriggerTarget.DestroyTarget(targ); return(true); } return(false); }
/// <summary> /// Adds targets to a Trigger/SPEvent. /// /// This method applies changes to the SerializedProperty. Only call if you expect this behaviour. /// </summary> /// <param name="triggerProperty"></param> /// <param name="objs"></param> public static void AddObjectsToTrigger(SerializedProperty triggerProperty, UnityEngine.Object[] objs) { if (triggerProperty == null) { throw new System.ArgumentNullException("triggerProperty"); } if (triggerProperty.serializedObject.isEditingMultipleObjects) { throw new System.ArgumentException("Can not use this method for multi-selected SerializedObjects.", "triggerProperty"); } try { triggerProperty.serializedObject.ApplyModifiedProperties(); var trigger = EditorHelper.GetTargetObjectOfProperty(triggerProperty) as BaseSPEvent; if (trigger == null) { return; } Undo.RecordObject(triggerProperty.serializedObject.targetObject, "Add Trigger Targets"); using (var set = TempCollection.GetSet <UnityEngine.Object>()) { for (int i = 0; i < trigger.Targets.Count; i++) { set.Add(trigger.Targets[i].Target); } foreach (var obj in objs) { if (set.Contains(obj)) { continue; } set.Add(obj); var targ = trigger.AddNew(); if (EventTriggerTarget.IsValidTriggerTarget(obj, TriggerActivationType.TriggerAllOnTarget)) { targ.ConfigureTriggerAll(obj); } else { targ.ConfigureCallMethod(obj, ""); } targ.Weight = 1f; } } triggerProperty.serializedObject.Update(); } catch (System.Exception ex) { Debug.LogException(ex); } }
private void _targetList_DrawElement(Rect area, int index, bool isActive, bool isFocused) { var element = _targetList.serializedProperty.GetArrayElementAtIndex(index); var targProp = element.FindPropertyRelative(EventTriggerTargetPropertyDrawer.PROP_TRIGGERABLETARG); const float MARGIN = 1.0f; const float WEIGHT_FIELD_WIDTH = 60f; const float PERC_FIELD_WIDTH = 45f; const float FULLWEIGHT_WIDTH = WEIGHT_FIELD_WIDTH + PERC_FIELD_WIDTH; EditorGUI.BeginProperty(area, GUIContent.none, targProp); Rect trigRect; if (_drawWeight && area.width > FULLWEIGHT_WIDTH) { var top = area.yMin + MARGIN; var labelRect = new Rect(area.xMin, top, EditorGUIUtility.labelWidth - FULLWEIGHT_WIDTH, EditorGUIUtility.singleLineHeight); var weightRect = new Rect(area.xMin + EditorGUIUtility.labelWidth - FULLWEIGHT_WIDTH, top, WEIGHT_FIELD_WIDTH, EditorGUIUtility.singleLineHeight); var percRect = new Rect(area.xMin + EditorGUIUtility.labelWidth - PERC_FIELD_WIDTH, top, PERC_FIELD_WIDTH, EditorGUIUtility.singleLineHeight); trigRect = new Rect(area.xMin + EditorGUIUtility.labelWidth, top, area.width - EditorGUIUtility.labelWidth, EditorGUIUtility.singleLineHeight); var weightProp = element.FindPropertyRelative(PROP_WEIGHT); float weight = weightProp.floatValue; if (this.OnDrawCustomizedEntryLabel != null) { this.OnDrawCustomizedEntryLabel(labelRect, element, index); } else { DrawDefaultListElementLabel(labelRect, element, index); } weightProp.floatValue = EditorGUI.FloatField(weightRect, weight); float p = (_totalWeight > 0f) ? (100f * weight / _totalWeight) : ((index == 0) ? 100f : 0f); EditorGUI.LabelField(percRect, string.Format("{0:0.#}%", p)); } else { //Draw Triggerable - this is the simple case to make a clean designer set up for newbs var top = area.yMin + MARGIN; var labelRect = new Rect(area.xMin, top, EditorGUIUtility.labelWidth, EditorGUIUtility.singleLineHeight); trigRect = new Rect(area.xMin + EditorGUIUtility.labelWidth, top, area.width - EditorGUIUtility.labelWidth, EditorGUIUtility.singleLineHeight); if (this.OnDrawCustomizedEntryLabel != null) { this.OnDrawCustomizedEntryLabel(labelRect, element, index); } else { DrawDefaultListElementLabel(labelRect, element, index); } } //Draw Triggerable - this is the simple case to make a clean designer set up for newbs EditorGUI.BeginChangeCheck(); var targObj = EventTriggerTargetPropertyDrawer.TargetObjectField(trigRect, GUIContent.none, targProp.objectReferenceValue); if (EditorGUI.EndChangeCheck()) { var actInfo = EventTriggerTargetPropertyDrawer.GetTriggerActivationInfo(element); targProp.objectReferenceValue = EventTriggerTarget.IsValidTriggerTarget(targObj, actInfo.ActivationType) ? targObj : null; } EditorGUI.EndProperty(); ReorderableListHelper.DrawDraggableElementDeleteContextMenu(_targetList, area, index, isActive, isFocused); }
private void DrawAdvanced_TriggerAll(Rect area, SerializedProperty property) { //Draw Target /* * var targRect = new Rect(area.xMin, area.yMin, area.width, EditorGUIUtility.singleLineHeight); * var targProp = property.FindPropertyRelative(TriggerTargetPropertyDrawer.PROP_TRIGGERABLETARG); * var targLabel = EditorHelper.TempContent("Triggerable Target"); * var targGo = GameObjectUtil.GetGameObjectFromSource(targProp.objectReferenceValue); * var newTargGo = EditorGUI.ObjectField(targRect, targLabel, targGo, typeof(GameObject), true) as GameObject; * if (newTargGo != targGo) * { * targGo = newTargGo; * targProp.objectReferenceValue = (targGo != null) ? targGo.transform : null; * } */ var targRect = new Rect(area.xMin, area.yMin, area.width, EditorGUIUtility.singleLineHeight); var targProp = property.FindPropertyRelative(EventTriggerTargetPropertyDrawer.PROP_TRIGGERABLETARG); var targLabel = EditorHelper.TempContent("Triggerable Target"); EditorGUI.BeginChangeCheck(); var targObj = TargetObjectField(targRect, targLabel, targProp.objectReferenceValue); if (EditorGUI.EndChangeCheck()) { targProp.objectReferenceValue = EventTriggerTarget.IsValidTriggerTarget(targObj, TriggerActivationType.TriggerAllOnTarget) ? targObj : null; } //Draw Triggerable Arg var argRect = new Rect(area.xMin, targRect.yMax, area.width - ARG_BTN_WIDTH, EditorGUIUtility.singleLineHeight); var btnRect = new Rect(argRect.xMax, argRect.yMin, ARG_BTN_WIDTH, EditorGUIUtility.singleLineHeight); var argArrayProp = property.FindPropertyRelative(EventTriggerTargetPropertyDrawer.PROP_TRIGGERABLEARGS); if (argArrayProp.arraySize == 0) { EditorGUI.LabelField(argRect, _defaultArgLabel, _undefinedArgLabel); if (GUI.Button(btnRect, _argBtnLabel)) { argArrayProp.arraySize = 1; argArrayProp.serializedObject.ApplyModifiedProperties(); } } else { if (argArrayProp.arraySize > 1) { argArrayProp.arraySize = 1; } var argProp = argArrayProp.GetArrayElementAtIndex(0); //EditorGUI.PropertyField(argRect, argProp, _defaultArgLabel); _variantDrawer.RestrictVariantType = false; _variantDrawer.ForcedObjectType = null; _variantDrawer.OnGUI(argRect, argProp, _defaultArgLabel); if (GUI.Button(btnRect, _argBtnLabel)) { argArrayProp.arraySize = 0; argArrayProp.serializedObject.ApplyModifiedProperties(); } } }