FSetActiveEvent.cs 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. using UnityEngine;
  2. using System.Collections;
  3. namespace Flux
  4. {
  5. //[FEvent("Game Object/Set Active")]
  6. public class FSetActiveEvent : FEvent
  7. {
  8. [SerializeField]
  9. private bool _active = true;
  10. [SerializeField]
  11. [Tooltip("Does the event set the opposite on the last frame?")]
  12. private bool _setOppositeOnFinish = true;
  13. private bool _wasActive = false;
  14. private GameObject _ownerGO = null;
  15. protected override void OnTrigger( float timeSinceTrigger )
  16. {
  17. if( _ownerGO == null )
  18. {
  19. _ownerGO = Owner.gameObject;
  20. _wasActive = _ownerGO.activeSelf;
  21. }
  22. // _ownerGO.SetActive( _active ); <- not needed since it will be handled OnUpdateEvent
  23. }
  24. protected override void OnUpdateEvent( float timeSinceTrigger )
  25. {
  26. if( _ownerGO.activeSelf != _active )
  27. _ownerGO.SetActive( _active );
  28. }
  29. protected override void OnFinish()
  30. {
  31. if( _setOppositeOnFinish )
  32. _ownerGO.SetActive( !_active );
  33. }
  34. protected override void OnStop ()
  35. {
  36. Owner.gameObject.SetActive( _wasActive );
  37. }
  38. }
  39. }