You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

130 lines
3.5 KiB

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEditor;
  5. namespace AmplifyShaderEditor
  6. {
  7. [CustomEditor( typeof( Texture2DArray ) )]
  8. public class CustomTexture2DArrayInspector : Editor
  9. {
  10. Texture2DArray m_target;
  11. [SerializeField]
  12. float m_index;
  13. Shader m_textureArrayPreview;
  14. Material m_previewMaterial;
  15. GUIStyle slider = null;
  16. GUIStyle thumb = null;
  17. GUIContent m_allButton = null;
  18. [SerializeField]
  19. bool m_seeAll;
  20. void OnEnable()
  21. {
  22. m_target = ( target as Texture2DArray );
  23. m_textureArrayPreview = AssetDatabase.LoadAssetAtPath<Shader>( AssetDatabase.GUIDToAssetPath( "610c24aad350fba4583068c6c22fa428" ) );
  24. m_previewMaterial = new Material( m_textureArrayPreview );
  25. slider = null;
  26. thumb = null;
  27. }
  28. public override void OnPreviewGUI( Rect r, GUIStyle background )
  29. {
  30. base.OnPreviewGUI( r, background );
  31. m_previewMaterial.SetTexture( "_MainTex", m_target );
  32. m_previewMaterial.SetFloat( "_Index", m_index );
  33. EditorGUI.DrawPreviewTexture( r, m_target, m_previewMaterial, ScaleMode.ScaleToFit, 1f );
  34. }
  35. private void OnDisable()
  36. {
  37. DestroyImmediate( m_previewMaterial );
  38. m_previewMaterial = null;
  39. }
  40. public override void OnInspectorGUI()
  41. {
  42. if( slider == null )
  43. slider = "preSlider";
  44. if( thumb == null )
  45. thumb = "preSliderThumb";
  46. if( m_allButton == null )
  47. m_allButton = EditorGUIUtility.IconContent( "PreTextureMipMapLow" );
  48. base.OnInspectorGUI();
  49. }
  50. public override bool HasPreviewGUI()
  51. {
  52. return true;
  53. }
  54. public override void OnPreviewSettings()
  55. {
  56. base.OnPreviewSettings();
  57. m_seeAll = GUILayout.Toggle( m_seeAll, m_allButton, "preButton" );
  58. EditorGUI.BeginDisabledGroup( m_seeAll );
  59. m_index = Mathf.Round( GUILayout.HorizontalSlider( m_index, 0, m_target.depth - 1, slider, thumb ) );
  60. EditorGUI.EndDisabledGroup();
  61. }
  62. public override void OnInteractivePreviewGUI( Rect r, GUIStyle background )
  63. {
  64. //base.OnInteractivePreviewGUI( r, background );
  65. if( m_seeAll )
  66. {
  67. int columns = Mathf.CeilToInt( Mathf.Sqrt( m_target.depth ) );
  68. float sizeX = r.width / columns - 20;
  69. float centerY = ( columns * columns ) - m_target.depth;
  70. int rows = columns;
  71. if( centerY >= columns )
  72. rows--;
  73. float sizeY = ( r.height - 16 ) / rows - 15;
  74. if( centerY >= columns )
  75. centerY = sizeY * 0.5f;
  76. else
  77. centerY = 0;
  78. Rect smallRect = r;
  79. if( rows > 1 )
  80. smallRect.y += ( 15 / ( rows - 1 ) );
  81. else
  82. smallRect.y += 15;
  83. smallRect.x = r.x + 10;
  84. smallRect.width = sizeX;
  85. smallRect.height = sizeY;
  86. for( int i = 0; i < m_target.depth; i++ )
  87. {
  88. m_previewMaterial.SetTexture( "_MainTex", m_target );
  89. m_previewMaterial.SetFloat( "_Index", i );
  90. EditorGUI.DrawPreviewTexture( smallRect, m_target, m_previewMaterial, ScaleMode.ScaleToFit, 1 );
  91. Rect dropRect = smallRect;
  92. float diff = smallRect.height - smallRect.width;
  93. if( diff > 0 )
  94. dropRect.y -= diff * 0.5f;
  95. dropRect.y += 16;
  96. EditorGUI.DropShadowLabel( dropRect, "[" + i + "]" );
  97. smallRect.x += sizeX + 20;
  98. if( ( ( i + 1 ) % ( columns ) ) == 0 )
  99. {
  100. smallRect.x = r.x + 10;
  101. smallRect.height = sizeY;
  102. smallRect.y += sizeY + 30;
  103. }
  104. }
  105. }
  106. else
  107. {
  108. m_previewMaterial.SetTexture( "_MainTex", m_target );
  109. m_previewMaterial.SetFloat( "_Index", m_index );
  110. EditorGUI.DrawPreviewTexture( r, m_target, m_previewMaterial, ScaleMode.ScaleToFit, 1f );
  111. EditorGUI.DropShadowLabel( r, "[" + m_index + "]" );
  112. }
  113. }
  114. }
  115. }