/// <summary> /// Do the action /// </summary> /// <param name="args">Event arguments.</param> /// <param name="action">Action.</param> virtual protected void DoImmediateAction(EventResponse action, System.EventArgs args) { Animator animator; Animation animation; switch (action.responseType) { case EventResponseType.DEBUG_LOG: Debug.Log(string.Format("Got event, arguments: {0}", args != null ? args.ToString() : "")); break; case EventResponseType.ACTIVATE_GAMEOBJECT: action.targetGameObject.SetActive(true); break; case EventResponseType.DEACTIVATE_GAMEOBJECT: action.targetGameObject.SetActive(false); break; case EventResponseType.SEND_MESSSAGE: action.targetGameObject.SendMessage(action.message, SendMessageOptions.DontRequireReceiver); break; case EventResponseType.ENABLE_BEHAVIOUR: if (action.targetComponent is Behaviour) { ((Behaviour)action.targetComponent).enabled = true; } else if (action.targetComponent is Renderer) { ((Renderer)action.targetComponent).enabled = true; } break; case EventResponseType.DISABLE_BEHAVIOUR: if (action.targetComponent is Behaviour) { ((Behaviour)action.targetComponent).enabled = false; } else if (action.targetComponent is Renderer) { ((Renderer)action.targetComponent).enabled = false; } break; case EventResponseType.PLAY_PARTICLES: if (action.targetComponent is ParticleSystem) { ((ParticleSystem)action.targetComponent).Play(); } break; case EventResponseType.PAUSE_PARTICLES: if (action.targetComponent is ParticleSystem) { ((ParticleSystem)action.targetComponent).Pause(); } break; case EventResponseType.SWITCH_SPRITE: if (action.targetComponent is SpriteRenderer) { ((SpriteRenderer)action.targetComponent).sprite = action.newSprite; } break; case EventResponseType.LOAD_SCENE: #if !UNITY_4_6 && !UNITY_5_1 && !UNITY_5_2 SceneManager.LoadScene(action.message); #else Application.LoadLevel(action.message); #endif break; case EventResponseType.START_EFFECT: if (action.targetComponent is FX_Base) { if (action.targetGameObject != null && action.message != null && action.message != "") { ((FX_Base)action.targetComponent).StartEffect(action.targetGameObject, action.message); } else { ((FX_Base)action.targetComponent).StartEffect(); } } else { Debug.LogWarning("Trying to play an Effect that isn't derived from FX_Base."); } break; case EventResponseType.PLAY_ANIMATION: animator = action.targetGameObject.GetComponent <Animator>(); if (animator != null) { animator.Play(action.message); } else { animation = action.targetGameObject.GetComponent <Animation>(); if (animation != null) { animation.Play(); } else { Debug.LogWarning("Couldn't find an Animation or Animatopr on the target GameObject"); } } break; case EventResponseType.STOP_ANIMATION: animation = action.targetGameObject.GetComponent <Animation>(); if (animation != null) { animation.Stop(); } else { Debug.LogWarning("Couldn't find an Animation or Animator on the target GameObject"); } break; } }
/// <summary> /// Applies the filters. /// </summary> /// <returns><c>true</c>, if filtering was passed, <c>false</c> otherwise.</returns> /// <param name="action">Action.</param> /// <param name="args">Arguments.</param> virtual protected bool ApplyFilters(EventResponse action, System.EventArgs args) { return(true); }
/// <summary> /// Draws an event response action in the inspector. /// </summary> /// <param name="action">Action.</param> public static void RenderAction(object target, object repsonder, EventResponse action) { if (!(target is EventResponder)) { Debug.LogWarning("Unexpected type passed to RenderAction()"); return; } GUILayout.Box("", GUILayout.ExpandWidth(true), GUILayout.Height(1)); if (action == null) { action = new EventResponse(); } EventResponse originalAction = new EventResponse(action); action.responseType = (EventResponseType)EditorGUILayout.EnumPopup(new GUIContent("Action Type", "The type of action to do when this event occurs."), action.responseType); // Delay action.delay = EditorGUILayout.FloatField(new GUIContent("Action Delay", "how long to wait before doing the action."), action.delay); if (action.delay < 0.0f) { action.delay = 0.0f; } else if (action.delay > 0.0f) { EditorGUILayout.HelpBox("If you use many events with delay you may notice some garbage collection issues on mobile devices", MessageType.Info); } // Game Object if (action.responseType == EventResponseType.ACTIVATE_GAMEOBJECT || action.responseType == EventResponseType.DEACTIVATE_GAMEOBJECT || action.responseType == EventResponseType.SEND_MESSSAGE) { action.targetGameObject = (GameObject)EditorGUILayout.ObjectField(new GUIContent("Game Object", "The game object that will be acted on"), action.targetGameObject, typeof(GameObject), true); } // Component if (action.responseType == EventResponseType.ENABLE_BEHAVIOUR || action.responseType == EventResponseType.DISABLE_BEHAVIOUR) { action.targetComponent = (Component)EditorGUILayout.ObjectField(new GUIContent("Behaviour", "The behaviour will be acted on"), action.targetComponent, typeof(Component), true); } // Particle system if (action.responseType == EventResponseType.PLAY_PARTICLES || action.responseType == EventResponseType.PAUSE_PARTICLES) { action.targetComponent = (Component)EditorGUILayout.ObjectField(new GUIContent("Particle System", "The particle system that will be acted on"), action.targetComponent, typeof(ParticleSystem), true); } // Send message if (action.responseType == EventResponseType.SEND_MESSSAGE) { action.message = EditorGUILayout.TextField(new GUIContent("Message", "The message to send via send message"), action.message); } // Sprite if (action.responseType == EventResponseType.SWITCH_SPRITE) { action.targetComponent = (Component)EditorGUILayout.ObjectField(new GUIContent("Sprite Renderer", "SpriteRenderer to update."), action.targetComponent, typeof(SpriteRenderer), true); action.newSprite = (Sprite)EditorGUILayout.ObjectField(new GUIContent("New Sprite", "Sprite to switch in."), action.newSprite, typeof(Sprite), true); } // Load level if (action.responseType == EventResponseType.LOAD_SCENE) { action.message = EditorGUILayout.TextField(new GUIContent("Scene Name", "The name of the scene to load (make sure its added to the build settings)."), action.message); } // Effects if (action.responseType == EventResponseType.START_EFFECT) { action.targetComponent = (Component)EditorGUILayout.ObjectField(new GUIContent("Effect", "The effect that will be played."), action.targetComponent, typeof(FX_Base), true); action.targetGameObject = (GameObject)EditorGUILayout.ObjectField(new GUIContent("Callback Object", "The game object that will be called back when the effect is finished"), action.targetGameObject, typeof(GameObject), true); if (action.targetComponent != null && action.targetGameObject != null) { action.message = EditorGUILayout.TextField(new GUIContent("Callback Message", "The name message to send on call back."), action.message); EditorGUILayout.HelpBox("Note that many effects do not support call backs.", MessageType.Info); } } // Animations if (action.responseType == EventResponseType.PLAY_ANIMATION || action.responseType == EventResponseType.STOP_ANIMATION) { action.targetGameObject = (GameObject)EditorGUILayout.ObjectField(new GUIContent("Animator", "GameObject holding an Animation of Animator to play or stop."), action.targetGameObject, typeof(GameObject), true); } // Animation state if (action.responseType == EventResponseType.PLAY_ANIMATION && action.targetGameObject != null) { Animator animator = action.targetGameObject.GetComponent <Animator>(); if (animator != null) { action.message = EditorGUILayout.TextField(new GUIContent("Animation State", "Name of the Animation State to play."), action.message); } } // Animation state if (action.responseType == EventResponseType.STOP_ANIMATION) { Animator animator = action.targetGameObject.GetComponent <Animator>(); if (animator != null) { EditorGUILayout.HelpBox("You cannot stop an Animator only an Animation. Instead use PLAY_ANIMATION and provide an IDLE or DEFAULT state", MessageType.Warning); } } if (!action.Equals(originalAction)) { if (target is EventResponder) { EditorUtility.SetDirty((EventResponder)target); } } // Remove EditorGUILayout.BeginHorizontal(); GUILayout.FlexibleSpace(); if (GUILayout.Button("Remove Action")) { if (target is EventResponder) { ((EventResponder)target).actions = ((EventResponder)target).actions.Where(a => a != action).ToArray(); } } EditorGUILayout.EndHorizontal(); }