SRSceneServiceBase.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. //#define ENABLE_LOGGING
  2. namespace SRF.Service
  3. {
  4. using System.Collections;
  5. using System.Diagnostics;
  6. using UnityEngine;
  7. using Debug = UnityEngine.Debug;
  8. public abstract class SRSceneServiceBase<T, TImpl> : SRServiceBase<T>, IAsyncService
  9. where T : class
  10. where TImpl : Component
  11. {
  12. private TImpl _rootObject;
  13. /// <summary>
  14. /// Name of the scene this service's contents are within
  15. /// </summary>
  16. protected abstract string SceneName { get; }
  17. /// <summary>
  18. /// Scene contents root object
  19. /// </summary>
  20. protected TImpl RootObject
  21. {
  22. get { return _rootObject; }
  23. }
  24. public bool IsLoaded
  25. {
  26. get { return _rootObject != null; }
  27. }
  28. [Conditional("ENABLE_LOGGING")]
  29. private void Log(string msg, Object target)
  30. {
  31. //#if ENABLE_LOGGING
  32. Debug.Log(msg, target);
  33. //#endif
  34. }
  35. protected override void Start()
  36. {
  37. base.Start();
  38. StartCoroutine(LoadCoroutine());
  39. }
  40. protected override void OnDestroy()
  41. {
  42. if (IsLoaded)
  43. {
  44. Destroy(_rootObject.gameObject);
  45. }
  46. base.OnDestroy();
  47. }
  48. protected virtual void OnLoaded() {}
  49. private IEnumerator LoadCoroutine()
  50. {
  51. if (_rootObject != null)
  52. {
  53. yield break;
  54. }
  55. SRServiceManager.LoadingCount++;
  56. #if UNITY_4_6 || UNITY_4_7 || UNITY_5_0 || UNITY_5_1 || UNITY_5_2
  57. if (Application.loadedLevelName == SceneName)
  58. #else
  59. if (UnityEngine.SceneManagement.SceneManager.GetSceneByName(SceneName).isLoaded)
  60. #endif
  61. {
  62. Log("[Service] Already in service scene {0}. Searching for root object...".Fmt(SceneName), this);
  63. }
  64. else
  65. {
  66. Log("[Service] Loading scene ({0})".Fmt(SceneName), this);
  67. #if UNITY_PRO_LICENSE || UNITY_5 || UNITY_5_3_OR_NEWER
  68. #if UNITY_4_6 || UNITY_4_7 || UNITY_5_0 || UNITY_5_1 || UNITY_5_2
  69. yield return Application.LoadLevelAdditiveAsync(SceneName);
  70. #else
  71. yield return UnityEngine.SceneManagement.SceneManager.LoadSceneAsync(SceneName, UnityEngine.SceneManagement.LoadSceneMode.Additive);
  72. #endif
  73. #else
  74. Application.LoadLevelAdditive(SceneName);
  75. yield return new WaitForEndOfFrame();
  76. #endif
  77. Log("[Service] Scene loaded. Searching for root object...", this);
  78. }
  79. var go = GameObject.Find(SceneName);
  80. if (go == null)
  81. {
  82. goto Error;
  83. }
  84. var timpl = go.GetComponent<TImpl>();
  85. if (timpl == null)
  86. {
  87. goto Error;
  88. }
  89. _rootObject = timpl;
  90. _rootObject.transform.parent = CachedTransform;
  91. DontDestroyOnLoad(go);
  92. Debug.Log("[Service] Loading {0} complete. (Scene: {1})".Fmt(GetType().Name, SceneName), this);
  93. SRServiceManager.LoadingCount--;
  94. OnLoaded();
  95. yield break;
  96. Error:
  97. SRServiceManager.LoadingCount--;
  98. Debug.LogError("[Service] Root object ({0}) not found".Fmt(SceneName), this);
  99. enabled = false;
  100. }
  101. }
  102. }