DisplayUGUIEditor.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. // UnityEngine.UI was moved to a package in 2019.2.0
  2. // Unfortunately no way to test for this across all Unity versions yet
  3. // You can set up the asmdef to reference the new package, but the package doesn't
  4. // existing in Unity 2017 etc, and it throws an error due to missing reference
  5. #define AVPRO_PACKAGE_UNITYUI
  6. #if (UNITY_2019_2_OR_NEWER && AVPRO_PACKAGE_UNITYUI) || (!UNITY_2019_2_OR_NEWER)
  7. using UnityEngine;
  8. using UnityEditor;
  9. using UnityEditor.UI;
  10. //-----------------------------------------------------------------------------
  11. // Copyright 2015-2021 RenderHeads Ltd. All rights reserved.
  12. //-----------------------------------------------------------------------------
  13. namespace RenderHeads.Media.AVProVideo.Editor
  14. {
  15. /// <summary>
  16. /// Editor for the DisplayUGUI component
  17. /// </summary>
  18. [CustomEditor(typeof(DisplayUGUI), true)]
  19. [CanEditMultipleObjects]
  20. public class DisplayUGUIEditor : GraphicEditor
  21. {
  22. // Note we have precedence for calling rectangle for just rect, even in the Inspector.
  23. // For example in the Camera component's Viewport Rect.
  24. // Hence sticking with Rect here to be consistent with corresponding property in the API.
  25. private static readonly GUIContent m_guiTextUVRectContent = new GUIContent("UV Rect");
  26. private SerializedProperty _propMediaPlayer;
  27. private SerializedProperty _propUVRect;
  28. private SerializedProperty _propDefaultTexture;
  29. private SerializedProperty _propNoDefaultDisplay;
  30. private SerializedProperty _propDisplayInEditor;
  31. private SerializedProperty _propSetNativeSize;
  32. private SerializedProperty _propScaleMode;
  33. [MenuItem("GameObject/UI/AVPro Video uGUI", false, 0)]
  34. public static void CreateGameObject()
  35. {
  36. GameObject parent = Selection.activeGameObject;
  37. RectTransform parentCanvasRenderer = ( parent != null ) ? parent.GetComponent<RectTransform>() : null;
  38. if( parentCanvasRenderer )
  39. {
  40. GameObject go = new GameObject("AVPro Video");
  41. go.transform.SetParent(parent.transform, false);
  42. go.AddComponent<RectTransform>();
  43. go.AddComponent<CanvasRenderer>();
  44. go.AddComponent<DisplayUGUI>();
  45. Selection.activeGameObject = go;
  46. }
  47. else
  48. {
  49. EditorUtility.DisplayDialog("AVPro Video", "You must make the AVPro Video uGUI object as a child of a Canvas.", "Ok");
  50. }
  51. }
  52. public override bool RequiresConstantRepaint()
  53. {
  54. DisplayUGUI displayComponent = target as DisplayUGUI;
  55. return (displayComponent != null && displayComponent.HasValidTexture());
  56. }
  57. protected override void OnEnable()
  58. {
  59. base.OnEnable();
  60. _propMediaPlayer = this.CheckFindProperty("_mediaPlayer");
  61. _propUVRect = this.CheckFindProperty("_uvRect");
  62. _propSetNativeSize = this.CheckFindProperty("_setNativeSize");
  63. _propScaleMode = this.CheckFindProperty("_scaleMode");
  64. _propNoDefaultDisplay = this.CheckFindProperty("_noDefaultDisplay");
  65. _propDisplayInEditor = this.CheckFindProperty("_displayInEditor");
  66. _propDefaultTexture = this.CheckFindProperty("_defaultTexture");
  67. SetShowNativeSize(true);
  68. }
  69. public override void OnInspectorGUI()
  70. {
  71. serializedObject.Update();
  72. EditorGUILayout.PropertyField(_propMediaPlayer);
  73. EditorGUILayout.PropertyField(_propDisplayInEditor);
  74. EditorGUILayout.PropertyField(_propNoDefaultDisplay);
  75. if (!_propNoDefaultDisplay.boolValue)
  76. {
  77. EditorGUILayout.PropertyField(_propDefaultTexture);
  78. }
  79. AppearanceControlsGUI();
  80. RaycastControlsGUI();
  81. EditorGUILayout.PropertyField(_propUVRect, m_guiTextUVRectContent);
  82. EditorGUILayout.PropertyField(_propSetNativeSize);
  83. EditorGUILayout.PropertyField(_propScaleMode);
  84. SetShowNativeSize(false);
  85. NativeSizeButtonGUI();
  86. serializedObject.ApplyModifiedProperties();
  87. }
  88. private void SetShowNativeSize(bool instant)
  89. {
  90. base.SetShowNativeSize(_propMediaPlayer.objectReferenceValue != null, instant);
  91. }
  92. /// <summary>
  93. /// Allow the texture to be previewed.
  94. /// </summary>
  95. public override bool HasPreviewGUI()
  96. {
  97. DisplayUGUI rawImage = target as DisplayUGUI;
  98. return rawImage != null;
  99. }
  100. /// <summary>
  101. /// Draw the Image preview.
  102. /// </summary>
  103. public override void OnPreviewGUI(Rect drawArea, GUIStyle background)
  104. {
  105. DisplayUGUI rawImage = target as DisplayUGUI;
  106. Texture tex = rawImage.mainTexture;
  107. if (tex == null)
  108. return;
  109. // Create the texture rectangle that is centered inside rect.
  110. Rect outerRect = drawArea;
  111. Matrix4x4 m = GUI.matrix;
  112. // Flip the image vertically
  113. if (rawImage.HasValidTexture())
  114. {
  115. if (rawImage.Player.TextureProducer.RequiresVerticalFlip())
  116. {
  117. GUIUtility.ScaleAroundPivot(new Vector2(1f, -1f), new Vector2(0f, outerRect.y + (outerRect.height / 2f)));
  118. }
  119. }
  120. EditorGUI.DrawTextureTransparent(outerRect, tex, ScaleMode.ScaleToFit);//, outer.width / outer.height);
  121. //SpriteDrawUtility.DrawSprite(tex, rect, outer, rawImage.uvRect, rawImage.canvasRenderer.GetColor());
  122. GUI.matrix = m;
  123. }
  124. /// <summary>
  125. /// Info String drawn at the bottom of the Preview
  126. /// </summary>
  127. public override string GetInfoString()
  128. {
  129. DisplayUGUI rawImage = target as DisplayUGUI;
  130. string text = string.Empty;
  131. if (rawImage.HasValidTexture())
  132. {
  133. text += string.Format("Video Size: {0}x{1}\n",
  134. Mathf.RoundToInt(Mathf.Abs(rawImage.mainTexture.width)),
  135. Mathf.RoundToInt(Mathf.Abs(rawImage.mainTexture.height)));
  136. }
  137. // Image size Text
  138. text += string.Format("Display Size: {0}x{1}",
  139. Mathf.RoundToInt(Mathf.Abs(rawImage.rectTransform.rect.width)),
  140. Mathf.RoundToInt(Mathf.Abs(rawImage.rectTransform.rect.height)));
  141. return text;
  142. }
  143. }
  144. }
  145. #endif