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.

145 lines
4.1 KiB

  1. using UnityEngine;
  2. using System.Collections.Generic;
  3. #if UNITY_EDITOR
  4. using UnityEditor;
  5. #endif
  6. namespace FogVolumeUtilities
  7. {
  8. public static class ExtensionMethods
  9. {
  10. public static float Remap(this float value, float from1, float to1, float from2, float to2)
  11. {
  12. return (value - from1) / (to1 - from1) * (to2 - from2) + from2;
  13. }
  14. public static bool TimeSnap(int Frames)
  15. {
  16. bool refresh = true;
  17. if (Application.isPlaying)
  18. {
  19. refresh = Time.frameCount <= 3 || (Time.frameCount % (1 + Frames)) == 0;
  20. return (refresh);
  21. }
  22. else
  23. return true;
  24. }
  25. public static void Swap<T>(ref T lhs, ref T rhs)
  26. {
  27. T temp = lhs;
  28. lhs = rhs;
  29. rhs = temp;
  30. }
  31. }
  32. public class Rendering
  33. {
  34. public static void EnsureKeyword(Material material, string name, bool enabled)
  35. {
  36. if (enabled != material.IsKeywordEnabled(name))
  37. {
  38. if (enabled)
  39. material.EnableKeyword(name);
  40. else
  41. material.DisableKeyword(name);
  42. }
  43. }
  44. }
  45. public struct int2
  46. {
  47. public int x;
  48. public int y;
  49. public int2(int x, int y)
  50. {
  51. this.x = x; this.y = y;
  52. }
  53. }
  54. public static class EditorExtension
  55. {
  56. private static string[] m_LayerNames = null;
  57. private static int[] m_LayerMasks = null;
  58. static EditorExtension()
  59. {
  60. var tmpNames = new List<string>();
  61. var tmpMasks = new List<int>();
  62. for (int i = 0; i < 32; i++)
  63. {
  64. try
  65. {
  66. var name = LayerMask.LayerToName(i);
  67. if (name != "")
  68. {
  69. tmpNames.Add(name);
  70. tmpMasks.Add(1 << i);
  71. }
  72. }
  73. catch { }
  74. }
  75. m_LayerNames = tmpNames.ToArray();
  76. m_LayerMasks = tmpMasks.ToArray();
  77. }
  78. public static void ToggleInHierarchy(Object obj, bool visible)
  79. {
  80. #if UNITY_EDITOR
  81. if (visible)
  82. obj.hideFlags = HideFlags.None;
  83. else
  84. obj.hideFlags = HideFlags.HideInHierarchy;
  85. try
  86. {
  87. EditorApplication.RepaintHierarchyWindow();
  88. EditorApplication.DirtyHierarchyWindowSorting();
  89. }
  90. catch { }
  91. #endif
  92. }
  93. #if UNITY_EDITOR
  94. public static int DrawLayerMaskField(Rect aPosition, int aMask, GUIContent aLabel)
  95. {
  96. int val = aMask;
  97. int maskVal = 0;
  98. for (int i = 0; i < m_LayerNames.Length; i++)
  99. {
  100. if (m_LayerMasks[i] != 0)
  101. {
  102. if ((val & m_LayerMasks[i]) == m_LayerMasks[i])
  103. maskVal |= 1 << i;
  104. }
  105. else if (val == 0)
  106. maskVal |= 1 << i;
  107. }
  108. int newMaskVal = UnityEditor.EditorGUI.MaskField(aPosition, aLabel, maskVal, m_LayerNames);
  109. int changes = maskVal ^ newMaskVal;
  110. for (int i = 0; i < m_LayerMasks.Length; i++)
  111. {
  112. if ((changes & (1 << i)) != 0) // has this list item changed?
  113. {
  114. if ((newMaskVal & (1 << i)) != 0) // has it been set?
  115. {
  116. if (m_LayerMasks[i] == 0) // special case: if "0" is set, just set the val to 0
  117. {
  118. val = 0;
  119. break;
  120. }
  121. else
  122. val |= m_LayerMasks[i];
  123. }
  124. else // it has been reset
  125. {
  126. val &= ~m_LayerMasks[i];
  127. }
  128. }
  129. }
  130. return val;
  131. }
  132. #endif
  133. }
  134. }