SRDependencyServiceBase`1.cs 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. using System;
  2. using System.Collections;
  3. using System.Diagnostics;
  4. using UnityEngine;
  5. namespace SRF.Service
  6. {
  7. public abstract class SRDependencyServiceBase<T> : SRServiceBase<T>, IAsyncService where T : class
  8. {
  9. protected abstract Type[] Dependencies { get; }
  10. public bool IsLoaded
  11. {
  12. get
  13. {
  14. return this._isLoaded;
  15. }
  16. }
  17. [Conditional("ENABLE_LOGGING")]
  18. private void Log(string msg, UnityEngine.Object target)
  19. {
  20. UnityEngine.Debug.Log(msg, target);
  21. }
  22. protected override void Start()
  23. {
  24. base.Start();
  25. base.StartCoroutine(this.LoadDependencies());
  26. }
  27. protected virtual void OnLoaded()
  28. {
  29. }
  30. private IEnumerator LoadDependencies()
  31. {
  32. SRServiceManager.LoadingCount++;
  33. foreach (Type d in this.Dependencies)
  34. {
  35. bool hasService = SRServiceManager.HasService(d);
  36. if (!hasService)
  37. {
  38. object service = SRServiceManager.GetService(d);
  39. if (service == null)
  40. {
  41. UnityEngine.Debug.LogError("[Service] Could not resolve dependency ({0})".Fmt(new object[]
  42. {
  43. d.Name
  44. }));
  45. base.enabled = false;
  46. yield break;
  47. }
  48. IAsyncService a = service as IAsyncService;
  49. if (a != null)
  50. {
  51. while (!a.IsLoaded)
  52. {
  53. yield return new WaitForEndOfFrame();
  54. }
  55. }
  56. }
  57. }
  58. this._isLoaded = true;
  59. SRServiceManager.LoadingCount--;
  60. this.OnLoaded();
  61. yield break;
  62. }
  63. private bool _isLoaded;
  64. }
  65. }