TextureImporterUtility.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /******************************************************************************
  2. * Spine Runtimes License Agreement
  3. * Last updated July 28, 2023. Replaces all prior versions.
  4. *
  5. * Copyright (c) 2013-2024, Esoteric Software LLC
  6. *
  7. * Integration of the Spine Runtimes into software or otherwise creating
  8. * derivative works of the Spine Runtimes is permitted under the terms and
  9. * conditions of Section 2 of the Spine Editor License Agreement:
  10. * http://esotericsoftware.com/spine-editor-license
  11. *
  12. * Otherwise, it is permitted to integrate the Spine Runtimes into software or
  13. * otherwise create derivative works of the Spine Runtimes (collectively,
  14. * "Products"), provided that each user of the Products must obtain their own
  15. * Spine Editor license and redistribution of the Products in any form must
  16. * include this license and copyright notice.
  17. *
  18. * THE SPINE RUNTIMES ARE PROVIDED BY ESOTERIC SOFTWARE LLC "AS IS" AND ANY
  19. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL ESOTERIC SOFTWARE LLC BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES,
  24. * BUSINESS INTERRUPTION, OR LOSS OF USE, DATA, OR PROFITS) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THE
  27. * SPINE RUNTIMES, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. *****************************************************************************/
  29. using System;
  30. using System.Collections.Generic;
  31. using System.Linq;
  32. using UnityEditor;
  33. namespace Spine.Unity.Editor {
  34. /// <summary>
  35. /// Utility class for working with TextureImporter.
  36. /// </summary>
  37. public static class TextureImporterUtility {
  38. private static IEnumerable<string> GetAllPlatforms () {
  39. BuildTarget[] buildTargets = (BuildTarget[])Enum.GetValues(typeof(BuildTarget));
  40. var platformNames = buildTargets.Select(x => x.ToString()).ToList();
  41. // Add additional platforms that are not part of BuildTarget enum.
  42. platformNames.Add("Server");
  43. return platformNames.ToArray();
  44. }
  45. /// <summary>Disables Texture Import settings platform overrides for all platforms.</summary>
  46. /// <param name="importer">The TextureImporter wrapper of the target texture asset.</param>
  47. /// <param name="disabledPlatforms">A list populated with platforms where overrides were previously enabled and
  48. /// which have now been disabled.</param>
  49. /// <returns>True if an override has been disabled for any platform, false otherwise.</returns>
  50. public static bool DisableOverrides (TextureImporter importer, out List<string> disabledPlatforms) {
  51. IEnumerable<string> platforms = GetAllPlatforms();
  52. disabledPlatforms = new List<string>();
  53. foreach (string platform in platforms) {
  54. var platformSettings = importer.GetPlatformTextureSettings(platform);
  55. if (!platformSettings.overridden)
  56. continue;
  57. disabledPlatforms.Add(platform);
  58. platformSettings.overridden = false;
  59. importer.SetPlatformTextureSettings(platformSettings);
  60. }
  61. if (disabledPlatforms.Count <= 0)
  62. return false;
  63. importer.SaveAndReimport();
  64. return true;
  65. }
  66. /// <summary>Enables Texture Import settings platform overrides for given platforms.</summary>
  67. /// <param name="importer">The TextureImporter wrapper of the target texture asset.</param>
  68. /// <param name="platformsToEnable">A list of platforms for which overrides shall be enabled.</param>
  69. public static void EnableOverrides (TextureImporter importer, List<string> platformsToEnable) {
  70. if (platformsToEnable.Count == 0)
  71. return;
  72. foreach (string platform in platformsToEnable) {
  73. TextureImporterPlatformSettings platformSettings = importer.GetPlatformTextureSettings(platform);
  74. platformSettings.overridden = true;
  75. importer.SetPlatformTextureSettings(platformSettings);
  76. }
  77. importer.SaveAndReimport();
  78. }
  79. }
  80. }