FR2_Unity.cs 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084
  1. #if UNITY_5_3_OR_NEWER
  2. #define UNITY_4_3_OR_NEWER
  3. #define UNITY_4_4_OR_NEWER
  4. #define UNITY_4_5_OR_NEWER
  5. #define UNITY_4_6_OR_NEWER
  6. #define UNITY_4_7_OR_NEWER
  7. #define UNITY_5_0_OR_NEWER
  8. #define UNITY_5_1_OR_NEWER
  9. #define UNITY_5_2_OR_NEWER
  10. #else
  11. #if UNITY_5
  12. #define UNITY_4_3_OR_NEWER
  13. #define UNITY_4_4_OR_NEWER
  14. #define UNITY_4_5_OR_NEWER
  15. #define UNITY_4_6_OR_NEWER
  16. #define UNITY_4_7_OR_NEWER
  17. #if UNITY_5_0
  18. #define UNITY_5_0_OR_NEWER
  19. #elif UNITY_5_1
  20. #define UNITY_5_0_OR_NEWER
  21. #define UNITY_5_1_OR_NEWER
  22. #elif UNITY_5_2
  23. #define UNITY_5_0_OR_NEWER
  24. #define UNITY_5_1_OR_NEWER
  25. #define UNITY_5_2_OR_NEWER
  26. #endif
  27. #else
  28. #if UNITY_4_3
  29. #define UNITY_4_3_OR_NEWER
  30. #elif UNITY_4_4
  31. #define UNITY_4_3_OR_NEWER
  32. #define UNITY_4_4_OR_NEWER
  33. #elif UNITY_4_5
  34. #define UNITY_4_3_OR_NEWER
  35. #define UNITY_4_4_OR_NEWER
  36. #define UNITY_4_5_OR_NEWER
  37. #elif UNITY_4_6
  38. #define UNITY_4_3_OR_NEWER
  39. #define UNITY_4_4_OR_NEWER
  40. #define UNITY_4_5_OR_NEWER
  41. #define UNITY_4_6_OR_NEWER
  42. #elif UNITY_4_7
  43. #define UNITY_4_3_OR_NEWER
  44. #define UNITY_4_4_OR_NEWER
  45. #define UNITY_4_5_OR_NEWER
  46. #define UNITY_4_6_OR_NEWER
  47. #define UNITY_4_7_OR_NEWER
  48. #endif
  49. #endif
  50. #endif
  51. #if UNITY_5_3_OR_NEWER
  52. #define UNITY_SCENE_MANAGER
  53. #endif
  54. using System;
  55. using System.Collections.Generic;
  56. using System.Reflection;
  57. using UnityEditor;
  58. #if FR2_ADDRESSABLE
  59. using UnityEditor.AddressableAssets;
  60. #endif
  61. using UnityEngine;
  62. using Object = UnityEngine.Object;
  63. #if UNITY_SCENE_MANAGER
  64. using UnityEngine.SceneManagement;
  65. using System.IO;
  66. #endif
  67. namespace vietlabs.fr2
  68. {
  69. public class FR2_Unity
  70. {
  71. internal static readonly AssetType[] FILTERS =
  72. {
  73. new AssetType("Scene", ".unity"),
  74. new AssetType("Prefab", ".prefab"),
  75. new AssetType("Model", ".3df", ".3dm", ".3dmf", ".3dv", ".3dx", ".c5d", ".lwo", ".lws", ".ma", ".mb",
  76. ".mesh", ".vrl", ".wrl", ".wrz", ".fbx", ".dae", ".3ds", ".dxf", ".obj", ".skp", ".max", ".blend"),
  77. new AssetType("Material", ".mat", ".cubemap", ".physicsmaterial"),
  78. new AssetType("Texture", ".ai", ".apng", ".png", ".bmp", ".cdr", ".dib", ".eps", ".exif", ".ico", ".icon",
  79. ".j", ".j2c", ".j2k", ".jas", ".jiff", ".jng", ".jp2", ".jpc", ".jpe", ".jpeg", ".jpf", ".jpg", "jpw",
  80. "jpx", "jtf", ".mac", ".omf", ".qif", ".qti", "qtif", ".tex", ".tfw", ".tga", ".tif", ".tiff", ".wmf",
  81. ".psd", ".exr", ".rendertexture"),
  82. new AssetType("Video", ".asf", ".asx", ".avi", ".dat", ".divx", ".dvx", ".mlv", ".m2l", ".m2t", ".m2ts",
  83. ".m2v", ".m4e", ".m4v", "mjp", ".mov", ".movie", ".mp21", ".mp4", ".mpe", ".mpeg", ".mpg", ".mpv2",
  84. ".ogm", ".qt", ".rm", ".rmvb", ".wmv", ".xvid", ".flv"),
  85. new AssetType("Audio", ".mp3", ".wav", ".ogg", ".aif", ".aiff", ".mod", ".it", ".s3m", ".xm"),
  86. new AssetType("Script", ".cs", ".js", ".boo"),
  87. new AssetType("Text", ".txt", ".json", ".xml", ".bytes", ".sql"),
  88. new AssetType("Shader", ".shader", ".cginc"),
  89. new AssetType("Animation", ".anim", ".controller", ".overridecontroller", ".mask"),
  90. new AssetType("Unity Asset", ".asset", ".guiskin", ".flare", ".fontsettings", ".prefs"),
  91. new AssetType("Others") //
  92. };
  93. public static HashSet<string> _Selection_AssetGUIDs;
  94. public static bool StringStartsWith(string source, params string[] prefixes)
  95. {
  96. if (string.IsNullOrEmpty(source)) return false;
  97. for (var i = 0; i < prefixes.Length; i++)
  98. {
  99. if (source.StartsWith(prefixes[i])) return true;
  100. }
  101. return false;
  102. }
  103. public static void SplitPath(string assetPath, out string assetName, out string assetExtension, out string assetFolder)
  104. {
  105. assetName = string.Empty;
  106. assetExtension = string.Empty;
  107. assetFolder = string.Empty;
  108. if (string.IsNullOrEmpty(assetPath)) return;
  109. var lastSlash = assetPath.LastIndexOf("/", StringComparison.Ordinal) + 1;
  110. var lastDot = assetPath.LastIndexOf(".", StringComparison.Ordinal);
  111. assetName = assetPath.Substring(lastSlash, assetPath.Length - lastSlash);
  112. assetExtension = (lastDot > lastSlash && lastDot > 0) ? assetPath.Substring(lastDot, assetPath.Length - lastDot).ToLower() : string.Empty;
  113. assetFolder = assetPath.Substring(0, lastSlash);
  114. }
  115. public static string[] Selection_AssetGUIDs
  116. {
  117. get
  118. {
  119. #if UNITY_5_0_OR_NEWER
  120. var objs = Selection.objects;
  121. _Selection_AssetGUIDs = new HashSet<string>();
  122. foreach(var item in objs)
  123. {
  124. #if UNITY_2018_1_OR_NEWER
  125. {
  126. var guid = "";
  127. long fileid =-1;
  128. try
  129. { // missing references will cause null exception
  130. if(AssetDatabase.TryGetGUIDAndLocalFileIdentifier(item, out guid, out fileid))
  131. {
  132. _Selection_AssetGUIDs.Add(guid+"/"+ fileid);
  133. //Debug.Log("guid: " + guid + " fileID: " + fileid);
  134. }
  135. } catch {}
  136. }
  137. #else
  138. {
  139. var path = AssetDatabase.GetAssetPath(item);
  140. if (string.IsNullOrEmpty(path)) continue;
  141. var guid = AssetDatabase.AssetPathToGUID(path);
  142. System.Reflection.PropertyInfo inspectorModeInfo =
  143. typeof(SerializedObject).GetProperty("inspectorMode", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
  144. SerializedObject serializedObject = new SerializedObject(item);
  145. inspectorModeInfo.SetValue(serializedObject, InspectorMode.Debug, null);
  146. SerializedProperty localIdProp =
  147. serializedObject.FindProperty("m_LocalIdentfierInFile"); //note the misspelling!
  148. var localId = localIdProp.longValue;
  149. if (localId <= 0)
  150. {
  151. localId = localIdProp.intValue;
  152. }
  153. if (localId <= 0)
  154. {
  155. continue;
  156. }
  157. if (!string.IsNullOrEmpty(guid)) _Selection_AssetGUIDs.Add(guid + "/" + localId);
  158. }
  159. #endif
  160. }
  161. return Selection.assetGUIDs;
  162. #else
  163. var mInfo =
  164. typeof(Selection).GetProperty("assetGUIDs", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static);
  165. if (mInfo != null){
  166. return (string[]) mInfo.GetValue(null, null);
  167. }
  168. Debug.LogWarning("Unity changed ! Selection.assetGUIDs not found !");
  169. return new string[0];
  170. #endif
  171. }
  172. }
  173. public static readonly Dictionary<int, string> HashClassesNormal = new Dictionary<int, string>
  174. {
  175. {1, "UnityEngine.GameObject"},
  176. {2, "UnityEngine.Component"},
  177. {4, "UnityEngine.Transform"},
  178. {8, "UnityEngine.Behaviour"},
  179. {12, "UnityEngine.ParticleAnimator"},
  180. {15, "UnityEngine.EllipsoidParticleEmitter"},
  181. {20, "UnityEngine.Camera"},
  182. {21, "UnityEngine.Material"},
  183. {23, "UnityEngine.MeshRenderer"},
  184. {25, "UnityEngine.Renderer"},
  185. {26, "UnityEngine.ParticleRenderer"},
  186. {27, "UnityEngine.Texture"},
  187. {28, "UnityEngine.Texture2D"},
  188. {33, "UnityEngine.MeshFilter"},
  189. {41, "UnityEngine.OcclusionPortal"},
  190. {43, "UnityEngine.Mesh"},
  191. {45, "UnityEngine.Skybox"},
  192. {47, "UnityEngine.QualitySettings"},
  193. {48, "UnityEngine.Shader"},
  194. {49, "UnityEngine.TextAsset"},
  195. {50, "UnityEngine.Rigidbody2D"},
  196. {53, "UnityEngine.Collider2D"},
  197. {54, "UnityEngine.Rigidbody"},
  198. {56, "UnityEngine.Collider"},
  199. {57, "UnityEngine.Joint"},
  200. {58, "UnityEngine.CircleCollider2D"},
  201. {59, "UnityEngine.HingeJoint"},
  202. {60, "UnityEngine.PolygonCollider2D"},
  203. {61, "UnityEngine.BoxCollider2D"},
  204. {62, "UnityEngine.PhysicsMaterial2D"},
  205. {64, "UnityEngine.MeshCollider"},
  206. {65, "UnityEngine.BoxCollider"},
  207. {68, "UnityEngine.EdgeCollider2D"},
  208. {72, "UnityEngine.ComputeShader"},
  209. {74, "UnityEngine.AnimationClip"},
  210. {75, "UnityEngine.ConstantForce"},
  211. {81, "UnityEngine.AudioListener"},
  212. {82, "UnityEngine.AudioSource"},
  213. {83, "UnityEngine.AudioClip"},
  214. {84, "UnityEngine.RenderTexture"},
  215. {87, "UnityEngine.MeshParticleEmitter"},
  216. {88, "UnityEngine.ParticleEmitter"},
  217. {89, "UnityEngine.Cubemap"},
  218. {90, "Avatar"},
  219. {92, "UnityEngine.GUILayer"},
  220. {93, "UnityEngine.RuntimeAnimatorController"},
  221. {95, "UnityEngine.Animator"},
  222. {96, "UnityEngine.TrailRenderer"},
  223. {102, "UnityEngine.TextMesh"},
  224. {104, "UnityEngine.RenderSettings"},
  225. {108, "UnityEngine.Light"},
  226. {111, "UnityEngine.Animation"},
  227. {114, "UnityEngine.MonoBehaviour"},
  228. {115, "UnityEditor.MonoScript"},
  229. {117, "UnityEngine.Texture3D"},
  230. {119, "UnityEngine.Projector"},
  231. {120, "UnityEngine.LineRenderer"},
  232. {121, "UnityEngine.Flare"},
  233. {123, "UnityEngine.LensFlare"},
  234. {124, "UnityEngine.FlareLayer"},
  235. {128, "UnityEngine.Font"},
  236. {129, "UnityEditor.PlayerSettings"},
  237. {131, "UnityEngine.GUITexture"},
  238. {132, "UnityEngine.GUIText"},
  239. {133, "UnityEngine.GUIElement"},
  240. {134, "UnityEngine.PhysicMaterial"},
  241. {135, "UnityEngine.SphereCollider"},
  242. {136, "UnityEngine.CapsuleCollider"},
  243. {137, "UnityEngine.SkinnedMeshRenderer"},
  244. {138, "UnityEngine.FixedJoint"},
  245. {142, "UnityEngine.AssetBundle"},
  246. {143, "UnityEngine.CharacterController"},
  247. {144, "UnityEngine.CharacterJoint"},
  248. {145, "UnityEngine.SpringJoint"},
  249. {146, "UnityEngine.WheelCollider"},
  250. {152, "UnityEngine.MovieTexture"},
  251. {153, "UnityEngine.ConfigurableJoint"},
  252. {154, "UnityEngine.TerrainCollider"},
  253. {156, "UnityEngine.TerrainData"},
  254. {157, "UnityEngine.LightmapSettings"},
  255. {158, "UnityEngine.WebCamTexture"},
  256. {159, "UnityEditor.EditorSettings"},
  257. {162, "UnityEditor.EditorUserSettings"},
  258. {164, "UnityEngine.AudioReverbFilter"},
  259. {165, "UnityEngine.AudioHighPassFilter"},
  260. {166, "UnityEngine.AudioChorusFilter"},
  261. {167, "UnityEngine.AudioReverbZone"},
  262. {168, "UnityEngine.AudioEchoFilter"},
  263. {169, "UnityEngine.AudioLowPassFilter"},
  264. {170, "UnityEngine.AudioDistortionFilter"},
  265. {171, "UnityEngine.SparseTexture"},
  266. {180, "UnityEngine.AudioBehaviour"},
  267. {182, "UnityEngine.WindZone"},
  268. {183, "UnityEngine.Cloth"},
  269. {192, "UnityEngine.OcclusionArea"},
  270. {193, "UnityEngine.Tree"},
  271. {198, "UnityEngine.ParticleSystem"},
  272. {199, "UnityEngine.ParticleSystemRenderer"},
  273. {200, "UnityEngine.ShaderVariantCollection"},
  274. {205, "UnityEngine.LODGroup"},
  275. {207, "UnityEngine.Motion"},
  276. {212, "UnityEngine.SpriteRenderer"},
  277. {213, "UnityEngine.Sprite"},
  278. {215, "UnityEngine.ReflectionProbe"},
  279. {218, "UnityEngine.Terrain"},
  280. {220, "UnityEngine.LightProbeGroup"},
  281. {221, "UnityEngine.AnimatorOverrideController"},
  282. {222, "UnityEngine.CanvasRenderer"},
  283. {223, "UnityEngine.Canvas"},
  284. {224, "UnityEngine.RectTransform"},
  285. {225, "UnityEngine.CanvasGroup"},
  286. {226, "UnityEngine.BillboardAsset"},
  287. {227, "UnityEngine.BillboardRenderer"},
  288. {229, "UnityEngine.AnchoredJoint2D"},
  289. {230, "UnityEngine.Joint2D"},
  290. {231, "UnityEngine.SpringJoint2D"},
  291. {232, "UnityEngine.DistanceJoint2D"},
  292. {233, "UnityEngine.HingeJoint2D"},
  293. {234, "UnityEngine.SliderJoint2D"},
  294. {235, "UnityEngine.WheelJoint2D"},
  295. {246, "UnityEngine.PhysicsUpdateBehaviour2D"},
  296. {247, "UnityEngine.ConstantForce2D"},
  297. {248, "UnityEngine.Effector2D"},
  298. {249, "UnityEngine.AreaEffector2D"},
  299. {250, "UnityEngine.PointEffector2D"},
  300. {251, "UnityEngine.PlatformEffector2D"},
  301. {252, "UnityEngine.SurfaceEffector2D"},
  302. {258, "UnityEngine.LightProbes"},
  303. {290, "UnityEngine.AssetBundleManifest"},
  304. {1003, "UnityEditor.AssetImporter"},
  305. {1004, "UnityEditor.AssetDatabase"},
  306. {1006, "UnityEditor.TextureImporter"},
  307. {1007, "UnityEditor.ShaderImporter"},
  308. {1011, "UnityEngine.AvatarMask"},
  309. {1020, "UnityEditor.AudioImporter"},
  310. {1029, "UnityEditor.DefaultAsset"},
  311. {1032, "UnityEditor.SceneAsset"},
  312. {1035, "UnityEditor.MonoImporter"},
  313. {1040, "UnityEditor.ModelImporter"},
  314. {1042, "UnityEditor.TrueTypeFontImporter"},
  315. {1044, "UnityEditor.MovieImporter"},
  316. {1045, "UnityEditor.EditorBuildSettings"},
  317. {1050, "UnityEditor.PluginImporter"},
  318. {1051, "UnityEditor.EditorUserBuildSettings"},
  319. {1105, "UnityEditor.HumanTemplate"},
  320. {1110, "UnityEditor.SpeedTreeImporter"},
  321. {1113, "UnityEditor.LightmapParameters"}
  322. };
  323. //private static Texture2D _whiteTexture;
  324. //public static Texture2D whiteTexture {
  325. // get {
  326. // return EditorGUIUtility.whiteTexture;
  327. // #if UNITY_5_0_OR_NEWER
  328. // return EditorGUIUtility.whiteTexture;
  329. // #else
  330. // if (_whiteTexture != null) return _whiteTexture;
  331. // _whiteTexture = new Texture2D(1,1, TextureFormat.RGBA32, false);
  332. // _whiteTexture.SetPixel(0, 0, Color.white);
  333. // _whiteTexture.hideFlags = HideFlags.DontSave;
  334. // return _whiteTexture;
  335. // #endif
  336. // }
  337. //}
  338. public static T LoadAssetAtPath<T>(string path) where T : Object
  339. {
  340. #if UNITY_5_1_OR_NEWER
  341. return AssetDatabase.LoadAssetAtPath<T>(path);
  342. #else
  343. return (T)AssetDatabase.LoadAssetAtPath(path, typeof(T));
  344. #endif
  345. }
  346. public static void SetWindowTitle(EditorWindow window, string title)
  347. {
  348. #if UNITY_5_1_OR_NEWER
  349. window.titleContent = new GUIContent(title);
  350. #else
  351. window.title = title;
  352. #endif
  353. }
  354. public static void GetCompilingPhase(string path, out bool isPlugin, out bool isEditor)
  355. {
  356. #if (UNITY_5_2_0 || UNITY_5_2_1) && !UNITY_5_2_OR_NEWER
  357. bool oldSystem = true;
  358. #else
  359. var oldSystem = false;
  360. #endif
  361. // ---- Old system: Editor for the plugin should be Plugins/Editor
  362. if (oldSystem)
  363. {
  364. bool isPluginEditor = path.StartsWith("Assets/Plugins/Editor/", StringComparison.Ordinal)
  365. || path.StartsWith("Assets/Standard Assets/Editor/", StringComparison.Ordinal)
  366. || path.StartsWith("Assets/Pro Standard Assets/Editor/",
  367. StringComparison.Ordinal);
  368. if (isPluginEditor)
  369. {
  370. isPlugin = true;
  371. isEditor = true;
  372. return;
  373. }
  374. }
  375. isPlugin = path.StartsWith("Assets/Plugins/", StringComparison.Ordinal)
  376. || path.StartsWith("Assets/Standard Assets/", StringComparison.Ordinal)
  377. || path.StartsWith("Assets/Pro Standard Assets/", StringComparison.Ordinal);
  378. isEditor = oldSystem && isPlugin ? false : path.Contains("/Editor/");
  379. }
  380. public static T LoadAssetWithGUID<T>(string guid) where T : Object
  381. {
  382. if (string.IsNullOrEmpty(guid))
  383. {
  384. return null;
  385. }
  386. string path = AssetDatabase.GUIDToAssetPath(guid);
  387. if (string.IsNullOrEmpty(path))
  388. {
  389. return null;
  390. }
  391. #if UNITY_5_1_OR_NEWER
  392. return AssetDatabase.LoadAssetAtPath<T>(path);
  393. #else
  394. return (T)AssetDatabase.LoadAssetAtPath(path, typeof(T));
  395. #endif
  396. }
  397. public static void UnloadUnusedAssets()
  398. {
  399. #if UNITY_5_0_OR_NEWER
  400. EditorUtility.UnloadUnusedAssetsImmediate();
  401. #else
  402. EditorUtility.UnloadUnusedAssets();
  403. #endif
  404. Resources.UnloadUnusedAssets();
  405. }
  406. internal static int Epoch(DateTime time)
  407. {
  408. return (int) (time.ToUniversalTime() - new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc)).TotalSeconds;
  409. }
  410. internal static bool DrawToggle(ref bool v, string label)
  411. {
  412. bool v1 = GUILayout.Toggle(v, label);
  413. if (v1 != v)
  414. {
  415. v = v1;
  416. return true;
  417. }
  418. return false;
  419. }
  420. internal static bool DrawToggleToolbar(ref bool v, string label, float width)
  421. {
  422. bool v1 = GUILayout.Toggle(v, label, EditorStyles.toolbarButton, GUILayout.Width(width));
  423. if (v1 != v)
  424. {
  425. v = v1;
  426. return true;
  427. }
  428. return false;
  429. }
  430. internal static bool DrawToggleToolbar(ref bool v, GUIContent icon, float width)
  431. {
  432. bool v1 = GUILayout.Toggle(v, icon, EditorStyles.toolbarButton, GUILayout.Width(width));
  433. if (v1 != v)
  434. {
  435. v = v1;
  436. return true;
  437. }
  438. return false;
  439. }
  440. public static string GetAddressable(string guid)
  441. {
  442. #if FR2_ADDRESSABLE
  443. var aaSettings = AddressableAssetSettingsDefaultObject.GetSettings(true);
  444. var entry = aaSettings.FindAssetEntry(guid);
  445. return entry != null ? entry.address : string.Empty;
  446. #endif
  447. return null;
  448. }
  449. internal static EditorWindow FindEditor(string className)
  450. {
  451. EditorWindow[] list = Resources.FindObjectsOfTypeAll<EditorWindow>();
  452. foreach (EditorWindow item in list)
  453. {
  454. if (item.GetType().FullName == className)
  455. {
  456. return item;
  457. }
  458. }
  459. return null;
  460. }
  461. internal static void RepaintAllEditor(string className)
  462. {
  463. EditorWindow[] list = Resources.FindObjectsOfTypeAll<EditorWindow>();
  464. foreach (EditorWindow item in list)
  465. {
  466. #if FR2_DEV
  467. Debug.Log(item.GetType().FullName);
  468. #endif
  469. if (item.GetType().FullName != className)
  470. {
  471. continue;
  472. }
  473. item.Repaint();
  474. }
  475. }
  476. internal static void RepaintProjectWindows()
  477. {
  478. RepaintAllEditor("UnityEditor.ProjectBrowser");
  479. }
  480. internal static void RepaintFR2Windows()
  481. {
  482. RepaintAllEditor("vietlabs.fr2.FR2_Window");
  483. }
  484. internal static void ExportSelection()
  485. {
  486. Type packageExportT = null;
  487. foreach (Assembly assembly in AppDomain.CurrentDomain.GetAssemblies())
  488. {
  489. packageExportT = assembly.GetType("UnityEditor.PackageExport");
  490. if (packageExportT != null)
  491. {
  492. break;
  493. }
  494. }
  495. if (packageExportT == null)
  496. {
  497. Debug.LogWarning("Export Package Error : UnityEditor.PackageExport not found !");
  498. return;
  499. }
  500. EditorWindow panel = EditorWindow.GetWindow(packageExportT, true, "Exporting package");
  501. #if UNITY_5_2_OR_NEWER
  502. var prop = "m_IncludeDependencies";
  503. #else
  504. var prop = "m_bIncludeDependencies";
  505. #endif
  506. FieldInfo fieldInfo = packageExportT.GetField(prop, BindingFlags.NonPublic | BindingFlags.Instance);
  507. if (fieldInfo == null)
  508. {
  509. Debug.LogWarning("Export Package error : " + prop + " not found !");
  510. return;
  511. }
  512. MethodInfo methodInfo =
  513. packageExportT.GetMethod("BuildAssetList", BindingFlags.NonPublic | BindingFlags.Instance);
  514. if (methodInfo == null)
  515. {
  516. Debug.LogWarning("Export Package error : BuildAssetList method not found !");
  517. return;
  518. }
  519. fieldInfo.SetValue(panel, false);
  520. methodInfo.Invoke(panel, null);
  521. panel.Repaint();
  522. }
  523. public static Type GetType(string typeName)
  524. {
  525. Type type = Type.GetType(typeName);
  526. if (type != null)
  527. {
  528. return type;
  529. }
  530. foreach (Assembly a in AppDomain.CurrentDomain.GetAssemblies())
  531. {
  532. type = a.GetType(typeName);
  533. if (type != null)
  534. {
  535. return type;
  536. }
  537. }
  538. return null;
  539. }
  540. public static IEnumerable<Transform> GetAllChild(Transform root)
  541. {
  542. yield return root;
  543. if (root.childCount <= 0)
  544. {
  545. yield break;
  546. }
  547. for (var i = 0; i < root.childCount; i++)
  548. {
  549. foreach (Transform item in GetAllChild(root.GetChild(i)))
  550. {
  551. yield return item;
  552. }
  553. }
  554. }
  555. public static IEnumerable<GameObject> getAllObjsInCurScene()
  556. {
  557. #if UNITY_SCENE_MANAGER
  558. for (var j = 0; j < SceneManager.sceneCount; j++)
  559. {
  560. Scene scene = SceneManager.GetSceneAt(j);
  561. foreach (GameObject item in GetGameObjectsInScene(scene))
  562. {
  563. yield return item;
  564. }
  565. }
  566. if (EditorApplication.isPlaying)
  567. {
  568. //dont destroy scene
  569. GameObject temp = null;
  570. try
  571. {
  572. temp = new GameObject();
  573. Object.DontDestroyOnLoad(temp);
  574. Scene dontDestroyOnLoad = temp.scene;
  575. Object.DestroyImmediate(temp);
  576. temp = null;
  577. foreach (GameObject item in GetGameObjectsInScene(dontDestroyOnLoad))
  578. {
  579. yield return item;
  580. }
  581. }
  582. finally
  583. {
  584. if (temp != null)
  585. {
  586. Object.DestroyImmediate(temp);
  587. }
  588. }
  589. }
  590. #else
  591. foreach (Transform obj in Resources.FindObjectsOfTypeAll(typeof(Transform)))
  592. {
  593. GameObject o = obj.gameObject;
  594. yield return o;
  595. }
  596. #endif
  597. }
  598. #if UNITY_SCENE_MANAGER
  599. private static IEnumerable<GameObject> GetGameObjectsInScene(Scene scene)
  600. {
  601. var rootObjects = new List<GameObject>();
  602. if (!scene.isLoaded)
  603. {
  604. yield break;
  605. }
  606. scene.GetRootGameObjects(rootObjects);
  607. // iterate root objects and do something
  608. for (var i = 0; i < rootObjects.Count; ++i)
  609. {
  610. GameObject gameObject = rootObjects[i];
  611. foreach (GameObject item in getAllChild(gameObject))
  612. {
  613. yield return item;
  614. }
  615. yield return gameObject;
  616. }
  617. }
  618. #endif
  619. public static IEnumerable<GameObject> getAllChild(GameObject target, bool returnMe = false)
  620. {
  621. if (returnMe)
  622. {
  623. yield return target;
  624. }
  625. if (target.transform.childCount > 0)
  626. {
  627. for (var i = 0; i < target.transform.childCount; i++)
  628. {
  629. yield return target.transform.GetChild(i).gameObject;
  630. foreach (GameObject item in getAllChild(target.transform.GetChild(i).gameObject, false))
  631. {
  632. yield return item;
  633. }
  634. }
  635. }
  636. }
  637. public static IEnumerable<Object> GetAllRefObjects(GameObject obj)
  638. {
  639. Component[] components = obj.GetComponents<Component>();
  640. foreach (Component com in components)
  641. {
  642. if (com == null)
  643. {
  644. continue;
  645. }
  646. var serialized = new SerializedObject(com);
  647. SerializedProperty it = serialized.GetIterator().Copy();
  648. while (it.NextVisible(true))
  649. {
  650. if (it.propertyType != SerializedPropertyType.ObjectReference)
  651. {
  652. continue;
  653. }
  654. if (it.objectReferenceValue == null)
  655. {
  656. continue;
  657. }
  658. yield return it.objectReferenceValue;
  659. }
  660. }
  661. }
  662. public static int StringMatch(string pattern, string input)
  663. {
  664. if (input == pattern)
  665. {
  666. return int.MaxValue;
  667. }
  668. if (input.Contains(pattern))
  669. {
  670. return int.MaxValue - 1;
  671. }
  672. var pidx = 0;
  673. var score = 0;
  674. var tokenScore = 0;
  675. for (var i = 0; i < input.Length; i++)
  676. {
  677. char ch = input[i];
  678. if (ch == pattern[pidx])
  679. {
  680. tokenScore += tokenScore + 1; //increasing score for continuos token
  681. pidx++;
  682. if (pidx >= pattern.Length)
  683. {
  684. break;
  685. }
  686. }
  687. else
  688. {
  689. tokenScore = 0;
  690. }
  691. score += tokenScore;
  692. }
  693. return score;
  694. }
  695. public static int GetIndex(string ext)
  696. {
  697. for (var i = 0; i < FILTERS.Length - 1; i++)
  698. {
  699. if (FILTERS[i].extension.Contains(ext))
  700. {
  701. return i;
  702. }
  703. }
  704. return FILTERS.Length - 1; //Others
  705. }
  706. public static void GuiLine(int i_height = 1)
  707. {
  708. Rect rect = EditorGUILayout.GetControlRect(false, i_height);
  709. rect.height = i_height;
  710. EditorGUI.DrawRect(rect, new Color(0.5f, 0.5f, 0.5f, 1));
  711. }
  712. public static bool IsInAsset(GameObject obj)
  713. {
  714. //#if UNITY_5_3_OR_NEWER
  715. // this not working in new empty created scene
  716. //return string.IsNullOrEmpty(obj.scene.name);
  717. //#else
  718. return !string.IsNullOrEmpty(AssetDatabase.GetAssetPath(obj));
  719. //#endif
  720. }
  721. #if FR2_DEBUG
  722. [MenuItem("Tools/Test Prefab")]
  723. static void TestPrefab()
  724. {
  725. GetPrefabParent(Selection.activeGameObject);
  726. }
  727. #endif
  728. public static string GetPrefabParent(Object obj)
  729. {
  730. #if UNITY_2018_3_OR_NEWER
  731. string prefabPath = PrefabUtility.GetPrefabAssetPathOfNearestInstanceRoot(obj);
  732. #elif UNITY_2018_2_OR_NEWER
  733. var prefab = PrefabUtility.GetCorrespondingObjectFromSource(obj);
  734. string prefabPath = AssetDatabase.GetAssetPath(prefab);
  735. #else
  736. var hierarchy_root = PrefabUtility.FindPrefabRoot((GameObject)obj);
  737. var prefab = PrefabUtility.GetPrefabParent(hierarchy_root);
  738. var prefabPath = AssetDatabase.GetAssetPath(prefab);
  739. #endif
  740. return AssetDatabase.AssetPathToGUID(prefabPath);
  741. }
  742. public static string GetGameObjectPath(GameObject obj, bool includeMe = true)
  743. {
  744. if (obj == null)
  745. {
  746. return string.Empty;
  747. }
  748. string path = includeMe ? "/" + obj.name : "/";
  749. while (obj.transform.parent != null)
  750. {
  751. obj = obj.transform.parent.gameObject;
  752. path = "/" + obj.name + path;
  753. }
  754. path = path.TrimStart('/');
  755. return path;
  756. }
  757. public static bool CheckIsPrefab(GameObject obj)
  758. {
  759. #if UNITY_2018_3_OR_NEWER
  760. // var t = PrefabUtility.GetPrefabAssetType(obj);
  761. // var isPrefab = (t != PrefabAssetType.NotAPrefab) && (t != PrefabAssetType.MissingAsset);
  762. return PrefabUtility.IsAnyPrefabInstanceRoot(obj);
  763. #else
  764. return PrefabUtility.GetPrefabType(obj) != PrefabType.None;
  765. #endif
  766. }
  767. public static TerrainTextureData[] GetTerrainTextureDatas(TerrainData data)
  768. {
  769. #if UNITY_2018_3_OR_NEWER
  770. var arr = new TerrainTextureData[data.terrainLayers.Length];
  771. for (var i = 0; i < data.terrainLayers.Length; i++)
  772. {
  773. TerrainLayer layer = data.terrainLayers[i];
  774. arr[i] = new TerrainTextureData
  775. (
  776. layer.normalMapTexture,
  777. layer.maskMapTexture,
  778. layer.diffuseTexture
  779. );
  780. }
  781. return arr;
  782. #else
  783. var arr = new TerrainTextureData[data.splatPrototypes.Length];
  784. for(int i = 0; i < data.splatPrototypes.Length; i++)
  785. {
  786. var layer = data.splatPrototypes[i];
  787. arr[i] = new TerrainTextureData
  788. (
  789. layer.normalMap,
  790. layer.texture
  791. );
  792. }
  793. return arr;
  794. #endif
  795. }
  796. public static int ReplaceTerrainTextureDatas(TerrainData terrain, Texture2D fromObj, Texture2D toObj)
  797. {
  798. var found = 0;
  799. #if UNITY_2018_3_OR_NEWER
  800. TerrainLayer[] arr3 = terrain.terrainLayers;
  801. for (var i = 0; i < arr3.Length; i++)
  802. {
  803. if (arr3[i].normalMapTexture == fromObj)
  804. {
  805. found++;
  806. arr3[i].normalMapTexture = toObj;
  807. }
  808. if (arr3[i].maskMapTexture == fromObj)
  809. {
  810. found++;
  811. arr3[i].maskMapTexture = toObj;
  812. }
  813. if (arr3[i].diffuseTexture == fromObj)
  814. {
  815. found++;
  816. arr3[i].diffuseTexture = toObj;
  817. }
  818. }
  819. terrain.terrainLayers = arr3;
  820. #else
  821. var arr3 = terrain.splatPrototypes;
  822. for (var i = 0; i < arr3.Length; i++)
  823. {
  824. if (arr3[i].texture == fromObj)
  825. {
  826. found++;
  827. arr3[i].texture = toObj;
  828. }
  829. if (arr3[i].normalMap == fromObj)
  830. {
  831. found++;
  832. arr3[i].normalMap = toObj;
  833. }
  834. }
  835. terrain.splatPrototypes = arr3;
  836. #endif
  837. return found;
  838. }
  839. public static void Clear<T1, T2>(ref Dictionary<T1, T2> dict)
  840. {
  841. if (dict == null)
  842. {
  843. dict = new Dictionary<T1, T2>();
  844. }
  845. else
  846. {
  847. dict.Clear();
  848. }
  849. }
  850. public static void Clear<T>(ref List<T> list)
  851. {
  852. if (list == null)
  853. {
  854. list = new List<T>();
  855. }
  856. else
  857. {
  858. list.Clear();
  859. }
  860. }
  861. public static SerializedProperty[] xGetSerializedProperties(Object go, bool processArray)
  862. {
  863. var so = new SerializedObject(go);
  864. so.Update();
  865. var result = new List<SerializedProperty>();
  866. SerializedProperty iterator = so.GetIterator();
  867. while (iterator.NextVisible(true))
  868. {
  869. SerializedProperty copy = iterator.Copy();
  870. if (processArray && iterator.isArray)
  871. {
  872. result.AddRange(xGetSOArray(copy));
  873. }
  874. else
  875. {
  876. result.Add(copy);
  877. }
  878. }
  879. return result.ToArray();
  880. }
  881. public static List<SerializedProperty> xGetSOArray(SerializedProperty prop)
  882. {
  883. int size = prop.arraySize;
  884. var result = new List<SerializedProperty>();
  885. for (var i = 0; i < size; i++)
  886. {
  887. SerializedProperty p = prop.GetArrayElementAtIndex(i);
  888. if (p.isArray)
  889. {
  890. result.AddRange(xGetSOArray(p.Copy()));
  891. }
  892. else
  893. {
  894. result.Add(p.Copy());
  895. }
  896. }
  897. return result;
  898. }
  899. public class TerrainTextureData
  900. {
  901. public Texture2D[] textures;
  902. public TerrainTextureData(params Texture2D[] param)
  903. {
  904. var count = 0;
  905. if (param != null)
  906. {
  907. count = param.Length;
  908. }
  909. textures = new Texture2D[count];
  910. for (var i = 0; i < count; i++)
  911. {
  912. textures[i] = param[i];
  913. }
  914. }
  915. }
  916. public static void BackupAndDeleteAssets(FR2_Ref[] assets)
  917. {
  918. var fileName = DateTime.Now.ToString("yyMMdd_hhmmss");
  919. var list = assets;
  920. var result = new List<string>();
  921. var selectedList = new List<string>();
  922. foreach (var item in list)
  923. {
  924. if (item.asset == null) continue;
  925. var oPath = item.asset.assetPath.Replace("\\", "/");
  926. if (!oPath.StartsWith("Assets/")) continue;
  927. result.Add(item.asset.assetPath);
  928. if (item.isSelected())
  929. {
  930. selectedList.Add(item.asset.assetPath);
  931. }
  932. }
  933. if (selectedList.Count != 0) result = selectedList;
  934. Directory.CreateDirectory("Library/FR2/");
  935. AssetDatabase.ExportPackage(result.ToArray(), "Library/FR2/bk_" + fileName + ".unitypackage");
  936. AssetDatabase.StartAssetEditing();
  937. try
  938. {
  939. for (var i = 0; i < result.Count; i++)
  940. {
  941. AssetDatabase.DeleteAsset(result[i]);
  942. }
  943. }
  944. finally
  945. {
  946. AssetDatabase.StopAssetEditing();
  947. AssetDatabase.Refresh();
  948. }
  949. FR2_Cache.DelayCheck4Changes();
  950. }
  951. }
  952. }