Нет описания
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. namespace UnityEditor.UI
  4. {
  5. [CustomEditor(typeof(CanvasScaler), true)]
  6. [CanEditMultipleObjects]
  7. /// <summary>
  8. /// Custom Editor for the CanvasScaler component.
  9. /// Extend this class to write a custom editor for a component derived from CanvasScaler.
  10. /// </summary>
  11. public class CanvasScalerEditor : Editor
  12. {
  13. SerializedProperty m_UiScaleMode;
  14. SerializedProperty m_ScaleFactor;
  15. SerializedProperty m_ReferenceResolution;
  16. SerializedProperty m_ScreenMatchMode;
  17. SerializedProperty m_MatchWidthOrHeight;
  18. SerializedProperty m_PhysicalUnit;
  19. SerializedProperty m_FallbackScreenDPI;
  20. SerializedProperty m_DefaultSpriteDPI;
  21. SerializedProperty m_DynamicPixelsPerUnit;
  22. SerializedProperty m_ReferencePixelsPerUnit;
  23. SerializedProperty m_PresetInfoIsWorld;
  24. const int kSliderEndpointLabelsHeight = 12;
  25. private class Styles
  26. {
  27. public GUIContent matchContent;
  28. public GUIContent widthContent;
  29. public GUIContent heightContent;
  30. public GUIContent uiScaleModeContent;
  31. public GUIStyle leftAlignedLabel;
  32. public GUIStyle rightAlignedLabel;
  33. public Styles()
  34. {
  35. matchContent = EditorGUIUtility.TrTextContent("Match");
  36. widthContent = EditorGUIUtility.TrTextContent("Width");
  37. heightContent = EditorGUIUtility.TrTextContent("Height");
  38. uiScaleModeContent = EditorGUIUtility.TrTextContent("UI Scale Mode");
  39. leftAlignedLabel = new GUIStyle(EditorStyles.label);
  40. rightAlignedLabel = new GUIStyle(EditorStyles.label);
  41. rightAlignedLabel.alignment = TextAnchor.MiddleRight;
  42. }
  43. }
  44. private static Styles s_Styles;
  45. private bool bIsPreset;
  46. protected virtual void OnEnable()
  47. {
  48. m_UiScaleMode = serializedObject.FindProperty("m_UiScaleMode");
  49. m_ScaleFactor = serializedObject.FindProperty("m_ScaleFactor");
  50. m_ReferenceResolution = serializedObject.FindProperty("m_ReferenceResolution");
  51. m_ScreenMatchMode = serializedObject.FindProperty("m_ScreenMatchMode");
  52. m_MatchWidthOrHeight = serializedObject.FindProperty("m_MatchWidthOrHeight");
  53. m_PhysicalUnit = serializedObject.FindProperty("m_PhysicalUnit");
  54. m_FallbackScreenDPI = serializedObject.FindProperty("m_FallbackScreenDPI");
  55. m_DefaultSpriteDPI = serializedObject.FindProperty("m_DefaultSpriteDPI");
  56. m_DynamicPixelsPerUnit = serializedObject.FindProperty("m_DynamicPixelsPerUnit");
  57. m_ReferencePixelsPerUnit = serializedObject.FindProperty("m_ReferencePixelsPerUnit");
  58. m_PresetInfoIsWorld = serializedObject.FindProperty("m_PresetInfoIsWorld");
  59. if (m_SerializedObject == null || m_SerializedObject.targetObject == null)
  60. bIsPreset = false;
  61. else
  62. bIsPreset = m_SerializedObject.targetObject is Component ? ((int)(m_SerializedObject.targetObject as Component).gameObject.hideFlags == 93) : !AssetDatabase.Contains(m_SerializedObject.targetObject);
  63. }
  64. public override void OnInspectorGUI()
  65. {
  66. if (s_Styles == null)
  67. s_Styles = new Styles();
  68. bool allAreRoot = true;
  69. bool showWorldDiffers = false;
  70. bool showWorld = false;
  71. if (bIsPreset)
  72. {
  73. showWorld = m_PresetInfoIsWorld.boolValue;
  74. }
  75. else
  76. {
  77. showWorld = ((target as CanvasScaler).GetComponent<Canvas>().renderMode == RenderMode.WorldSpace);
  78. m_PresetInfoIsWorld.boolValue = showWorld;
  79. serializedObject.ApplyModifiedProperties();
  80. for (int i = 0; i < targets.Length; i++)
  81. {
  82. CanvasScaler scaler = targets[i] as CanvasScaler;
  83. Canvas canvas = scaler.GetComponent<Canvas>();
  84. if (!canvas.isRootCanvas)
  85. {
  86. allAreRoot = false;
  87. break;
  88. }
  89. if (showWorld && canvas.renderMode != RenderMode.WorldSpace || !showWorld && canvas.renderMode == RenderMode.WorldSpace)
  90. {
  91. showWorldDiffers = true;
  92. break;
  93. }
  94. }
  95. if (!allAreRoot)
  96. {
  97. EditorGUILayout.HelpBox("Non-root Canvases will not be scaled.", MessageType.Warning);
  98. return;
  99. }
  100. }
  101. serializedObject.Update();
  102. EditorGUI.showMixedValue = showWorldDiffers;
  103. using (new EditorGUI.DisabledScope(showWorld || showWorldDiffers))
  104. {
  105. if (showWorld || showWorldDiffers)
  106. {
  107. EditorGUILayout.Popup(s_Styles.uiScaleModeContent.text, 0, new[] { "World" });
  108. }
  109. else
  110. {
  111. EditorGUILayout.PropertyField(m_UiScaleMode, s_Styles.uiScaleModeContent);
  112. }
  113. }
  114. EditorGUI.showMixedValue = false;
  115. if (!showWorldDiffers && !(!showWorld && m_UiScaleMode.hasMultipleDifferentValues))
  116. {
  117. EditorGUILayout.Space();
  118. // World Canvas
  119. if (showWorld)
  120. {
  121. EditorGUILayout.PropertyField(m_DynamicPixelsPerUnit);
  122. }
  123. // Constant pixel size
  124. else if (m_UiScaleMode.enumValueIndex == (int)CanvasScaler.ScaleMode.ConstantPixelSize)
  125. {
  126. EditorGUILayout.PropertyField(m_ScaleFactor);
  127. }
  128. // Scale with screen size
  129. else if (m_UiScaleMode.enumValueIndex == (int)CanvasScaler.ScaleMode.ScaleWithScreenSize)
  130. {
  131. EditorGUILayout.PropertyField(m_ReferenceResolution);
  132. EditorGUILayout.PropertyField(m_ScreenMatchMode);
  133. if (m_ScreenMatchMode.enumValueIndex == (int)CanvasScaler.ScreenMatchMode.MatchWidthOrHeight && !m_ScreenMatchMode.hasMultipleDifferentValues)
  134. {
  135. Rect r = EditorGUILayout.GetControlRect(true, EditorGUIUtility.singleLineHeight + kSliderEndpointLabelsHeight);
  136. DualLabeledSlider(r, m_MatchWidthOrHeight, s_Styles.matchContent, s_Styles.widthContent, s_Styles.heightContent);
  137. }
  138. }
  139. // Constant physical size
  140. else if (m_UiScaleMode.enumValueIndex == (int)CanvasScaler.ScaleMode.ConstantPhysicalSize)
  141. {
  142. EditorGUILayout.PropertyField(m_PhysicalUnit);
  143. EditorGUILayout.PropertyField(m_FallbackScreenDPI);
  144. EditorGUILayout.PropertyField(m_DefaultSpriteDPI);
  145. }
  146. EditorGUILayout.PropertyField(m_ReferencePixelsPerUnit);
  147. }
  148. serializedObject.ApplyModifiedProperties();
  149. }
  150. private static void DualLabeledSlider(Rect position, SerializedProperty property, GUIContent mainLabel, GUIContent labelLeft, GUIContent labelRight)
  151. {
  152. position.height = EditorGUIUtility.singleLineHeight;
  153. Rect pos = position;
  154. position.y += 12;
  155. position.xMin += EditorGUIUtility.labelWidth;
  156. position.xMax -= EditorGUIUtility.fieldWidth;
  157. GUI.Label(position, labelLeft, s_Styles.leftAlignedLabel);
  158. GUI.Label(position, labelRight, s_Styles.rightAlignedLabel);
  159. EditorGUI.PropertyField(pos, property, mainLabel);
  160. }
  161. }
  162. }