暫無描述
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.

TexturePlatformSettingsController.cs 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. using System.Collections.Generic;
  2. using UnityEngine;
  3. using UnityEngine.Assertions;
  4. namespace UnityEditor.U2D.PSD
  5. {
  6. internal class TexturePlatformSettingsController
  7. {
  8. public bool HandleDefaultSettings(List<TextureImporterPlatformSettings> platformSettings, TexturePlatformSettingsView view)
  9. {
  10. Assert.IsTrue(platformSettings.Count > 0, "At least 1 platform setting is needed to display the texture platform setting UI.");
  11. int allSize = platformSettings[0].maxTextureSize;
  12. TextureImporterCompression allCompression = platformSettings[0].textureCompression;
  13. bool allUseCrunchedCompression = platformSettings[0].crunchedCompression;
  14. int allCompressionQuality = platformSettings[0].compressionQuality;
  15. TextureResizeAlgorithm allResizeAlgorithm = platformSettings[0].resizeAlgorithm;
  16. var newSize = allSize;
  17. var newCompression = allCompression;
  18. var newUseCrunchedCompression = allUseCrunchedCompression;
  19. var newCompressionQuality = allCompressionQuality;
  20. var newResizeAlgorithm = allResizeAlgorithm;
  21. bool mixedSize = false;
  22. bool mixedCompression = false;
  23. bool mixedUseCrunchedCompression = false;
  24. bool mixedCompressionQuality = false;
  25. bool mixedResizeAlgorithm = false;
  26. bool sizeChanged = false;
  27. bool compressionChanged = false;
  28. bool useCrunchedCompressionChanged = false;
  29. bool compressionQualityChanged = false;
  30. bool resizedChanged = false;
  31. for (var i = 1; i < platformSettings.Count; ++i)
  32. {
  33. var settings = platformSettings[i];
  34. if (settings.maxTextureSize != allSize)
  35. mixedSize = true;
  36. if (settings.textureCompression != allCompression)
  37. mixedCompression = true;
  38. if (settings.crunchedCompression != allUseCrunchedCompression)
  39. mixedUseCrunchedCompression = true;
  40. if (settings.compressionQuality != allCompressionQuality)
  41. mixedCompressionQuality = true;
  42. if (settings.resizeAlgorithm != allResizeAlgorithm)
  43. mixedResizeAlgorithm = true;
  44. }
  45. EditorGUI.indentLevel++;
  46. newSize = view.DrawMaxSize(allSize, mixedSize, false, out sizeChanged);
  47. newResizeAlgorithm = view.DrawResizeAlgorithm(allResizeAlgorithm, mixedResizeAlgorithm, false, out resizedChanged);
  48. newCompression = view.DrawCompression(allCompression, mixedCompression, out compressionChanged);
  49. if (!mixedCompression && allCompression != TextureImporterCompression.Uncompressed)
  50. {
  51. newUseCrunchedCompression = view.DrawUseCrunchedCompression(allUseCrunchedCompression, mixedUseCrunchedCompression, out useCrunchedCompressionChanged);
  52. if (!mixedUseCrunchedCompression && allUseCrunchedCompression)
  53. {
  54. newCompressionQuality = view.DrawCompressionQualitySlider(allCompressionQuality, mixedCompressionQuality, out compressionQualityChanged);
  55. }
  56. }
  57. EditorGUI.indentLevel--;
  58. if (sizeChanged || compressionChanged || useCrunchedCompressionChanged || compressionQualityChanged || resizedChanged)
  59. {
  60. for (var i = 0; i < platformSettings.Count; ++i)
  61. {
  62. if (sizeChanged)
  63. platformSettings[i].maxTextureSize = newSize;
  64. if (compressionChanged)
  65. platformSettings[i].textureCompression = newCompression;
  66. if (useCrunchedCompressionChanged)
  67. platformSettings[i].crunchedCompression = newUseCrunchedCompression;
  68. if (compressionQualityChanged)
  69. platformSettings[i].compressionQuality = newCompressionQuality;
  70. if (resizedChanged)
  71. platformSettings[i].resizeAlgorithm = newResizeAlgorithm;
  72. }
  73. return true;
  74. }
  75. else
  76. return false;
  77. }
  78. public bool HandlePlatformSettings(BuildTarget buildTarget, List<TextureImporterPlatformSettings> platformSettings, TexturePlatformSettingsView view)
  79. {
  80. if (buildTarget == BuildTarget.NoTarget)
  81. {
  82. return HandleDefaultSettings(platformSettings, view);
  83. }
  84. Assert.IsTrue(platformSettings.Count > 0, "At least 1 platform setting is needed to display the texture platform setting UI.");
  85. bool allOverride = platformSettings[0].overridden;
  86. int allSize = platformSettings[0].maxTextureSize;
  87. TextureImporterFormat allFormat = platformSettings[0].format;
  88. int allCompressionQuality = platformSettings[0].compressionQuality;
  89. TextureResizeAlgorithm allResizeAlgorithm = platformSettings[0].resizeAlgorithm;
  90. var newResizeAlgorithm = allResizeAlgorithm;
  91. var newOverride = allOverride;
  92. var newSize = allSize;
  93. var newFormat = allFormat;
  94. var newCompressionQuality = allCompressionQuality;
  95. bool mixedOverride = false;
  96. bool mixedSize = false;
  97. bool mixedFormat = false;
  98. bool mixedCompression = false;
  99. bool mixedResizeAlgorithm = false;
  100. bool overrideChanged = false;
  101. bool sizeChanged = false;
  102. bool formatChanged = false;
  103. bool compressionChanged = false;
  104. bool resizedChanged = false;
  105. for (var i = 1; i < platformSettings.Count; ++i)
  106. {
  107. var settings = platformSettings[i];
  108. if (settings.overridden != allOverride)
  109. mixedOverride = true;
  110. if (settings.maxTextureSize != allSize)
  111. mixedSize = true;
  112. if (settings.format != allFormat)
  113. mixedFormat = true;
  114. if (settings.compressionQuality != allCompressionQuality)
  115. mixedCompression = true;
  116. if (settings.resizeAlgorithm != allResizeAlgorithm)
  117. mixedResizeAlgorithm = true;
  118. }
  119. EditorGUI.indentLevel++;
  120. newOverride = view.DrawOverride(allOverride, mixedOverride, out overrideChanged);
  121. newResizeAlgorithm = view.DrawResizeAlgorithm(allResizeAlgorithm, mixedResizeAlgorithm, mixedOverride || !allOverride, out resizedChanged);
  122. newSize = view.DrawMaxSize(allSize, mixedSize, mixedOverride || !allOverride, out sizeChanged);
  123. int[] formatValues = null;
  124. string[] formatStrings = null;
  125. AcquireTextureFormatValuesAndStrings(buildTarget, out formatValues, out formatStrings);
  126. newFormat = view.DrawFormat(allFormat, formatValues, formatStrings, mixedFormat, mixedOverride || !allOverride, out formatChanged);
  127. if (!mixedFormat && !mixedOverride && allOverride && IsFormatRequireCompressionSetting(allFormat))
  128. {
  129. bool showAsEnum =
  130. buildTarget == BuildTarget.iOS ||
  131. buildTarget == BuildTarget.tvOS ||
  132. buildTarget == BuildTarget.Android
  133. ;
  134. if (showAsEnum)
  135. {
  136. int compressionMode = 1;
  137. if (allCompressionQuality == (int)TextureCompressionQuality.Fast)
  138. compressionMode = 0;
  139. else if (allCompressionQuality == (int)TextureCompressionQuality.Best)
  140. compressionMode = 2;
  141. var returnValue = view.DrawCompressionQualityPopup(compressionMode, mixedCompression, out compressionChanged);
  142. if (compressionChanged)
  143. {
  144. switch (returnValue)
  145. {
  146. case 0:
  147. newCompressionQuality = (int)TextureCompressionQuality.Fast;
  148. break;
  149. case 1:
  150. newCompressionQuality = (int)TextureCompressionQuality.Normal;
  151. break;
  152. case 2:
  153. newCompressionQuality = (int)TextureCompressionQuality.Best;
  154. break;
  155. default:
  156. Assert.IsTrue(false, "ITexturePlatformSettingsView.DrawCompressionQualityPopup should never return compression option value that's not 0, 1 or 2.");
  157. break;
  158. }
  159. }
  160. }
  161. else
  162. {
  163. newCompressionQuality = view.DrawCompressionQualitySlider(allCompressionQuality, mixedCompression, out compressionChanged);
  164. }
  165. }
  166. EditorGUI.indentLevel--;
  167. if (overrideChanged || sizeChanged || formatChanged || compressionChanged || resizedChanged)
  168. {
  169. for (var i = 0; i < platformSettings.Count; ++i)
  170. {
  171. if (overrideChanged)
  172. platformSettings[i].overridden = newOverride;
  173. if (sizeChanged)
  174. platformSettings[i].maxTextureSize = newSize;
  175. if (formatChanged)
  176. platformSettings[i].format = newFormat;
  177. if (compressionChanged)
  178. platformSettings[i].compressionQuality = newCompressionQuality;
  179. if (resizedChanged)
  180. platformSettings[i].resizeAlgorithm = newResizeAlgorithm;
  181. }
  182. return true;
  183. }
  184. else
  185. return false;
  186. }
  187. public void AcquireTextureFormatValuesAndStrings(BuildTarget buildTarget, out int[] formatValues, out string[] formatStrings)
  188. {
  189. if (IsGLESMobileTargetPlatform(buildTarget))
  190. {
  191. if (buildTarget == BuildTarget.iOS || buildTarget == BuildTarget.tvOS)
  192. {
  193. formatValues = TexturePlatformSettingsModal.kTextureFormatsValueApplePVR;
  194. formatStrings = TexturePlatformSettingsModal.s_TextureFormatStringsApplePVR;
  195. }
  196. else
  197. {
  198. formatValues = TexturePlatformSettingsModal.kTextureFormatsValueAndroid;
  199. formatStrings = TexturePlatformSettingsModal.s_TextureFormatStringsAndroid;
  200. }
  201. }
  202. else
  203. {
  204. if (buildTarget == BuildTarget.WebGL)
  205. {
  206. formatValues = TexturePlatformSettingsModal.kTextureFormatsValueWebGL;
  207. formatStrings = TexturePlatformSettingsModal.s_TextureFormatStringsWebGL;
  208. }
  209. else
  210. {
  211. formatValues = TexturePlatformSettingsModal.kTextureFormatsValueDefault;
  212. formatStrings = TexturePlatformSettingsModal.s_TextureFormatStringsDefault;
  213. }
  214. }
  215. }
  216. internal static bool IsFormatRequireCompressionSetting(TextureImporterFormat format)
  217. {
  218. return ArrayUtility.Contains<TextureImporterFormat>(TexturePlatformSettingsModal.kFormatsWithCompressionSettings, format);
  219. }
  220. internal static bool IsGLESMobileTargetPlatform(BuildTarget target)
  221. {
  222. return target == BuildTarget.iOS || target == BuildTarget.tvOS || target == BuildTarget.Android;
  223. }
  224. }
  225. }