LayerMaskExtensions.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. using UnityEngine;
  2. using System.Collections.Generic;
  3. namespace RootMotion {
  4. /// <summary>
  5. /// This class contains tools for working with LayerMasks.
  6. /// Most of this was copied from Unity Wiki: http://wiki.unity3d.com/index.php?title=LayerMaskExtensions.
  7. /// </summary>
  8. public static class LayerMaskExtensions
  9. {
  10. /// <summary>
  11. /// Does the LayerMask contain a specific layer index?
  12. /// </summary>
  13. public static bool Contains(LayerMask mask, int layer) {
  14. return mask == (mask | (1 << layer));
  15. }
  16. /// <summary>
  17. /// Creates a LayerMask from an array of layer names.
  18. /// </summary>
  19. public static LayerMask Create(params string[] layerNames)
  20. {
  21. return NamesToMask(layerNames);
  22. }
  23. /// <summary>
  24. /// Creates a LayerMask from an array of layer indexes.
  25. /// </summary>
  26. public static LayerMask Create(params int[] layerNumbers)
  27. {
  28. return LayerNumbersToMask(layerNumbers);
  29. }
  30. /// <summary>
  31. /// Creates a LayerMask from a number of layer names.
  32. /// </summary>
  33. public static LayerMask NamesToMask(params string[] layerNames)
  34. {
  35. LayerMask ret = (LayerMask)0;
  36. foreach(var name in layerNames)
  37. {
  38. ret |= (1 << LayerMask.NameToLayer(name));
  39. }
  40. return ret;
  41. }
  42. /// <summary>
  43. /// Creates a LayerMask from a number of layer indexes.
  44. /// </summary>
  45. public static LayerMask LayerNumbersToMask(params int[] layerNumbers)
  46. {
  47. LayerMask ret = (LayerMask)0;
  48. foreach(var layer in layerNumbers)
  49. {
  50. ret |= (1 << layer);
  51. }
  52. return ret;
  53. }
  54. /// <summary>
  55. /// Inverts a LayerMask.
  56. /// </summary>
  57. public static LayerMask Inverse(this LayerMask original)
  58. {
  59. return ~original;
  60. }
  61. /// <summary>
  62. /// Adds a number of layer names to an existing LayerMask.
  63. /// </summary>
  64. public static LayerMask AddToMask(this LayerMask original, params string[] layerNames)
  65. {
  66. return original | NamesToMask(layerNames);
  67. }
  68. /// <summary>
  69. /// Removes a number of layer names from an existing LayerMask.
  70. /// </summary>
  71. public static LayerMask RemoveFromMask(this LayerMask original, params string[] layerNames)
  72. {
  73. LayerMask invertedOriginal = ~original;
  74. return ~(invertedOriginal | NamesToMask(layerNames));
  75. }
  76. /// <summary>
  77. /// Returns a string array of layer names from a LayerMask.
  78. /// </summary>
  79. public static string[] MaskToNames(this LayerMask original)
  80. {
  81. var output = new List<string>();
  82. for (int i = 0; i < 32; ++i)
  83. {
  84. int shifted = 1 << i;
  85. if ((original & shifted) == shifted)
  86. {
  87. string layerName = LayerMask.LayerToName(i);
  88. if (!string.IsNullOrEmpty(layerName))
  89. {
  90. output.Add(layerName);
  91. }
  92. }
  93. }
  94. return output.ToArray();
  95. }
  96. /// <summary>
  97. /// Returns an array of layer indexes from a LayerMask.
  98. /// </summary>
  99. public static int[] MaskToNumbers(this LayerMask original)
  100. {
  101. var output = new List<int>();
  102. for (int i = 0; i < 32; ++i)
  103. {
  104. int shifted = 1 << i;
  105. if ((original & shifted) == shifted)
  106. {
  107. output.Add(i);
  108. }
  109. }
  110. return output.ToArray();
  111. }
  112. /// <summary>
  113. /// Parses a LayerMask to a string.
  114. /// </summary>
  115. public static string MaskToString(this LayerMask original)
  116. {
  117. return MaskToString(original, ", ");
  118. }
  119. /// <summary>
  120. /// Parses a LayerMask to a string using the specified delimiter.
  121. /// </summary>
  122. public static string MaskToString(this LayerMask original, string delimiter)
  123. {
  124. return string.Join(delimiter, MaskToNames(original));
  125. }
  126. }
  127. }