1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084 |
- #if UNITY_5_3_OR_NEWER
- #define UNITY_4_3_OR_NEWER
- #define UNITY_4_4_OR_NEWER
- #define UNITY_4_5_OR_NEWER
- #define UNITY_4_6_OR_NEWER
- #define UNITY_4_7_OR_NEWER
- #define UNITY_5_0_OR_NEWER
- #define UNITY_5_1_OR_NEWER
- #define UNITY_5_2_OR_NEWER
- #else
- #if UNITY_5
- #define UNITY_4_3_OR_NEWER
- #define UNITY_4_4_OR_NEWER
- #define UNITY_4_5_OR_NEWER
- #define UNITY_4_6_OR_NEWER
- #define UNITY_4_7_OR_NEWER
-
- #if UNITY_5_0
- #define UNITY_5_0_OR_NEWER
- #elif UNITY_5_1
- #define UNITY_5_0_OR_NEWER
- #define UNITY_5_1_OR_NEWER
- #elif UNITY_5_2
- #define UNITY_5_0_OR_NEWER
- #define UNITY_5_1_OR_NEWER
- #define UNITY_5_2_OR_NEWER
- #endif
- #else
- #if UNITY_4_3
- #define UNITY_4_3_OR_NEWER
- #elif UNITY_4_4
- #define UNITY_4_3_OR_NEWER
- #define UNITY_4_4_OR_NEWER
- #elif UNITY_4_5
- #define UNITY_4_3_OR_NEWER
- #define UNITY_4_4_OR_NEWER
- #define UNITY_4_5_OR_NEWER
- #elif UNITY_4_6
- #define UNITY_4_3_OR_NEWER
- #define UNITY_4_4_OR_NEWER
- #define UNITY_4_5_OR_NEWER
- #define UNITY_4_6_OR_NEWER
- #elif UNITY_4_7
- #define UNITY_4_3_OR_NEWER
- #define UNITY_4_4_OR_NEWER
- #define UNITY_4_5_OR_NEWER
- #define UNITY_4_6_OR_NEWER
- #define UNITY_4_7_OR_NEWER
- #endif
- #endif
- #endif
- #if UNITY_5_3_OR_NEWER
- #define UNITY_SCENE_MANAGER
- #endif
- using System;
- using System.Collections.Generic;
- using System.Reflection;
- using UnityEditor;
- #if FR2_ADDRESSABLE
- using UnityEditor.AddressableAssets;
- #endif
- using UnityEngine;
- using Object = UnityEngine.Object;
- #if UNITY_SCENE_MANAGER
- using UnityEngine.SceneManagement;
- using System.IO;
- #endif
- namespace vietlabs.fr2
- {
- public class FR2_Unity
- {
- internal static readonly AssetType[] FILTERS =
- {
- new AssetType("Scene", ".unity"),
- new AssetType("Prefab", ".prefab"),
- new AssetType("Model", ".3df", ".3dm", ".3dmf", ".3dv", ".3dx", ".c5d", ".lwo", ".lws", ".ma", ".mb",
- ".mesh", ".vrl", ".wrl", ".wrz", ".fbx", ".dae", ".3ds", ".dxf", ".obj", ".skp", ".max", ".blend"),
- new AssetType("Material", ".mat", ".cubemap", ".physicsmaterial"),
- new AssetType("Texture", ".ai", ".apng", ".png", ".bmp", ".cdr", ".dib", ".eps", ".exif", ".ico", ".icon",
- ".j", ".j2c", ".j2k", ".jas", ".jiff", ".jng", ".jp2", ".jpc", ".jpe", ".jpeg", ".jpf", ".jpg", "jpw",
- "jpx", "jtf", ".mac", ".omf", ".qif", ".qti", "qtif", ".tex", ".tfw", ".tga", ".tif", ".tiff", ".wmf",
- ".psd", ".exr", ".rendertexture"),
- new AssetType("Video", ".asf", ".asx", ".avi", ".dat", ".divx", ".dvx", ".mlv", ".m2l", ".m2t", ".m2ts",
- ".m2v", ".m4e", ".m4v", "mjp", ".mov", ".movie", ".mp21", ".mp4", ".mpe", ".mpeg", ".mpg", ".mpv2",
- ".ogm", ".qt", ".rm", ".rmvb", ".wmv", ".xvid", ".flv"),
- new AssetType("Audio", ".mp3", ".wav", ".ogg", ".aif", ".aiff", ".mod", ".it", ".s3m", ".xm"),
- new AssetType("Script", ".cs", ".js", ".boo"),
- new AssetType("Text", ".txt", ".json", ".xml", ".bytes", ".sql"),
- new AssetType("Shader", ".shader", ".cginc"),
- new AssetType("Animation", ".anim", ".controller", ".overridecontroller", ".mask"),
- new AssetType("Unity Asset", ".asset", ".guiskin", ".flare", ".fontsettings", ".prefs"),
- new AssetType("Others") //
- };
- public static HashSet<string> _Selection_AssetGUIDs;
-
- public static bool StringStartsWith(string source, params string[] prefixes)
- {
- if (string.IsNullOrEmpty(source)) return false;
- for (var i = 0; i < prefixes.Length; i++)
- {
- if (source.StartsWith(prefixes[i])) return true;
- }
- return false;
- }
-
- public static void SplitPath(string assetPath, out string assetName, out string assetExtension, out string assetFolder)
- {
- assetName = string.Empty;
- assetExtension = string.Empty;
- assetFolder = string.Empty;
- if (string.IsNullOrEmpty(assetPath)) return;
- var lastSlash = assetPath.LastIndexOf("/", StringComparison.Ordinal) + 1;
- var lastDot = assetPath.LastIndexOf(".", StringComparison.Ordinal);
-
- assetName = assetPath.Substring(lastSlash, assetPath.Length - lastSlash);
- assetExtension = (lastDot > lastSlash && lastDot > 0) ? assetPath.Substring(lastDot, assetPath.Length - lastDot).ToLower() : string.Empty;
- assetFolder = assetPath.Substring(0, lastSlash);
- }
- public static string[] Selection_AssetGUIDs
- {
- get
- {
- #if UNITY_5_0_OR_NEWER
- var objs = Selection.objects;
- _Selection_AssetGUIDs = new HashSet<string>();
- foreach(var item in objs)
- {
- #if UNITY_2018_1_OR_NEWER
- {
- var guid = "";
- long fileid =-1;
-
- try
- { // missing references will cause null exception
- if(AssetDatabase.TryGetGUIDAndLocalFileIdentifier(item, out guid, out fileid))
- {
- _Selection_AssetGUIDs.Add(guid+"/"+ fileid);
- //Debug.Log("guid: " + guid + " fileID: " + fileid);
- }
- } catch {}
-
- }
- #else
- {
- var path = AssetDatabase.GetAssetPath(item);
- if (string.IsNullOrEmpty(path)) continue;
- var guid = AssetDatabase.AssetPathToGUID(path);
- System.Reflection.PropertyInfo inspectorModeInfo =
- typeof(SerializedObject).GetProperty("inspectorMode", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
- SerializedObject serializedObject = new SerializedObject(item);
- inspectorModeInfo.SetValue(serializedObject, InspectorMode.Debug, null);
- SerializedProperty localIdProp =
- serializedObject.FindProperty("m_LocalIdentfierInFile"); //note the misspelling!
- var localId = localIdProp.longValue;
- if (localId <= 0)
- {
- localId = localIdProp.intValue;
- }
- if (localId <= 0)
- {
- continue;
- }
- if (!string.IsNullOrEmpty(guid)) _Selection_AssetGUIDs.Add(guid + "/" + localId);
- }
- #endif
- }
- return Selection.assetGUIDs;
- #else
- var mInfo =
- typeof(Selection).GetProperty("assetGUIDs", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static);
- if (mInfo != null){
- return (string[]) mInfo.GetValue(null, null);
- }
- Debug.LogWarning("Unity changed ! Selection.assetGUIDs not found !");
- return new string[0];
- #endif
- }
- }
-
-
- public static readonly Dictionary<int, string> HashClassesNormal = new Dictionary<int, string>
- {
- {1, "UnityEngine.GameObject"},
- {2, "UnityEngine.Component"},
- {4, "UnityEngine.Transform"},
- {8, "UnityEngine.Behaviour"},
- {12, "UnityEngine.ParticleAnimator"},
- {15, "UnityEngine.EllipsoidParticleEmitter"},
- {20, "UnityEngine.Camera"},
- {21, "UnityEngine.Material"},
- {23, "UnityEngine.MeshRenderer"},
- {25, "UnityEngine.Renderer"},
- {26, "UnityEngine.ParticleRenderer"},
- {27, "UnityEngine.Texture"},
- {28, "UnityEngine.Texture2D"},
- {33, "UnityEngine.MeshFilter"},
- {41, "UnityEngine.OcclusionPortal"},
- {43, "UnityEngine.Mesh"},
- {45, "UnityEngine.Skybox"},
- {47, "UnityEngine.QualitySettings"},
- {48, "UnityEngine.Shader"},
- {49, "UnityEngine.TextAsset"},
- {50, "UnityEngine.Rigidbody2D"},
- {53, "UnityEngine.Collider2D"},
- {54, "UnityEngine.Rigidbody"},
- {56, "UnityEngine.Collider"},
- {57, "UnityEngine.Joint"},
- {58, "UnityEngine.CircleCollider2D"},
- {59, "UnityEngine.HingeJoint"},
- {60, "UnityEngine.PolygonCollider2D"},
- {61, "UnityEngine.BoxCollider2D"},
- {62, "UnityEngine.PhysicsMaterial2D"},
- {64, "UnityEngine.MeshCollider"},
- {65, "UnityEngine.BoxCollider"},
- {68, "UnityEngine.EdgeCollider2D"},
- {72, "UnityEngine.ComputeShader"},
- {74, "UnityEngine.AnimationClip"},
- {75, "UnityEngine.ConstantForce"},
- {81, "UnityEngine.AudioListener"},
- {82, "UnityEngine.AudioSource"},
- {83, "UnityEngine.AudioClip"},
- {84, "UnityEngine.RenderTexture"},
- {87, "UnityEngine.MeshParticleEmitter"},
- {88, "UnityEngine.ParticleEmitter"},
- {89, "UnityEngine.Cubemap"},
- {90, "Avatar"},
- {92, "UnityEngine.GUILayer"},
- {93, "UnityEngine.RuntimeAnimatorController"},
- {95, "UnityEngine.Animator"},
- {96, "UnityEngine.TrailRenderer"},
- {102, "UnityEngine.TextMesh"},
- {104, "UnityEngine.RenderSettings"},
- {108, "UnityEngine.Light"},
- {111, "UnityEngine.Animation"},
- {114, "UnityEngine.MonoBehaviour"},
- {115, "UnityEditor.MonoScript"},
- {117, "UnityEngine.Texture3D"},
- {119, "UnityEngine.Projector"},
- {120, "UnityEngine.LineRenderer"},
- {121, "UnityEngine.Flare"},
- {123, "UnityEngine.LensFlare"},
- {124, "UnityEngine.FlareLayer"},
- {128, "UnityEngine.Font"},
- {129, "UnityEditor.PlayerSettings"},
- {131, "UnityEngine.GUITexture"},
- {132, "UnityEngine.GUIText"},
- {133, "UnityEngine.GUIElement"},
- {134, "UnityEngine.PhysicMaterial"},
- {135, "UnityEngine.SphereCollider"},
- {136, "UnityEngine.CapsuleCollider"},
- {137, "UnityEngine.SkinnedMeshRenderer"},
- {138, "UnityEngine.FixedJoint"},
- {142, "UnityEngine.AssetBundle"},
- {143, "UnityEngine.CharacterController"},
- {144, "UnityEngine.CharacterJoint"},
- {145, "UnityEngine.SpringJoint"},
- {146, "UnityEngine.WheelCollider"},
- {152, "UnityEngine.MovieTexture"},
- {153, "UnityEngine.ConfigurableJoint"},
- {154, "UnityEngine.TerrainCollider"},
- {156, "UnityEngine.TerrainData"},
- {157, "UnityEngine.LightmapSettings"},
- {158, "UnityEngine.WebCamTexture"},
- {159, "UnityEditor.EditorSettings"},
- {162, "UnityEditor.EditorUserSettings"},
- {164, "UnityEngine.AudioReverbFilter"},
- {165, "UnityEngine.AudioHighPassFilter"},
- {166, "UnityEngine.AudioChorusFilter"},
- {167, "UnityEngine.AudioReverbZone"},
- {168, "UnityEngine.AudioEchoFilter"},
- {169, "UnityEngine.AudioLowPassFilter"},
- {170, "UnityEngine.AudioDistortionFilter"},
- {171, "UnityEngine.SparseTexture"},
- {180, "UnityEngine.AudioBehaviour"},
- {182, "UnityEngine.WindZone"},
- {183, "UnityEngine.Cloth"},
- {192, "UnityEngine.OcclusionArea"},
- {193, "UnityEngine.Tree"},
- {198, "UnityEngine.ParticleSystem"},
- {199, "UnityEngine.ParticleSystemRenderer"},
- {200, "UnityEngine.ShaderVariantCollection"},
- {205, "UnityEngine.LODGroup"},
- {207, "UnityEngine.Motion"},
- {212, "UnityEngine.SpriteRenderer"},
- {213, "UnityEngine.Sprite"},
- {215, "UnityEngine.ReflectionProbe"},
- {218, "UnityEngine.Terrain"},
- {220, "UnityEngine.LightProbeGroup"},
- {221, "UnityEngine.AnimatorOverrideController"},
- {222, "UnityEngine.CanvasRenderer"},
- {223, "UnityEngine.Canvas"},
- {224, "UnityEngine.RectTransform"},
- {225, "UnityEngine.CanvasGroup"},
- {226, "UnityEngine.BillboardAsset"},
- {227, "UnityEngine.BillboardRenderer"},
- {229, "UnityEngine.AnchoredJoint2D"},
- {230, "UnityEngine.Joint2D"},
- {231, "UnityEngine.SpringJoint2D"},
- {232, "UnityEngine.DistanceJoint2D"},
- {233, "UnityEngine.HingeJoint2D"},
- {234, "UnityEngine.SliderJoint2D"},
- {235, "UnityEngine.WheelJoint2D"},
- {246, "UnityEngine.PhysicsUpdateBehaviour2D"},
- {247, "UnityEngine.ConstantForce2D"},
- {248, "UnityEngine.Effector2D"},
- {249, "UnityEngine.AreaEffector2D"},
- {250, "UnityEngine.PointEffector2D"},
- {251, "UnityEngine.PlatformEffector2D"},
- {252, "UnityEngine.SurfaceEffector2D"},
- {258, "UnityEngine.LightProbes"},
- {290, "UnityEngine.AssetBundleManifest"},
- {1003, "UnityEditor.AssetImporter"},
- {1004, "UnityEditor.AssetDatabase"},
- {1006, "UnityEditor.TextureImporter"},
- {1007, "UnityEditor.ShaderImporter"},
- {1011, "UnityEngine.AvatarMask"},
- {1020, "UnityEditor.AudioImporter"},
- {1029, "UnityEditor.DefaultAsset"},
- {1032, "UnityEditor.SceneAsset"},
- {1035, "UnityEditor.MonoImporter"},
- {1040, "UnityEditor.ModelImporter"},
- {1042, "UnityEditor.TrueTypeFontImporter"},
- {1044, "UnityEditor.MovieImporter"},
- {1045, "UnityEditor.EditorBuildSettings"},
- {1050, "UnityEditor.PluginImporter"},
- {1051, "UnityEditor.EditorUserBuildSettings"},
- {1105, "UnityEditor.HumanTemplate"},
- {1110, "UnityEditor.SpeedTreeImporter"},
- {1113, "UnityEditor.LightmapParameters"}
- };
- //private static Texture2D _whiteTexture;
- //public static Texture2D whiteTexture {
- // get {
- // return EditorGUIUtility.whiteTexture;
- // #if UNITY_5_0_OR_NEWER
- // return EditorGUIUtility.whiteTexture;
- // #else
- // if (_whiteTexture != null) return _whiteTexture;
- // _whiteTexture = new Texture2D(1,1, TextureFormat.RGBA32, false);
- // _whiteTexture.SetPixel(0, 0, Color.white);
- // _whiteTexture.hideFlags = HideFlags.DontSave;
- // return _whiteTexture;
- // #endif
- // }
- //}
- public static T LoadAssetAtPath<T>(string path) where T : Object
- {
- #if UNITY_5_1_OR_NEWER
- return AssetDatabase.LoadAssetAtPath<T>(path);
- #else
- return (T)AssetDatabase.LoadAssetAtPath(path, typeof(T));
- #endif
- }
- public static void SetWindowTitle(EditorWindow window, string title)
- {
- #if UNITY_5_1_OR_NEWER
- window.titleContent = new GUIContent(title);
- #else
- window.title = title;
- #endif
- }
- public static void GetCompilingPhase(string path, out bool isPlugin, out bool isEditor)
- {
- #if (UNITY_5_2_0 || UNITY_5_2_1) && !UNITY_5_2_OR_NEWER
- bool oldSystem = true;
- #else
- var oldSystem = false;
- #endif
- // ---- Old system: Editor for the plugin should be Plugins/Editor
- if (oldSystem)
- {
- bool isPluginEditor = path.StartsWith("Assets/Plugins/Editor/", StringComparison.Ordinal)
- || path.StartsWith("Assets/Standard Assets/Editor/", StringComparison.Ordinal)
- || path.StartsWith("Assets/Pro Standard Assets/Editor/",
- StringComparison.Ordinal);
- if (isPluginEditor)
- {
- isPlugin = true;
- isEditor = true;
- return;
- }
- }
- isPlugin = path.StartsWith("Assets/Plugins/", StringComparison.Ordinal)
- || path.StartsWith("Assets/Standard Assets/", StringComparison.Ordinal)
- || path.StartsWith("Assets/Pro Standard Assets/", StringComparison.Ordinal);
- isEditor = oldSystem && isPlugin ? false : path.Contains("/Editor/");
- }
- public static T LoadAssetWithGUID<T>(string guid) where T : Object
- {
- if (string.IsNullOrEmpty(guid))
- {
- return null;
- }
- string path = AssetDatabase.GUIDToAssetPath(guid);
- if (string.IsNullOrEmpty(path))
- {
- return null;
- }
- #if UNITY_5_1_OR_NEWER
- return AssetDatabase.LoadAssetAtPath<T>(path);
- #else
- return (T)AssetDatabase.LoadAssetAtPath(path, typeof(T));
- #endif
- }
- public static void UnloadUnusedAssets()
- {
- #if UNITY_5_0_OR_NEWER
- EditorUtility.UnloadUnusedAssetsImmediate();
- #else
- EditorUtility.UnloadUnusedAssets();
- #endif
- Resources.UnloadUnusedAssets();
- }
- internal static int Epoch(DateTime time)
- {
- return (int) (time.ToUniversalTime() - new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc)).TotalSeconds;
- }
- internal static bool DrawToggle(ref bool v, string label)
- {
- bool v1 = GUILayout.Toggle(v, label);
- if (v1 != v)
- {
- v = v1;
- return true;
- }
- return false;
- }
- internal static bool DrawToggleToolbar(ref bool v, string label, float width)
- {
- bool v1 = GUILayout.Toggle(v, label, EditorStyles.toolbarButton, GUILayout.Width(width));
- if (v1 != v)
- {
- v = v1;
- return true;
- }
- return false;
- }
- internal static bool DrawToggleToolbar(ref bool v, GUIContent icon, float width)
- {
- bool v1 = GUILayout.Toggle(v, icon, EditorStyles.toolbarButton, GUILayout.Width(width));
- if (v1 != v)
- {
- v = v1;
- return true;
- }
- return false;
- }
- public static string GetAddressable(string guid)
- {
- #if FR2_ADDRESSABLE
- var aaSettings = AddressableAssetSettingsDefaultObject.GetSettings(true);
- var entry = aaSettings.FindAssetEntry(guid);
- return entry != null ? entry.address : string.Empty;
- #endif
-
- return null;
- }
-
- internal static EditorWindow FindEditor(string className)
- {
- EditorWindow[] list = Resources.FindObjectsOfTypeAll<EditorWindow>();
- foreach (EditorWindow item in list)
- {
- if (item.GetType().FullName == className)
- {
- return item;
- }
- }
- return null;
- }
- internal static void RepaintAllEditor(string className)
- {
- EditorWindow[] list = Resources.FindObjectsOfTypeAll<EditorWindow>();
- foreach (EditorWindow item in list)
- {
- #if FR2_DEV
- Debug.Log(item.GetType().FullName);
- #endif
- if (item.GetType().FullName != className)
- {
- continue;
- }
- item.Repaint();
- }
- }
- internal static void RepaintProjectWindows()
- {
- RepaintAllEditor("UnityEditor.ProjectBrowser");
- }
- internal static void RepaintFR2Windows()
- {
- RepaintAllEditor("vietlabs.fr2.FR2_Window");
- }
- internal static void ExportSelection()
- {
- Type packageExportT = null;
- foreach (Assembly assembly in AppDomain.CurrentDomain.GetAssemblies())
- {
- packageExportT = assembly.GetType("UnityEditor.PackageExport");
- if (packageExportT != null)
- {
- break;
- }
- }
- if (packageExportT == null)
- {
- Debug.LogWarning("Export Package Error : UnityEditor.PackageExport not found !");
- return;
- }
- EditorWindow panel = EditorWindow.GetWindow(packageExportT, true, "Exporting package");
- #if UNITY_5_2_OR_NEWER
- var prop = "m_IncludeDependencies";
- #else
- var prop = "m_bIncludeDependencies";
- #endif
- FieldInfo fieldInfo = packageExportT.GetField(prop, BindingFlags.NonPublic | BindingFlags.Instance);
- if (fieldInfo == null)
- {
- Debug.LogWarning("Export Package error : " + prop + " not found !");
- return;
- }
- MethodInfo methodInfo =
- packageExportT.GetMethod("BuildAssetList", BindingFlags.NonPublic | BindingFlags.Instance);
- if (methodInfo == null)
- {
- Debug.LogWarning("Export Package error : BuildAssetList method not found !");
- return;
- }
- fieldInfo.SetValue(panel, false);
- methodInfo.Invoke(panel, null);
- panel.Repaint();
- }
- public static Type GetType(string typeName)
- {
- Type type = Type.GetType(typeName);
- if (type != null)
- {
- return type;
- }
- foreach (Assembly a in AppDomain.CurrentDomain.GetAssemblies())
- {
- type = a.GetType(typeName);
- if (type != null)
- {
- return type;
- }
- }
- return null;
- }
- public static IEnumerable<Transform> GetAllChild(Transform root)
- {
- yield return root;
- if (root.childCount <= 0)
- {
- yield break;
- }
- for (var i = 0; i < root.childCount; i++)
- {
- foreach (Transform item in GetAllChild(root.GetChild(i)))
- {
- yield return item;
- }
- }
- }
- public static IEnumerable<GameObject> getAllObjsInCurScene()
- {
- #if UNITY_SCENE_MANAGER
- for (var j = 0; j < SceneManager.sceneCount; j++)
- {
- Scene scene = SceneManager.GetSceneAt(j);
- foreach (GameObject item in GetGameObjectsInScene(scene))
- {
- yield return item;
- }
- }
- if (EditorApplication.isPlaying)
- {
- //dont destroy scene
- GameObject temp = null;
- try
- {
- temp = new GameObject();
- Object.DontDestroyOnLoad(temp);
- Scene dontDestroyOnLoad = temp.scene;
- Object.DestroyImmediate(temp);
- temp = null;
- foreach (GameObject item in GetGameObjectsInScene(dontDestroyOnLoad))
- {
- yield return item;
- }
- }
- finally
- {
- if (temp != null)
- {
- Object.DestroyImmediate(temp);
- }
- }
- }
- #else
- foreach (Transform obj in Resources.FindObjectsOfTypeAll(typeof(Transform)))
- {
- GameObject o = obj.gameObject;
- yield return o;
- }
- #endif
- }
- #if UNITY_SCENE_MANAGER
- private static IEnumerable<GameObject> GetGameObjectsInScene(Scene scene)
- {
- var rootObjects = new List<GameObject>();
- if (!scene.isLoaded)
- {
- yield break;
- }
- scene.GetRootGameObjects(rootObjects);
- // iterate root objects and do something
- for (var i = 0; i < rootObjects.Count; ++i)
- {
- GameObject gameObject = rootObjects[i];
- foreach (GameObject item in getAllChild(gameObject))
- {
- yield return item;
- }
- yield return gameObject;
- }
- }
- #endif
- public static IEnumerable<GameObject> getAllChild(GameObject target, bool returnMe = false)
- {
- if (returnMe)
- {
- yield return target;
- }
- if (target.transform.childCount > 0)
- {
- for (var i = 0; i < target.transform.childCount; i++)
- {
- yield return target.transform.GetChild(i).gameObject;
- foreach (GameObject item in getAllChild(target.transform.GetChild(i).gameObject, false))
- {
- yield return item;
- }
- }
- }
- }
- public static IEnumerable<Object> GetAllRefObjects(GameObject obj)
- {
- Component[] components = obj.GetComponents<Component>();
- foreach (Component com in components)
- {
- if (com == null)
- {
- continue;
- }
- var serialized = new SerializedObject(com);
- SerializedProperty it = serialized.GetIterator().Copy();
- while (it.NextVisible(true))
- {
- if (it.propertyType != SerializedPropertyType.ObjectReference)
- {
- continue;
- }
- if (it.objectReferenceValue == null)
- {
- continue;
- }
- yield return it.objectReferenceValue;
- }
- }
- }
- public static int StringMatch(string pattern, string input)
- {
- if (input == pattern)
- {
- return int.MaxValue;
- }
- if (input.Contains(pattern))
- {
- return int.MaxValue - 1;
- }
- var pidx = 0;
- var score = 0;
- var tokenScore = 0;
- for (var i = 0; i < input.Length; i++)
- {
- char ch = input[i];
- if (ch == pattern[pidx])
- {
- tokenScore += tokenScore + 1; //increasing score for continuos token
- pidx++;
- if (pidx >= pattern.Length)
- {
- break;
- }
- }
- else
- {
- tokenScore = 0;
- }
- score += tokenScore;
- }
- return score;
- }
- public static int GetIndex(string ext)
- {
- for (var i = 0; i < FILTERS.Length - 1; i++)
- {
- if (FILTERS[i].extension.Contains(ext))
- {
- return i;
- }
- }
- return FILTERS.Length - 1; //Others
- }
- public static void GuiLine(int i_height = 1)
- {
- Rect rect = EditorGUILayout.GetControlRect(false, i_height);
- rect.height = i_height;
- EditorGUI.DrawRect(rect, new Color(0.5f, 0.5f, 0.5f, 1));
- }
- public static bool IsInAsset(GameObject obj)
- {
- //#if UNITY_5_3_OR_NEWER
- // this not working in new empty created scene
- //return string.IsNullOrEmpty(obj.scene.name);
- //#else
- return !string.IsNullOrEmpty(AssetDatabase.GetAssetPath(obj));
- //#endif
- }
- #if FR2_DEBUG
- [MenuItem("Tools/Test Prefab")]
- static void TestPrefab()
- {
- GetPrefabParent(Selection.activeGameObject);
- }
- #endif
-
- public static string GetPrefabParent(Object obj)
- {
- #if UNITY_2018_3_OR_NEWER
- string prefabPath = PrefabUtility.GetPrefabAssetPathOfNearestInstanceRoot(obj);
- #elif UNITY_2018_2_OR_NEWER
- var prefab = PrefabUtility.GetCorrespondingObjectFromSource(obj);
- string prefabPath = AssetDatabase.GetAssetPath(prefab);
- #else
- var hierarchy_root = PrefabUtility.FindPrefabRoot((GameObject)obj);
- var prefab = PrefabUtility.GetPrefabParent(hierarchy_root);
- var prefabPath = AssetDatabase.GetAssetPath(prefab);
- #endif
- return AssetDatabase.AssetPathToGUID(prefabPath);
- }
- public static string GetGameObjectPath(GameObject obj, bool includeMe = true)
- {
- if (obj == null)
- {
- return string.Empty;
- }
- string path = includeMe ? "/" + obj.name : "/";
- while (obj.transform.parent != null)
- {
- obj = obj.transform.parent.gameObject;
- path = "/" + obj.name + path;
- }
- path = path.TrimStart('/');
- return path;
- }
- public static bool CheckIsPrefab(GameObject obj)
- {
- #if UNITY_2018_3_OR_NEWER
- // var t = PrefabUtility.GetPrefabAssetType(obj);
- // var isPrefab = (t != PrefabAssetType.NotAPrefab) && (t != PrefabAssetType.MissingAsset);
- return PrefabUtility.IsAnyPrefabInstanceRoot(obj);
- #else
- return PrefabUtility.GetPrefabType(obj) != PrefabType.None;
- #endif
- }
- public static TerrainTextureData[] GetTerrainTextureDatas(TerrainData data)
- {
- #if UNITY_2018_3_OR_NEWER
- var arr = new TerrainTextureData[data.terrainLayers.Length];
- for (var i = 0; i < data.terrainLayers.Length; i++)
- {
- TerrainLayer layer = data.terrainLayers[i];
- arr[i] = new TerrainTextureData
- (
- layer.normalMapTexture,
- layer.maskMapTexture,
- layer.diffuseTexture
- );
- }
- return arr;
- #else
- var arr = new TerrainTextureData[data.splatPrototypes.Length];
- for(int i = 0; i < data.splatPrototypes.Length; i++)
- {
- var layer = data.splatPrototypes[i];
- arr[i] = new TerrainTextureData
- (
- layer.normalMap,
- layer.texture
- );
- }
- return arr;
- #endif
- }
- public static int ReplaceTerrainTextureDatas(TerrainData terrain, Texture2D fromObj, Texture2D toObj)
- {
- var found = 0;
- #if UNITY_2018_3_OR_NEWER
- TerrainLayer[] arr3 = terrain.terrainLayers;
- for (var i = 0; i < arr3.Length; i++)
- {
- if (arr3[i].normalMapTexture == fromObj)
- {
- found++;
- arr3[i].normalMapTexture = toObj;
- }
- if (arr3[i].maskMapTexture == fromObj)
- {
- found++;
- arr3[i].maskMapTexture = toObj;
- }
- if (arr3[i].diffuseTexture == fromObj)
- {
- found++;
- arr3[i].diffuseTexture = toObj;
- }
- }
- terrain.terrainLayers = arr3;
- #else
- var arr3 = terrain.splatPrototypes;
- for (var i = 0; i < arr3.Length; i++)
- {
- if (arr3[i].texture == fromObj)
- {
- found++;
- arr3[i].texture = toObj;
- }
- if (arr3[i].normalMap == fromObj)
- {
- found++;
- arr3[i].normalMap = toObj;
- }
- }
- terrain.splatPrototypes = arr3;
- #endif
- return found;
- }
- public static void Clear<T1, T2>(ref Dictionary<T1, T2> dict)
- {
- if (dict == null)
- {
- dict = new Dictionary<T1, T2>();
- }
- else
- {
- dict.Clear();
- }
- }
-
- public static void Clear<T>(ref List<T> list)
- {
- if (list == null)
- {
- list = new List<T>();
- }
- else
- {
- list.Clear();
- }
- }
-
- public static SerializedProperty[] xGetSerializedProperties(Object go, bool processArray)
- {
- var so = new SerializedObject(go);
- so.Update();
- var result = new List<SerializedProperty>();
- SerializedProperty iterator = so.GetIterator();
- while (iterator.NextVisible(true))
- {
- SerializedProperty copy = iterator.Copy();
- if (processArray && iterator.isArray)
- {
- result.AddRange(xGetSOArray(copy));
- }
- else
- {
- result.Add(copy);
- }
- }
- return result.ToArray();
- }
- public static List<SerializedProperty> xGetSOArray(SerializedProperty prop)
- {
- int size = prop.arraySize;
- var result = new List<SerializedProperty>();
- for (var i = 0; i < size; i++)
- {
- SerializedProperty p = prop.GetArrayElementAtIndex(i);
- if (p.isArray)
- {
- result.AddRange(xGetSOArray(p.Copy()));
- }
- else
- {
- result.Add(p.Copy());
- }
- }
- return result;
- }
- public class TerrainTextureData
- {
- public Texture2D[] textures;
- public TerrainTextureData(params Texture2D[] param)
- {
- var count = 0;
- if (param != null)
- {
- count = param.Length;
- }
- textures = new Texture2D[count];
- for (var i = 0; i < count; i++)
- {
- textures[i] = param[i];
- }
- }
- }
- public static void BackupAndDeleteAssets(FR2_Ref[] assets)
- {
- var fileName = DateTime.Now.ToString("yyMMdd_hhmmss");
- var list = assets;
- var result = new List<string>();
- var selectedList = new List<string>();
- foreach (var item in list)
- {
- if (item.asset == null) continue;
- var oPath = item.asset.assetPath.Replace("\\", "/");
- if (!oPath.StartsWith("Assets/")) continue;
- result.Add(item.asset.assetPath);
- if (item.isSelected())
- {
- selectedList.Add(item.asset.assetPath);
- }
- }
- if (selectedList.Count != 0) result = selectedList;
- Directory.CreateDirectory("Library/FR2/");
- AssetDatabase.ExportPackage(result.ToArray(), "Library/FR2/bk_" + fileName + ".unitypackage");
- AssetDatabase.StartAssetEditing();
- try
- {
- for (var i = 0; i < result.Count; i++)
- {
- AssetDatabase.DeleteAsset(result[i]);
- }
- }
- finally
- {
- AssetDatabase.StopAssetEditing();
- AssetDatabase.Refresh();
- }
- FR2_Cache.DelayCheck4Changes();
- }
- }
- }
|