FR2_Helper.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. using System.Collections.Generic;
  2. using UnityEditor;
  3. using UnityEngine;
  4. using UnityEngine.SceneManagement;
  5. namespace vietlabs.fr2
  6. {
  7. public class FR2_Helper
  8. {
  9. internal static readonly AssetType[] FILTERS =
  10. {
  11. new AssetType("Scene", ".unity"),
  12. new AssetType("Prefab", ".prefab"),
  13. new AssetType("Model", ".3df", ".3dm", ".3dmf", ".3dv", ".3dx", ".c5d", ".lwo", ".lws", ".ma", ".mb",
  14. ".mesh", ".vrl", ".wrl", ".wrz", ".fbx", ".dae", ".3ds", ".dxf", ".obj", ".skp", ".max", ".blend"),
  15. new AssetType("Material", ".mat", ".cubemap", ".physicsmaterial"),
  16. new AssetType("Texture", ".ai", ".apng", ".png", ".bmp", ".cdr", ".dib", ".eps", ".exif", ".ico", ".icon",
  17. ".j", ".j2c", ".j2k", ".jas", ".jiff", ".jng", ".jp2", ".jpc", ".jpe", ".jpeg", ".jpf", ".jpg", "jpw",
  18. "jpx", "jtf", ".mac", ".omf", ".qif", ".qti", "qtif", ".tex", ".tfw", ".tga", ".tif", ".tiff", ".wmf",
  19. ".psd", ".exr", ".rendertexture"),
  20. new AssetType("Video", ".asf", ".asx", ".avi", ".dat", ".divx", ".dvx", ".mlv", ".m2l", ".m2t", ".m2ts",
  21. ".m2v", ".m4e", ".m4v", "mjp", ".mov", ".movie", ".mp21", ".mp4", ".mpe", ".mpeg", ".mpg", ".mpv2",
  22. ".ogm", ".qt", ".rm", ".rmvb", ".wmv", ".xvid", ".flv"),
  23. new AssetType("Audio", ".mp3", ".wav", ".ogg", ".aif", ".aiff", ".mod", ".it", ".s3m", ".xm"),
  24. new AssetType("Script", ".cs", ".js", ".boo"),
  25. new AssetType("Text", ".txt", ".json", ".xml", ".bytes", ".sql"),
  26. new AssetType("Shader", ".shader", ".cginc"),
  27. new AssetType("Animation", ".anim", ".controller", ".overridecontroller", ".mask"),
  28. new AssetType("Unity Asset", ".asset", ".guiskin", ".flare", ".fontsettings", ".prefs"),
  29. new AssetType("Others") //
  30. };
  31. public static IEnumerable<GameObject> getAllObjsInCurScene()
  32. {
  33. // foreach (GameObject obj in Object.FindObjectsOfType(typeof(GameObject)))
  34. // {
  35. // yield return obj;
  36. // }
  37. for (var j = 0; j < SceneManager.sceneCount; j++)
  38. {
  39. Scene scene = SceneManager.GetSceneAt(j);
  40. foreach (GameObject item in GetGameObjectsInScene(scene))
  41. {
  42. yield return item;
  43. }
  44. }
  45. if (EditorApplication.isPlaying)
  46. {
  47. //dont destroy scene
  48. GameObject temp = null;
  49. try
  50. {
  51. temp = new GameObject();
  52. Object.DontDestroyOnLoad(temp);
  53. Scene dontDestroyOnLoad = temp.scene;
  54. Object.DestroyImmediate(temp);
  55. temp = null;
  56. foreach (GameObject item in GetGameObjectsInScene(dontDestroyOnLoad))
  57. {
  58. yield return item;
  59. }
  60. }
  61. finally
  62. {
  63. if (temp != null)
  64. {
  65. Object.DestroyImmediate(temp);
  66. }
  67. }
  68. }
  69. }
  70. private static IEnumerable<GameObject> GetGameObjectsInScene(Scene scene)
  71. {
  72. var rootObjects = new List<GameObject>();
  73. scene.GetRootGameObjects(rootObjects);
  74. // iterate root objects and do something
  75. for (var i = 0; i < rootObjects.Count; ++i)
  76. {
  77. GameObject gameObject = rootObjects[i];
  78. foreach (GameObject item in getAllChild(gameObject))
  79. {
  80. yield return item;
  81. }
  82. yield return gameObject;
  83. }
  84. }
  85. public static IEnumerable<GameObject> getAllChild(GameObject target)
  86. {
  87. if (target.transform.childCount > 0)
  88. {
  89. for (var i = 0; i < target.transform.childCount; i++)
  90. {
  91. yield return target.transform.GetChild(i).gameObject;
  92. foreach (GameObject item in getAllChild(target.transform.GetChild(i).gameObject))
  93. {
  94. yield return item;
  95. }
  96. }
  97. }
  98. }
  99. public static IEnumerable<Object> GetAllRefObjects(GameObject obj)
  100. {
  101. Component[] components = obj.GetComponents<Component>();
  102. foreach (Component com in components)
  103. {
  104. if (com == null)
  105. {
  106. continue;
  107. }
  108. var serialized = new SerializedObject(com);
  109. SerializedProperty it = serialized.GetIterator().Copy();
  110. while (it.NextVisible(true))
  111. {
  112. if (it.propertyType != SerializedPropertyType.ObjectReference)
  113. {
  114. continue;
  115. }
  116. if (it.objectReferenceValue == null)
  117. {
  118. continue;
  119. }
  120. yield return it.objectReferenceValue;
  121. }
  122. }
  123. }
  124. public static int StringMatch(string pattern, string input)
  125. {
  126. if (input == pattern)
  127. {
  128. return int.MaxValue;
  129. }
  130. if (input.Contains(pattern))
  131. {
  132. return int.MaxValue - 1;
  133. }
  134. var pidx = 0;
  135. var score = 0;
  136. var tokenScore = 0;
  137. for (var i = 0; i < input.Length; i++)
  138. {
  139. char ch = input[i];
  140. if (ch == pattern[pidx])
  141. {
  142. tokenScore += tokenScore + 1; //increasing score for continuos token
  143. pidx++;
  144. if (pidx >= pattern.Length)
  145. {
  146. break;
  147. }
  148. }
  149. else
  150. {
  151. tokenScore = 0;
  152. }
  153. score += tokenScore;
  154. }
  155. return score;
  156. }
  157. public static int GetIndex(string ext)
  158. {
  159. for (var i = 0; i < FILTERS.Length - 1; i++)
  160. {
  161. if (FILTERS[i].extension.Contains(ext))
  162. {
  163. return i;
  164. }
  165. }
  166. return FILTERS.Length - 1; //Others
  167. }
  168. public static void GuiLine(int i_height = 1)
  169. {
  170. Rect rect = EditorGUILayout.GetControlRect(false, i_height);
  171. rect.height = i_height;
  172. EditorGUI.DrawRect(rect, new Color(0.5f, 0.5f, 0.5f, 1));
  173. }
  174. public static string GetfileSizeString(long fileSize)
  175. {
  176. return fileSize <= 1024
  177. ? fileSize + " B"
  178. : fileSize <= 1024 * 1024
  179. ? Mathf.RoundToInt(fileSize / 1024f) + " KB"
  180. : Mathf.RoundToInt(fileSize / 1024f / 1024f) + " MB";
  181. }
  182. }
  183. }