TestHome.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. using UnityEngine.SceneManagement;
  2. using UnityEngine.UI;
  3. using UnityEngine;
  4. #if(UNITY_2018_3_OR_NEWER && UNITY_ANDROID)
  5. using UnityEngine.Android;
  6. #endif
  7. using System.Collections;
  8. /// <summary>
  9. /// TestHome serves a game controller object for this application.
  10. /// </summary>
  11. public class TestHome : MonoBehaviour
  12. {
  13. // Use this for initialization
  14. #if (UNITY_2018_3_OR_NEWER && UNITY_ANDROID)
  15. private ArrayList permissionList = new ArrayList();
  16. #endif
  17. static TestHelloUnityVideo app = null;
  18. private string HomeSceneName = "SceneHome";
  19. private string PlaySceneName = "SceneHelloVideo";
  20. // PLEASE KEEP THIS App ID IN SAFE PLACE
  21. // Get your own App ID at https://dashboard.agora.io/
  22. [SerializeField]
  23. private string AppID = "your_appid";
  24. void Awake()
  25. {
  26. #if (UNITY_2018_3_OR_NEWER && UNITY_ANDROID)
  27. permissionList.Add(Permission.Microphone);
  28. permissionList.Add(Permission.Camera);
  29. #endif
  30. // keep this alive across scenes
  31. DontDestroyOnLoad(this.gameObject);
  32. }
  33. void Start()
  34. {
  35. CheckAppId();
  36. }
  37. void Update()
  38. {
  39. CheckPermissions();
  40. }
  41. private void CheckAppId()
  42. {
  43. Debug.Assert(AppID.Length > 10, "Please fill in your AppId first on Game Controller object.");
  44. GameObject go = GameObject.Find("AppIDText");
  45. if (go != null)
  46. {
  47. Text appIDText = go.GetComponent<Text>();
  48. if (appIDText != null)
  49. {
  50. if (string.IsNullOrEmpty(AppID))
  51. {
  52. appIDText.text = "AppID: " + "UNDEFINED!";
  53. }
  54. else
  55. {
  56. appIDText.text = "AppID: " + AppID.Substring(0, 4) + "********" + AppID.Substring(AppID.Length - 4, 4);
  57. }
  58. }
  59. }
  60. }
  61. /// <summary>
  62. /// Checks for platform dependent permissions.
  63. /// </summary>
  64. private void CheckPermissions()
  65. {
  66. #if (UNITY_2018_3_OR_NEWER && UNITY_ANDROID)
  67. foreach(string permission in permissionList)
  68. {
  69. if (!Permission.HasUserAuthorizedPermission(permission))
  70. {
  71. Permission.RequestUserPermission(permission);
  72. }
  73. }
  74. #endif
  75. }
  76. public void onJoinButtonClicked()
  77. {
  78. // get parameters (channel name, channel profile, etc.)
  79. GameObject go = GameObject.Find("ChannelName");
  80. InputField field = go.GetComponent<InputField>();
  81. // create app if nonexistent
  82. if (ReferenceEquals(app, null))
  83. {
  84. app = new TestHelloUnityVideo(); // create app
  85. app.loadEngine(AppID); // load engine
  86. }
  87. // join channel and jump to next scene
  88. app.join(field.text);
  89. SceneManager.sceneLoaded += OnLevelFinishedLoading; // configure GameObject after scene is loaded
  90. SceneManager.LoadScene(PlaySceneName, LoadSceneMode.Single);
  91. }
  92. public void onLeaveButtonClicked()
  93. {
  94. if (!ReferenceEquals(app, null))
  95. {
  96. app.leave(); // leave channel
  97. app.unloadEngine(); // delete engine
  98. app = null; // delete app
  99. SceneManager.LoadScene(HomeSceneName, LoadSceneMode.Single);
  100. }
  101. Destroy(gameObject);
  102. }
  103. public void OnLevelFinishedLoading(Scene scene, LoadSceneMode mode)
  104. {
  105. if (scene.name == PlaySceneName)
  106. {
  107. if (!ReferenceEquals(app, null))
  108. {
  109. app.onSceneHelloVideoLoaded(); // call this after scene is loaded
  110. }
  111. SceneManager.sceneLoaded -= OnLevelFinishedLoading;
  112. }
  113. }
  114. void OnApplicationPause(bool paused)
  115. {
  116. if (!ReferenceEquals(app, null))
  117. {
  118. app.EnableVideo(paused);
  119. }
  120. }
  121. void OnApplicationQuit()
  122. {
  123. if (!ReferenceEquals(app, null))
  124. {
  125. app.unloadEngine();
  126. }
  127. }
  128. }