/*WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW*\ ( ( ) ) |/ \| ) ) _((_ || (c) Wanzyee Studio < wanzyeestudio.blogspot.com > || ( ( |_ _ |=n |\ /| _____)) | ! ] U \.ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ./ (_(__(S) |___*/ using UnityEngine; using UnityEditor; using System; using System.Collections.Generic; using System.IO; using System.Linq; using Object = UnityEngine.Object; namespace WanzyeeStudio.Editrix{ /// /// Include some convenient methods for editor or asset operation. /// public static class EditrixUtility{ /// /// Get all main assets in the project folder. /// /// The all assets. /// If set to true show progress bar while scanning. /* * The filter includes "ScriptableObject" 'coz of the bug in Unity 5.2.2. */ public static Object[] GetAllAssets(bool progressBar = false){ var _g = AssetDatabase.FindAssets("t:Object t:ScriptableObject"); var _p = _g.Select(_v => AssetDatabase.GUIDToAssetPath(_v)).OrderBy(_v => _v).ToArray(); var _a = new List(); for(int i = 0; i < _p.Length; i++){ if(progressBar) EditorUtility.DisplayProgressBar("Scan Assets", _p[i], (float)i / (float)_p.Length); _a.Add(AssetDatabase.LoadMainAssetAtPath(_p[i])); } if(progressBar) EditorUtility.ClearProgressBar(); return _a.Where(_v => null != _v).Distinct().ToArray(); } /// /// Get all asset labels used in project, or only find the ones used by assigned assets. /// /// The asset labels. /// Assets. public static string[] GetAllAssetLabels(params Object[] assets){ if(null == assets || 0 == assets.Length) assets = GetAllAssets(); return assets.Distinct( ).Where(_v => (null != _v && AssetDatabase.Contains(_v)) ).SelectMany(_v => AssetDatabase.GetLabels(_v) ).Distinct( ).OrderBy(_v => _v ).ToArray(); } /// /// Get an order string of given object for sorting. /// /// /// /// It's asset path, append with sibling if relative to UnityEngine.GameObject. /// Optional to sort asset or hierarchy object first. /// /// /// The order. /// Source object. /// If set to true asset first. /// public static string GetObjectOrder(Object source, bool assetFirst = true){ var _p = AssetDatabase.GetAssetPath(source); if(!AssetDatabase.Contains(source)) _p = "h_" + _p; else _p = (assetFirst ? "a_" : "p_") + _p; Func _h = (o) => { var _s = ""; for(var t = o; null != t; t = t.parent) _s = "." + t.GetSiblingIndex().ToString("D7") + _s; return _s; }; if(source is GameObject) return _p + _h.Invoke(((GameObject)source).transform); else if(!(source is Component)) return _p; var _i = "#" + Array.IndexOf(((Component)source).GetComponents(), (Component)source); return _p + _h.Invoke(((Component)source).transform) + _i; } /// /// Determine if the path can be used to create a file or directory. /// /// /// /// Optional to throw an exception message or just return false if invalid. /// Check IoUtility.CheckCreatable() at the first. /// Then return true if the file doesn't exist yet or force to overwrite. /// Otherwise popup a dialog for the user to make the decision. /// /// /// true if is creatable; otherwise, false. /// Path. /// Overwrite. /// Flag to throw an exception or return false. /// public static bool CheckIoCreatable(string path, bool overwrite = false, bool exception = false){ if(!IoUtility.CheckCreatable(path, exception)) return false; if(overwrite || !File.Exists(path)) return true; var _m = Path.GetFileName(path) + " already exists.\nDo you want to replace it?"; if(EditorUtility.DisplayDialog("Confirm Save As", _m, "Yes", "No")) return true; if(exception) throw new OperationCanceledException(path + " already exists."); else return false; } } }