AgoraCallbackObject.cs 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. #if UNITY_EDITOR_WIN || UNITY_EDITOR_OSX || UNITY_STANDALONE_WIN || UNITY_STANDALONE_OSX || UNITY_IOS || UNITY_ANDROID
  2. using UnityEngine;
  3. using Object = UnityEngine.Object;
  4. namespace Agora.Rtc
  5. {
  6. internal sealed class AgoraCallbackObject
  7. {
  8. private GameObject _CallbackGameObject { get; set; }
  9. internal AgoraCallbackQueue _CallbackQueue { set; get; }
  10. private string GameObjectName { set; get; }
  11. internal AgoraCallbackObject(string gameObjectName)
  12. {
  13. GameObjectName = gameObjectName;
  14. InitGameObject(gameObjectName);
  15. }
  16. internal void Release()
  17. {
  18. DeInitGameObject(GameObjectName);
  19. }
  20. private void InitGameObject(string gameObjectName)
  21. {
  22. DeInitGameObject(gameObjectName);
  23. _CallbackGameObject = new GameObject(gameObjectName);
  24. _CallbackQueue = _CallbackGameObject.AddComponent<AgoraCallbackQueue>();
  25. Object.DontDestroyOnLoad(_CallbackGameObject);
  26. _CallbackGameObject.hideFlags = HideFlags.HideInHierarchy;
  27. }
  28. private void DeInitGameObject(string gameObjectName)
  29. {
  30. var gameObject = GameObject.Find(gameObjectName);
  31. if (!ReferenceEquals(gameObject, null))
  32. {
  33. AgoraCallbackQueue callbackQueue = gameObject.GetComponent<AgoraCallbackQueue>();
  34. if (!ReferenceEquals(callbackQueue, null))
  35. {
  36. callbackQueue.ClearQueue();
  37. }
  38. Object.Destroy(gameObject);
  39. }
  40. }
  41. }
  42. }
  43. #endif