IsVisible.cs 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. using UnityEngine;
  2. namespace BehaviorDesigner.Runtime.Tasks.Unity.UnityRenderer
  3. {
  4. [TaskCategory("Unity/Renderer")]
  5. [TaskDescription("Returns Success if the Renderer is visible, otherwise Failure.")]
  6. public class IsVisible : Conditional
  7. {
  8. [Tooltip("The GameObject that the task operates on. If null the task GameObject is used.")]
  9. public SharedGameObject targetGameObject;
  10. // cache the renderer component
  11. private Renderer renderer;
  12. private GameObject prevGameObject;
  13. public override void OnStart()
  14. {
  15. var currentGameObject = GetDefaultGameObject(targetGameObject.Value);
  16. if (currentGameObject != prevGameObject) {
  17. renderer = currentGameObject.GetComponent<Renderer>();
  18. prevGameObject = currentGameObject;
  19. }
  20. }
  21. public override TaskStatus OnUpdate()
  22. {
  23. if (renderer == null) {
  24. Debug.LogWarning("Renderer is null");
  25. return TaskStatus.Failure;
  26. }
  27. return renderer.isVisible ? TaskStatus.Success : TaskStatus.Failure;
  28. }
  29. public override void OnReset()
  30. {
  31. targetGameObject = null;
  32. }
  33. }
  34. }