FollowerBase.cs 901 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public abstract class FollowerBase : MonoBehaviour
  5. {
  6. [SerializeField]
  7. private float followTime = 2;
  8. private Coroutine followCoroutine;
  9. public abstract void SetTransform(Transform transform);
  10. protected virtual void OnEnable()
  11. {
  12. followCoroutine = StartCoroutine(Following(followTime));
  13. }
  14. protected virtual void OnDisable()
  15. {
  16. if (followCoroutine != null)
  17. {
  18. StopCoroutine(followCoroutine);
  19. }
  20. }
  21. IEnumerator Following(float time)
  22. {
  23. while (true)
  24. {
  25. yield return new WaitForSeconds(time);
  26. yield return null;
  27. if (SvrManager.Instance && SvrManager.Instance.IsRunning)
  28. {
  29. Follow();
  30. }
  31. }
  32. }
  33. protected abstract void Follow();
  34. }