123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135 |
-
- /*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{
- /// <summary>
- /// Include some convenient methods for editor or asset operation.
- /// </summary>
- public static class EditrixUtility{
- /// <summary>
- /// Get all main assets in the project folder.
- /// </summary>
- /// <returns>The all assets.</returns>
- /// <param name="progressBar">If set to <c>true</c> show progress bar while scanning.</param>
- /*
- * 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<Object>();
- 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();
- }
- /// <summary>
- /// Get all asset labels used in project, or only find the ones used by assigned assets.
- /// </summary>
- /// <returns>The asset labels.</returns>
- /// <param name="assets">Assets.</param>
- 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();
- }
-
- /// <summary>
- /// Get an order <c>string</c> of given object for sorting.
- /// </summary>
- ///
- /// <remarks>
- /// It's asset path, append with sibling if relative to <c>UnityEngine.GameObject</c>.
- /// Optional to sort asset or hierarchy object first.
- /// </remarks>
- ///
- /// <returns>The order.</returns>
- /// <param name="source">Source object.</param>
- /// <param name="assetFirst">If set to <c>true</c> asset first.</param>
- ///
- 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<Transform, string> _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>(), (Component)source);
- return _p + _h.Invoke(((Component)source).transform) + _i;
- }
- /// <summary>
- /// Determine if the path can be used to create a file or directory.
- /// </summary>
- ///
- /// <remarks>
- /// Optional to throw an exception message or just return <c>false</c> if invalid.
- /// Check <c>IoUtility.CheckCreatable()</c> at the first.
- /// Then return <c>true</c> if the file doesn't exist yet or force to <c>overwrite</c>.
- /// Otherwise popup a dialog for the user to make the decision.
- /// </remarks>
- ///
- /// <returns><c>true</c> if is creatable; otherwise, <c>false</c>.</returns>
- /// <param name="path">Path.</param>
- /// <param name="overwrite">Overwrite.</param>
- /// <param name="exception">Flag to throw an exception or return <c>false</c>.</param>
- ///
- 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;
- }
- }
- }
|