EditrixUtility.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. 
  2. /*WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW*\ ( ( ) )
  3. |/ \| ) ) _((_
  4. || (c) Wanzyee Studio < wanzyeestudio.blogspot.com > || ( ( |_ _ |=n
  5. |\ /| _____)) | ! ] U
  6. \.ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ./ (_(__(S) |___*/
  7. using UnityEngine;
  8. using UnityEditor;
  9. using System;
  10. using System.Collections.Generic;
  11. using System.IO;
  12. using System.Linq;
  13. using Object = UnityEngine.Object;
  14. namespace WanzyeeStudio.Editrix{
  15. /// <summary>
  16. /// Include some convenient methods for editor or asset operation.
  17. /// </summary>
  18. public static class EditrixUtility{
  19. /// <summary>
  20. /// Get all main assets in the project folder.
  21. /// </summary>
  22. /// <returns>The all assets.</returns>
  23. /// <param name="progressBar">If set to <c>true</c> show progress bar while scanning.</param>
  24. /*
  25. * The filter includes "ScriptableObject" 'coz of the bug in Unity 5.2.2.
  26. */
  27. public static Object[] GetAllAssets(bool progressBar = false){
  28. var _g = AssetDatabase.FindAssets("t:Object t:ScriptableObject");
  29. var _p = _g.Select(_v => AssetDatabase.GUIDToAssetPath(_v)).OrderBy(_v => _v).ToArray();
  30. var _a = new List<Object>();
  31. for(int i = 0; i < _p.Length; i++){
  32. if(progressBar) EditorUtility.DisplayProgressBar("Scan Assets", _p[i], (float)i / (float)_p.Length);
  33. _a.Add(AssetDatabase.LoadMainAssetAtPath(_p[i]));
  34. }
  35. if(progressBar) EditorUtility.ClearProgressBar();
  36. return _a.Where(_v => null != _v).Distinct().ToArray();
  37. }
  38. /// <summary>
  39. /// Get all asset labels used in project, or only find the ones used by assigned assets.
  40. /// </summary>
  41. /// <returns>The asset labels.</returns>
  42. /// <param name="assets">Assets.</param>
  43. public static string[] GetAllAssetLabels(params Object[] assets){
  44. if(null == assets || 0 == assets.Length) assets = GetAllAssets();
  45. return assets.Distinct(
  46. ).Where(_v => (null != _v && AssetDatabase.Contains(_v))
  47. ).SelectMany(_v => AssetDatabase.GetLabels(_v)
  48. ).Distinct(
  49. ).OrderBy(_v => _v
  50. ).ToArray();
  51. }
  52. /// <summary>
  53. /// Get an order <c>string</c> of given object for sorting.
  54. /// </summary>
  55. ///
  56. /// <remarks>
  57. /// It's asset path, append with sibling if relative to <c>UnityEngine.GameObject</c>.
  58. /// Optional to sort asset or hierarchy object first.
  59. /// </remarks>
  60. ///
  61. /// <returns>The order.</returns>
  62. /// <param name="source">Source object.</param>
  63. /// <param name="assetFirst">If set to <c>true</c> asset first.</param>
  64. ///
  65. public static string GetObjectOrder(Object source, bool assetFirst = true){
  66. var _p = AssetDatabase.GetAssetPath(source);
  67. if(!AssetDatabase.Contains(source)) _p = "h_" + _p;
  68. else _p = (assetFirst ? "a_" : "p_") + _p;
  69. Func<Transform, string> _h = (o) => {
  70. var _s = "";
  71. for(var t = o; null != t; t = t.parent) _s = "." + t.GetSiblingIndex().ToString("D7") + _s;
  72. return _s;
  73. };
  74. if(source is GameObject) return _p + _h.Invoke(((GameObject)source).transform);
  75. else if(!(source is Component)) return _p;
  76. var _i = "#" + Array.IndexOf(((Component)source).GetComponents<Component>(), (Component)source);
  77. return _p + _h.Invoke(((Component)source).transform) + _i;
  78. }
  79. /// <summary>
  80. /// Determine if the path can be used to create a file or directory.
  81. /// </summary>
  82. ///
  83. /// <remarks>
  84. /// Optional to throw an exception message or just return <c>false</c> if invalid.
  85. /// Check <c>IoUtility.CheckCreatable()</c> at the first.
  86. /// Then return <c>true</c> if the file doesn't exist yet or force to <c>overwrite</c>.
  87. /// Otherwise popup a dialog for the user to make the decision.
  88. /// </remarks>
  89. ///
  90. /// <returns><c>true</c> if is creatable; otherwise, <c>false</c>.</returns>
  91. /// <param name="path">Path.</param>
  92. /// <param name="overwrite">Overwrite.</param>
  93. /// <param name="exception">Flag to throw an exception or return <c>false</c>.</param>
  94. ///
  95. public static bool CheckIoCreatable(string path, bool overwrite = false, bool exception = false){
  96. if(!IoUtility.CheckCreatable(path, exception)) return false;
  97. if(overwrite || !File.Exists(path)) return true;
  98. var _m = Path.GetFileName(path) + " already exists.\nDo you want to replace it?";
  99. if(EditorUtility.DisplayDialog("Confirm Save As", _m, "Yes", "No")) return true;
  100. if(exception) throw new OperationCanceledException(path + " already exists.");
  101. else return false;
  102. }
  103. }
  104. }