TMPro_SortingLayerHelper.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /*
  2. * Copyright (c) 2014, Nick Gravelyn.
  3. *
  4. * This software is provided 'as-is', without any express or implied
  5. * warranty. In no event will the authors be held liable for any damages
  6. * arising from the use of this software.
  7. *
  8. * Permission is granted to anyone to use this software for any purpose,
  9. * including commercial applications, and to alter it and redistribute it
  10. * freely, subject to the following restrictions:
  11. *
  12. * 1. The origin of this software must not be misrepresented; you must not
  13. * claim that you wrote the original software. If you use this software
  14. * in a product, an acknowledgment in the product documentation would be
  15. * appreciated but is not required.
  16. *
  17. * 2. Altered source versions must be plainly marked as such, and must not be
  18. * misrepresented as being the original software.
  19. *
  20. * 3. This notice may not be removed or altered from any source
  21. * distribution.
  22. */
  23. using UnityEngine;
  24. using UnityEditor;
  25. using System;
  26. using System.Reflection;
  27. namespace TMPro
  28. {
  29. // Helpers used by the different sorting layer classes.
  30. public static class SortingLayerHelper
  31. {
  32. private static Type _utilityType;
  33. private static PropertyInfo _sortingLayerNamesProperty;
  34. private static MethodInfo _getSortingLayerUserIdMethod;
  35. static SortingLayerHelper()
  36. {
  37. _utilityType = Type.GetType("UnityEditorInternal.InternalEditorUtility, UnityEditor");
  38. _sortingLayerNamesProperty = _utilityType.GetProperty("sortingLayerNames", BindingFlags.Static | BindingFlags.NonPublic);
  39. _getSortingLayerUserIdMethod = _utilityType.GetMethod("GetSortingLayerUniqueID", BindingFlags.Static | BindingFlags.NonPublic);
  40. }
  41. // Gets an array of sorting layer names.
  42. // Since this uses reflection, callers should check for 'null' which will be returned if the reflection fails.
  43. public static string[] sortingLayerNames
  44. {
  45. get
  46. {
  47. if (_sortingLayerNamesProperty == null)
  48. {
  49. return null;
  50. }
  51. return _sortingLayerNamesProperty.GetValue(null, null) as string[];
  52. }
  53. }
  54. // Given the ID of a sorting layer, returns the sorting layer's name
  55. public static string GetSortingLayerNameFromID(int id)
  56. {
  57. string[] names = sortingLayerNames;
  58. if (names == null)
  59. {
  60. return null;
  61. }
  62. for (int i = 0; i < names.Length; i++)
  63. {
  64. if (GetSortingLayerIDForIndex(i) == id)
  65. {
  66. return names[i];
  67. }
  68. }
  69. return null;
  70. }
  71. // Given the name of a sorting layer, returns the ID.
  72. public static int GetSortingLayerIDForName(string name)
  73. {
  74. string[] names = sortingLayerNames;
  75. if (names == null)
  76. {
  77. return 0;
  78. }
  79. return GetSortingLayerIDForIndex(Array.IndexOf(names, name));
  80. }
  81. // Helper to convert from a sorting layer INDEX to a sorting layer ID. These are not the same thing.
  82. // IDs are based on the order in which layers were created and do not change when reordering the layers.
  83. // Thankfully there is a private helper we can call to get the ID for a layer given its index.
  84. public static int GetSortingLayerIDForIndex(int index)
  85. {
  86. if (_getSortingLayerUserIdMethod == null)
  87. {
  88. return 0;
  89. }
  90. return (int)_getSortingLayerUserIdMethod.Invoke(null, new object[] { index });
  91. }
  92. }
  93. }