SampleApp_Multiple.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. namespace RenderHeads.Media.AVProVideo.Demos
  5. {
  6. public class SampleApp_Multiple : MonoBehaviour
  7. {
  8. private void Update()
  9. {
  10. foreach (DisplayUGUI displayUGUI in this.m_aAddedVideos)
  11. {
  12. if (displayUGUI.gameObject != null && !displayUGUI.gameObject.activeSelf && displayUGUI._mediaPlayer != null && displayUGUI._mediaPlayer.Control != null && displayUGUI._mediaPlayer.Control.IsPlaying())
  13. {
  14. displayUGUI.gameObject.SetActive(true);
  15. }
  16. }
  17. }
  18. private void UpdateVideosLayout()
  19. {
  20. GameObject gameObject = GameObject.Find("Canvas/Panel");
  21. RectTransform rectTransform = (!gameObject) ? null : gameObject.GetComponent<RectTransform>();
  22. if (rectTransform)
  23. {
  24. Vector2 sizeDelta = rectTransform.sizeDelta;
  25. Vector2 vector = new Vector2(sizeDelta.x * 0.5f, sizeDelta.y * 0.5f);
  26. int count = this.m_aAddedVideos.Count;
  27. int num = Mathf.CeilToInt(Mathf.Sqrt((float)count));
  28. float num2 = 1f / (float)num * sizeDelta.x;
  29. float num3 = 1f / (float)num * sizeDelta.y;
  30. for (int i = 0; i < count; i++)
  31. {
  32. DisplayUGUI displayUGUI = this.m_aAddedVideos[i];
  33. int num4 = i % num;
  34. int num5 = i / num;
  35. displayUGUI.rectTransform.anchoredPosition = new Vector2(num2 * (float)num4 - vector.x, num3 * (float)(-(float)num5) + vector.y);
  36. displayUGUI.rectTransform.sizeDelta = new Vector2(num2, num3);
  37. }
  38. }
  39. }
  40. public void AddVideoClicked()
  41. {
  42. this.m_NumVideosAdded++;
  43. GameObject gameObject = new GameObject("AVPro MediaPlayer " + this.m_NumVideosAdded.ToString());
  44. MediaPlayer mediaPlayer = gameObject.AddComponent<MediaPlayer>();
  45. mediaPlayer.m_VideoPath = this.m_videoPath;
  46. mediaPlayer.m_AutoStart = true;
  47. mediaPlayer.m_Loop = true;
  48. mediaPlayer.SetGuiPositionFromVideoIndex(this.m_NumVideosAdded - 1);
  49. mediaPlayer.SetDebugGuiEnabled(this.m_NumVideosAdded < 5);
  50. GameObject gameObject2 = GameObject.Find("Canvas/Panel");
  51. if (gameObject2 != null)
  52. {
  53. GameObject gameObject3 = new GameObject("AVPro Video uGUI " + this.m_NumVideosAdded.ToString());
  54. gameObject3.transform.parent = gameObject2.transform;
  55. gameObject3.SetActive(false);
  56. gameObject3.AddComponent<RectTransform>();
  57. gameObject3.AddComponent<CanvasRenderer>();
  58. DisplayUGUI displayUGUI = gameObject3.AddComponent<DisplayUGUI>();
  59. displayUGUI._mediaPlayer = mediaPlayer;
  60. displayUGUI._scaleMode = ScaleMode.StretchToFill;
  61. displayUGUI.rectTransform.localScale = Vector3.one;
  62. displayUGUI.rectTransform.pivot = new Vector2(0f, 1f);
  63. this.m_aAddedVideos.Add(displayUGUI);
  64. this.UpdateVideosLayout();
  65. }
  66. }
  67. public void RemoveVideoClicked()
  68. {
  69. if (this.m_aAddedVideos.Count > 0)
  70. {
  71. int index = UnityEngine.Random.Range(0, this.m_aAddedVideos.Count);
  72. DisplayUGUI displayUGUI = this.m_aAddedVideos[index];
  73. if (displayUGUI._mediaPlayer != null)
  74. {
  75. displayUGUI._mediaPlayer.CloseVideo();
  76. UnityEngine.Object.Destroy(displayUGUI._mediaPlayer.gameObject);
  77. displayUGUI._mediaPlayer = null;
  78. }
  79. UnityEngine.Object.Destroy(displayUGUI.gameObject);
  80. this.m_aAddedVideos.RemoveAt(index);
  81. this.m_NumVideosAdded--;
  82. }
  83. }
  84. private void OnDestroy()
  85. {
  86. foreach (DisplayUGUI displayUGUI in this.m_aAddedVideos)
  87. {
  88. if (displayUGUI._mediaPlayer)
  89. {
  90. displayUGUI._mediaPlayer = null;
  91. }
  92. }
  93. this.m_aAddedVideos.Clear();
  94. }
  95. public string m_videoPath = "BigBuckBunny_360p30.mp4";
  96. private int m_NumVideosAdded;
  97. private List<DisplayUGUI> m_aAddedVideos = new List<DisplayUGUI>();
  98. }
  99. }