CommonMethods.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using UnityEngine;
  7. using UnityEngine.UI;
  8. namespace Bitsplash
  9. {
  10. class CommonMethods
  11. {
  12. public static readonly Rect UVAbsolute = new Rect(0f, 0f, 1f, 1f);
  13. internal static T EnsureComponent<T>(GameObject obj,bool hide = false) where T : Component
  14. {
  15. T comp = obj.GetComponent<T>();
  16. if (comp == null)
  17. comp = obj.AddComponent<T>();
  18. if (Application.isPlaying == false && Application.isEditor == true)
  19. {
  20. // comp.tag = "EditorOnly";
  21. comp.hideFlags = HideFlags.DontSaveInEditor;
  22. }
  23. return comp;
  24. }
  25. public static void HideObject(GameObject obj)
  26. {
  27. if (Application.isPlaying == false && Application.isEditor == true)
  28. {
  29. obj.tag = "EditorOnly";
  30. obj.hideFlags = HideFlags.DontSaveInEditor;
  31. }
  32. obj.hideFlags = obj.hideFlags | HideFlags.HideInHierarchy | HideFlags.HideInInspector;
  33. }
  34. public static void SafeDestroy( UnityEngine.Object obj)
  35. {
  36. if (obj == null)
  37. return;
  38. if (Application.isEditor && Application.isPlaying == false)
  39. UnityEngine.Object.DestroyImmediate(obj);
  40. else
  41. UnityEngine.Object.Destroy(obj);
  42. obj = null;
  43. }
  44. public static Rect HorizontalTextureTile(float amount)
  45. {
  46. return new Rect(0f, 0f, amount, 1f);
  47. }
  48. public static Rect VerticalTextureTile(float amount)
  49. {
  50. return new Rect(0f, 0f,1f, amount);
  51. }
  52. public static float InterpolateInRectX(float v,Rect r)
  53. {
  54. return r.x + v * r.width;
  55. }
  56. public static float InterpolateInRectY(float v, Rect r)
  57. {
  58. return r.y + v * r.height;
  59. }
  60. public static Vector2 InterpolateInRect(Vector2 v,Rect r)
  61. {
  62. return new Vector2(r.x + v.x * r.width, r.y + v.y * r.height);
  63. }
  64. public static void DrawVertical(float x, Rect bounds, float thickness, Rect uv, Color color, VertexHelper vh)
  65. {
  66. Rect r = new Rect(x-thickness*0.5f, bounds.y, thickness,bounds.height);
  67. DrawRect(r, uv, color, vh);
  68. }
  69. public static void DrawHorizontal(float y,Rect bounds,float thickness,Rect uv, Color color,VertexHelper vh)
  70. {
  71. Rect r = new Rect(bounds.x, y - thickness * 0.5f, bounds.width, thickness);
  72. DrawRect(r, uv, color, vh);
  73. }
  74. public static void DrawRect(Rect rect,Rect uv,Color color,VertexHelper vh)
  75. {
  76. int index = vh.currentVertCount;
  77. vh.AddVert(new Vector3(rect.xMin, rect.yMin, 0f), color, new Vector2(uv.xMin, uv.yMin));
  78. vh.AddVert(new Vector3(rect.xMin, rect.yMax, 0f), color, new Vector2(uv.xMin, uv.yMax));
  79. vh.AddVert(new Vector3(rect.xMax, rect.yMax, 0f), color, new Vector2(uv.xMax, uv.yMax));
  80. vh.AddVert(new Vector3(rect.xMax, rect.yMin, 0f), color, new Vector2(uv.xMax, uv.yMin));
  81. vh.AddTriangle(index, index+ 1, index+2);
  82. vh.AddTriangle(index+2, index+3, index);
  83. }
  84. public static bool IsRangeSelected(DateTime from, DateTime to, HashSet<DateTime> selection)
  85. {
  86. from = from.Date;
  87. to = to.Date;
  88. if (from == to)
  89. {
  90. return selection.Count == 1 && selection.Contains(from);
  91. }
  92. if (from > to)
  93. {
  94. DateTime tmp = from;
  95. from = to;
  96. to = tmp;
  97. }
  98. DateTime iterator = from;
  99. int count = 0;
  100. while (iterator <= to)
  101. {
  102. if (selection.Contains(iterator.Date) == false)
  103. return false;
  104. count++;
  105. iterator += TimeSpan.FromDays(1);
  106. }
  107. if (selection.Count != count)
  108. return false;
  109. return true;
  110. }
  111. public static void SelectRange(DateTime from, DateTime to, HashSet<DateTime> selection)
  112. {
  113. selection.Clear();
  114. from = from.Date;
  115. to = to.Date;
  116. if (from == to)
  117. {
  118. selection.Add(from);
  119. return;
  120. }
  121. if (from > to)
  122. {
  123. DateTime tmp = from;
  124. from = to;
  125. to = tmp;
  126. }
  127. DateTime iterator = from;
  128. while (iterator <= to)
  129. {
  130. selection.Add(iterator.Date);
  131. iterator += TimeSpan.FromDays(1);
  132. }
  133. }
  134. }
  135. }