/Assets/AVProVideo/Demos/Scripts/SampleApp_Multiple.cs

https://bitbucket.org/solaris_3diq/devweek2018_realsense · C# · 151 lines · 112 code · 25 blank · 14 comment · 22 complexity · 628f7b8c79113316b60268f4511417d1 MD5 · raw file

  1. #if UNITY_4_6 || UNITY_4_7 || UNITY_4_8 || UNITY_5 || UNITY_5_4_OR_NEWER
  2. #define UNITY_FEATURE_UGUI
  3. #endif
  4. using UnityEngine;
  5. #if UNITY_FEATURE_UGUI
  6. using UnityEngine.UI;
  7. using System.Collections;
  8. using System.Collections.Generic;
  9. //-----------------------------------------------------------------------------
  10. // Copyright 2015-2017 RenderHeads Ltd. All rights reserverd.
  11. //-----------------------------------------------------------------------------
  12. namespace RenderHeads.Media.AVProVideo.Demos
  13. {
  14. public class SampleApp_Multiple : MonoBehaviour
  15. {
  16. public string m_videoPath = "BigBuckBunny_360p30.mp4";
  17. private int m_NumVideosAdded = 0;
  18. List<DisplayUGUI> m_aAddedVideos = new List<DisplayUGUI>();
  19. void Update()
  20. {
  21. #if UNITY_ANDROID
  22. // To handle 'back' button on Android devices
  23. if (Input.GetKeyDown(KeyCode.Escape))
  24. {
  25. Application.Quit();
  26. }
  27. #endif
  28. foreach( DisplayUGUI gui in m_aAddedVideos )
  29. {
  30. if( gui.gameObject != null && gui.gameObject.activeSelf == false &&
  31. gui._mediaPlayer != null && gui._mediaPlayer.Control != null && ( gui._mediaPlayer.Control.IsPlaying() ) )
  32. {
  33. gui.gameObject.SetActive( true );
  34. }
  35. }
  36. }
  37. private void UpdateVideosLayout()
  38. {
  39. GameObject canvasPanel = GameObject.Find("Canvas/Panel");
  40. RectTransform canvasRectTransform = ( canvasPanel ) ? canvasPanel.GetComponent<RectTransform>() : null;
  41. if( canvasRectTransform )
  42. {
  43. Vector2 canvasSize = canvasRectTransform.sizeDelta;
  44. Vector2 halfCanvasSize = new Vector2( canvasSize.x * 0.5f, canvasSize.y * 0.5f );
  45. int iNumMovies = m_aAddedVideos.Count;
  46. int iNumColRows = Mathf.CeilToInt (Mathf.Sqrt( iNumMovies ));
  47. float fWidth = (1.0f / iNumColRows) * canvasSize.x;
  48. float fHeight = (1.0f / iNumColRows) * canvasSize.y;
  49. for( int i = 0; i < iNumMovies; ++i )
  50. {
  51. DisplayUGUI gui = m_aAddedVideos[ i ];
  52. int x = i % iNumColRows;
  53. int y = i / iNumColRows;
  54. gui.rectTransform.anchoredPosition = new Vector2( ((fWidth * x) - halfCanvasSize.x), ((fHeight * -y) + halfCanvasSize.y) );
  55. gui.rectTransform.sizeDelta = new Vector2( fWidth, fHeight );
  56. }
  57. }
  58. }
  59. public void AddVideoClicked()
  60. {
  61. ++m_NumVideosAdded;
  62. // New media player
  63. GameObject newMediaPlayerGameObject = new GameObject( "AVPro MediaPlayer " + m_NumVideosAdded.ToString() );
  64. MediaPlayer newMediaPlayer = newMediaPlayerGameObject.AddComponent<MediaPlayer>();
  65. newMediaPlayer.m_VideoPath = m_videoPath;
  66. newMediaPlayer.m_AutoStart = true;
  67. newMediaPlayer.m_Loop = true;
  68. newMediaPlayer.SetGuiPositionFromVideoIndex( m_NumVideosAdded - 1 );
  69. newMediaPlayer.SetDebugGuiEnabled( m_NumVideosAdded < 5 );
  70. // New uGUI object
  71. GameObject canvasPanel = GameObject.Find("Canvas/Panel");
  72. if( canvasPanel != null )
  73. {
  74. // New game object, and make it a child of the canvas panel
  75. GameObject newGuiGameObject = new GameObject( "AVPro Video uGUI " + m_NumVideosAdded.ToString() );
  76. newGuiGameObject.transform.parent = canvasPanel.transform;
  77. newGuiGameObject.SetActive( false );
  78. // Add the components
  79. newGuiGameObject.AddComponent<RectTransform>();
  80. newGuiGameObject.AddComponent<CanvasRenderer>();
  81. // Add the DisplayUGUI;
  82. DisplayUGUI newGuiObject = newGuiGameObject.AddComponent<DisplayUGUI>();
  83. newGuiObject._mediaPlayer = newMediaPlayer;
  84. newGuiObject._scaleMode = ScaleMode.StretchToFill;
  85. newGuiObject.rectTransform.localScale = Vector3.one;
  86. newGuiObject.rectTransform.pivot = new Vector2( 0.0f, 1.0f );
  87. m_aAddedVideos.Add( newGuiObject );
  88. // Update layout
  89. UpdateVideosLayout();
  90. }
  91. }
  92. public void RemoveVideoClicked()
  93. {
  94. if (m_aAddedVideos.Count > 0)
  95. {
  96. // Pick a random element to remove
  97. int index = Random.Range(0, m_aAddedVideos.Count);
  98. DisplayUGUI gui = m_aAddedVideos[index];
  99. // Stop and destroy the MediaPlayer
  100. if (gui._mediaPlayer != null)
  101. {
  102. gui._mediaPlayer.CloseVideo();
  103. GameObject.Destroy(gui._mediaPlayer.gameObject);
  104. gui._mediaPlayer = null;
  105. }
  106. // Destroy the Display object
  107. GameObject.Destroy(gui.gameObject);
  108. // Update variables
  109. m_aAddedVideos.RemoveAt(index);
  110. m_NumVideosAdded--;
  111. }
  112. }
  113. void OnDestroy()
  114. {
  115. foreach( DisplayUGUI gui in m_aAddedVideos )
  116. {
  117. if( gui._mediaPlayer )
  118. {
  119. gui._mediaPlayer = null;
  120. }
  121. }
  122. m_aAddedVideos.Clear();
  123. }
  124. }
  125. }
  126. #endif