public static ActiveEditorTracker GetActiveEditorTrackerForSelectedObject() { var inspectorWindow = GetInspectorWindow(); if (inspectorWindow == null) { return(null); } if (CSReflectionTools.inspectorWindowType == null) { return(null); } inspectorWindow.Repaint(); ActiveEditorTracker result = null; var trackerProperty = CSReflectionTools.GetPropertyInfo(CSReflectionTools.inspectorWindowType, "tracker"); if (trackerProperty != null) { result = (ActiveEditorTracker)trackerProperty.GetValue(inspectorWindow, null); } else { Debug.LogError(Maintainer.ConstructError("Can't get ActiveEditorTracker from the InspectorWindow!")); } return(result); }
public static void TreeToList <T>(T root, IList <T> result) where T : TreeElement { if (result == null) { throw new NullReferenceException(Maintainer.ConstructError("The input 'IList<T> result' list is null!")); } result.Clear(); var stack = new Stack <T>(); stack.Push(root); while (stack.Count > 0) { var current = stack.Pop(); result.Add(current); if (current.children == null || current.children.Count <= 0) { continue; } for (var i = current.children.Count - 1; i >= 0; i--) { stack.Push((T)current.children[i]); } } }
private static void CheckLightMapSettings(TreeConjunction conjunction, int candidateInstanceId) { lightmapSettings = lightmapSettings ? lightmapSettings : CSSettingsTools.GetInSceneLightmapSettings(); if (lightmapSettings == null) { return; } lightmapSettingsSo = lightmapSettingsSo ?? new SerializedObject(lightmapSettings); lightmapParametersField = lightmapParametersField ?? lightmapSettingsSo.FindProperty( "m_LightmapEditorSettings.m_LightmapParameters"); if (lightmapParametersField != null && lightmapParametersField.propertyType == SerializedPropertyType.ObjectReference) { if (lightmapParametersField.objectReferenceInstanceIDValue == candidateInstanceId) { var entry = new ReferencingEntryData { location = Location.SceneLightingSettings, prefixLabel = "Lighting settings (Scene tab > Lightmapping Settings > Lightmap Parameters)" }; conjunction.referencedAtInfo.AddNewEntry(entry); } } else { Debug.LogError( Maintainer.ConstructError( "Can't find m_LightmapParameters at the LightmapSettings!")); } }
public void MoveElements(TreeItem parentItem, int insertionIndex, List <TreeItem> elements) { if (insertionIndex < 0) { throw new ArgumentException(Maintainer.ConstructError("Invalid input: insertionIndex is -1, client needs to decide what index elements should be reparented at!")); } if (parentItem == null) { return; } if (insertionIndex > 0) { insertionIndex -= parentItem.Children.GetRange(0, insertionIndex).Count(elements.Contains); } foreach (var draggedItem in elements) { draggedItem.Parent.Children.Remove(draggedItem); draggedItem.Parent = parentItem; } if (parentItem.Children == null) { parentItem.Children = new List <TreeItem>(); } parentItem.Children.InsertRange(insertionIndex, elements); TreeItemUtility.UpdateDepthValues(root); TreeItemUtility.TreeToList(root, data); Changed(); }
public static void ValidateDepthValues <T>(T[] array) where T : TreeElement { if (array.Length == 0) { throw new ArgumentException(Maintainer.ConstructError("list should have items, count is 0, check before calling ValidateDepthValues!"), "array"); } if (array[0].depth != -1) { throw new ArgumentException(Maintainer.ConstructError("list item at index 0 should have a depth of -1 (since this should be the hidden root of the tree). Depth is: " + array[0].depth + "!"), "array"); } for (var i = 0; i < array.Length - 1; i++) { var depth = array[i].depth; var nextDepth = array[i + 1].depth; if (nextDepth > depth && nextDepth - depth > 1) { throw new ArgumentException(Maintainer.ConstructError(string.Format("Invalid depth info in input list. Depth cannot increase more than 1 per row. Index {0} has depth {1} while index {2} has depth {3}!", i, depth, i + 1, nextDepth))); } } for (var i = 1; i < array.Length; ++i) { if (array[i].depth < 0) { throw new ArgumentException(Maintainer.ConstructError("Invalid depth value for item at index " + i + ". Only the first item (the root) should have depth below 0!")); } } if (array.Length > 1 && array[1].depth != 0) { throw new ArgumentException(Maintainer.ConstructError("Input list item at index 1 is assumed to have a depth of 0!"), "array"); } }
public void AddElements(IList <T> elements, TreeItem parent, int insertPosition) { if (elements == null) { throw new ArgumentNullException("elements", Maintainer.ConstructError("elements is null!")); } if (elements.Count == 0) { throw new ArgumentNullException("elements", Maintainer.ConstructError("elements Count is 0: nothing to add!")); } if (parent == null) { throw new ArgumentNullException("parent", Maintainer.ConstructError("parent is null!")); } if (parent.Children == null) { parent.Children = new List <TreeItem>(); } parent.Children.InsertRange(insertPosition, elements.Cast <TreeItem> ()); foreach (var element in elements) { element.Parent = parent; element.depth = parent.depth + 1; TreeItemUtility.UpdateDepthValues(element); } TreeItemUtility.TreeToList(root, data); Changed(); }
private void GetSplitterState() { if (splitterState != null) { return; } var savedState = MaintainerPersonalSettings.References.splitterState; object result; try { if (!string.IsNullOrEmpty(savedState)) { result = JsonUtility.FromJson(savedState, CSReflectionTools.splitterStateType); } else { result = Activator.CreateInstance(CSReflectionTools.splitterStateType, new [] { 100f, 50f }, new [] { 90, 47 }, null); } } catch (Exception e) { Debug.LogError(Maintainer.ConstructError("Couldn't create instance of the SplitterState class!\n" + e, ReferencesFinder.ModuleName)); throw e; } splitterState = result; }
public static void SetInspectorToDebug(SerializedObject serializedObject) { if (inspectorModeCachedSetter == null) { var pi = typeof(SerializedObject).GetProperty("inspectorMode", BindingFlags.NonPublic | BindingFlags.Instance); if (pi != null) { var mi = pi.GetSetMethod(true); if (mi != null) { inspectorModeCachedSetter = (Action <SerializedObject, InspectorMode>)Delegate.CreateDelegate(typeof(Action <SerializedObject, InspectorMode>), mi); } else { Debug.LogError(Maintainer.ConstructError("Can't get the setter for the SerializedObject.inspectorMode property!")); return; } } else { Debug.LogError(Maintainer.ConstructError("Can't get the SerializedObject.inspectorMode property!")); return; } } inspectorModeCachedSetter.Invoke(serializedObject, InspectorMode.Debug); }
private static long GetUniqueObjectIdFromAssetObject(Object unityObject) { /*var go = unityObject as GameObject; * if (go != null) * { * unityObject = go.transform; * }*/ long result = -1; var path = AssetDatabase.GetAssetPath(unityObject); if (!string.IsNullOrEmpty(path)) { #if UNITY_2018_2_OR_NEWER result = GetAssetLocalIdentifierInFile(unityObject); #else if (AssetDatabase.IsMainAsset(unityObject)) { result = path.GetHashCode(); } else { result = GetLocalIdentifierInFile(unityObject); } #endif } else { Debug.LogError(Maintainer.ConstructError("Can't get path to the asset " + unityObject.name)); } return(result); }
public static void UpdateDepthValues <T>(T root) where T : TreeElement { if (root == null) { throw new ArgumentNullException("root", Maintainer.ConstructError("The root is null!")); } if (!root.HasChildren) { return; } var stack = new Stack <TreeElement>(); stack.Push(root); while (stack.Count > 0) { var current = stack.Pop(); if (current.children != null) { foreach (var child in current.children) { child.depth = current.depth + 1; stack.Push(child); } } } }
private static bool RevealAndSelectGameObjectInPrefab(string enclosingAssetPath, string transformPath, long objectId, long componentId) { var targetAsset = AssetDatabase.LoadMainAssetAtPath(enclosingAssetPath) as GameObject; if (targetAsset == null) { return(false); } Object target = CSObjectTools.FindChildGameObjectRecursive(targetAsset.transform, objectId, targetAsset.transform.name, transformPath); // in some cases, prefabs can have nested non-GameObject items if (target == null) { var allObjectsInPrefab = AssetDatabase.LoadAllAssetsAtPath(enclosingAssetPath); foreach (var objectOnPrefab in allObjectsInPrefab) { if (objectOnPrefab is BillboardAsset || objectOnPrefab is TreeData) { var objectOnPrefabId = CSObjectTools.GetUniqueObjectId(objectOnPrefab); if (objectOnPrefabId == objectId) { target = objectOnPrefab; } } } } if (target == null) { Debug.LogError(Maintainer.ConstructError("Couldn't find target Game Object " + transformPath + " at " + enclosingAssetPath + " with ObjectID " + objectId + "!")); return(false); } if (target is GameObject) { CSObjectTools.SelectGameObject((GameObject)target, false); } else { Selection.activeObject = target; } if (transformPath.Split('/').Length > 2) { EditorApplication.delayCall += () => { EditorGUIUtility.PingObject(targetAsset); }; } if (componentId != -1) { return(TryFoldAllComponentsExceptId(componentId)); } return(true); }
private static List <string> ExtractReferencedAssets(Object assetGroup) { var so = new SerializedObject(assetGroup); var serializedEntries = so.FindProperty("m_SerializeEntries"); if (serializedEntries == null) { // legacy package version used this name serializedEntries = so.FindProperty("m_serializeEntries"); if (serializedEntries == null) { Debug.LogError(Maintainer.ConstructError("Can't reach serialize entries in AddressableAssetGroup!")); return(null); } } if (!serializedEntries.isArray) { Debug.LogError(Maintainer.ConstructError("Can't find serialize entries array in AddressableAssetGroup!")); return(null); } var result = new List <string>(); var count = serializedEntries.arraySize; for (var i = 0; i < count; i++) { var item = serializedEntries.GetArrayElementAtIndex(i); if (item == null) { Debug.LogWarning(Maintainer.ConstructWarning("Serialize entry from AddressableAssetGroup is null!")); continue; } var referencedGUID = item.FindPropertyRelative("m_GUID"); if (referencedGUID == null || referencedGUID.propertyType != SerializedPropertyType.String) { Debug.LogError(Maintainer.ConstructError("Can't reach Serialize entry GUID of AddressableAssetGroup!")); return(null); } var path = AssetDatabase.GUIDToAssetPath(referencedGUID.stringValue); if (!path.StartsWith("Assets")) { continue; } var guid = AssetDatabase.AssetPathToGUID(path); if (!string.IsNullOrEmpty(guid)) { result.Add(guid); } } return(result); }
public static long GetUniqueObjectId(Object unityObject, bool recursiveCall = false) { var id = -1L; var siblingId = 0; if (unityObject == null) { return(id); } var go = unityObject as GameObject; if (go != null) { siblingId = go.transform.GetSiblingIndex(); //unityObject = go.transform; } if (CSPrefabTools.IsInstance(unityObject)) { var prefabAssetSource = CSPrefabTools.GetAssetSource(unityObject); if (prefabAssetSource != null) { if (!recursiveCall) { id = GetUniqueObjectId(prefabAssetSource, true); return(id + siblingId); } Debug.LogError(Maintainer.ConstructError("Couldn't reach asset source: " + unityObject.name), unityObject); } } if (AssetDatabase.Contains(unityObject)) { id = GetUniqueObjectIdFromAssetObject(unityObject); } else { id = GetLocalIdentifierInFile(unityObject); if (id <= 0) { id = unityObject.GetInstanceID(); } } if (id <= 0) { id = siblingId; } if (id <= 0) { id = unityObject.name.GetHashCode(); } return(id); }
private static void CheckRenderSettingsTexture(TreeConjunction conjunction, int candidateInstanceId) { renderSettings = renderSettings ? renderSettings : CSSettingsTools.GetInSceneRenderSettings(); if (renderSettings == null) { return; } renderSettingsSo = renderSettingsSo ?? new SerializedObject(renderSettings); renderHaloField = renderHaloField ?? renderSettingsSo.FindProperty("m_HaloTexture"); if (renderHaloField != null && renderHaloField.propertyType == SerializedPropertyType.ObjectReference) { if (renderHaloField.objectReferenceInstanceIDValue == candidateInstanceId) { var entry = new ReferencingEntryData { location = Location.SceneLightingSettings, prefixLabel = "Lighting settings (Scene tab > Other Settings > Halo Texture)" }; conjunction.referencedAtInfo.AddNewEntry(entry); } } else { Debug.LogError(Maintainer.ConstructError("Can't find m_HaloTexture at the RenderSettings!")); } renderSpotField = renderSpotField ?? renderSettingsSo.FindProperty("m_SpotCookie"); if (renderSpotField != null && renderSpotField.propertyType == SerializedPropertyType.ObjectReference) { if (renderSpotField.objectReferenceInstanceIDValue == candidateInstanceId) { var entry = new ReferencingEntryData { location = Location.SceneLightingSettings, prefixLabel = "Lighting settings (Scene tab > Other Settings > Spot Cookie)" }; conjunction.referencedAtInfo.AddNewEntry(entry); } } else { Debug.LogError(Maintainer.ConstructError("Can't find m_SpotCookie at the RenderSettings!")); } /*var iterator = renderSettingsSo.GetIterator(); * while (iterator.Next(true)) * { * if (iterator.propertyType == SerializedPropertyType.ObjectReference) * { * Debug.Log(iterator.propertyPath + " [" + iterator.objectReferenceValue + "]"); * } * }*/ }
public static OpenSceneResult OpenScene(string path, bool activate = true) { #if UNITY_2018_3_OR_NEWER StageUtility.GoToMainStage(); #endif var result = new OpenSceneResult(); if (string.IsNullOrEmpty(path)) { Debug.LogError(Maintainer.ConstructError("Can't open scene since path is absent!")); return(result); } var targetScene = SceneManager.GetSceneByPath(path); result.scene = targetScene; result.scenePath = path; if (targetScene == SceneManager.GetActiveScene()) { result.success = true; return(result); } if (!targetScene.isLoaded) { result.sceneWasAdded = EditorSceneManager.GetSceneManagerSetup().All(s => s.path != targetScene.path); try { targetScene = EditorSceneManager.OpenScene(path, OpenSceneMode.Additive); } catch (Exception e) { Debug.LogError(Maintainer.ConstructError("Error while opening scene: " + path + "\n" + e)); return(result); } result.scene = targetScene; if (!targetScene.IsValid()) { Debug.LogError(Maintainer.ConstructError("Can't open scene since path leads to invalid scene!")); return(result); } result.sceneWasLoaded = true; } result.success = true; if (activate) { SceneManager.SetActiveScene(targetScene); } return(result); }
private static bool RevealAndSelectGameObjectInPrefab(string enclosingAssetPath, string transformPath, long objectId, long componentId) { /*Debug.Log("LOOKING FOR objectId " + objectId); * Debug.Log("enclosingAssetPath " + enclosingAssetPath);*/ var targetAsset = AssetDatabase.LoadMainAssetAtPath(enclosingAssetPath) as GameObject; var prefabType = PrefabUtility.GetPrefabAssetType(targetAsset); GameObject target; if (prefabType == PrefabAssetType.Model) { target = targetAsset; } else { if (!AssetDatabase.OpenAsset(targetAsset)) { Debug.LogError(Maintainer.ConstructError("Couldn't open prefab at " + enclosingAssetPath + "!")); return(false); } var stage = PrefabStageUtility.GetCurrentPrefabStage(); if (stage == null) { Debug.LogError(Maintainer.ConstructError("Couldn't get prefab stage for prefab at " + enclosingAssetPath + "!")); return(false); } target = stage.prefabContentsRoot; } if (target == null) { Debug.LogError(Maintainer.ConstructError("Couldn't find target Game Object " + transformPath + " at " + enclosingAssetPath + " with ObjectID " + objectId + "!")); return(false); } target = CSObjectTools.FindChildGameObjectRecursive(target.transform, objectId, target.transform.name, transformPath); EditorApplication.delayCall += () => { CSObjectTools.SelectGameObject(target, false); EditorGUIUtility.PingObject(targetAsset); if (componentId != -1) { EditorApplication.delayCall += () => { TryFoldAllComponentsExceptId(componentId); }; } }; return(true); }
private static bool RevealAndSelectGameObjectInScene(string path, string transformPath, long objectId, long componentId) { Scene targetScene; if (!string.IsNullOrEmpty(path)) { var openResult = OpenSceneForReveal(path); if (!openResult.success) { return(false); } targetScene = openResult.scene; } else { targetScene = CSSceneTools.GetUntitledScene(); } if (!targetScene.IsValid()) { Debug.LogError(Maintainer.ConstructError("Target scene is not valid or not found! Scene path: " + path + ", looked for ObjectID " + objectId + "!")); return(false); } var target = CSObjectTools.FindGameObjectInScene(targetScene, objectId, transformPath); if (target == null) { Debug.LogError(Maintainer.ConstructError("Couldn't find target Game Object " + transformPath + " at " + path + " with ObjectID " + objectId + "!")); return(false); } // workaround for a bug when Unity doesn't expand hierarchy in scene EditorApplication.delayCall += () => { EditorGUIUtility.PingObject(target); }; CSObjectTools.SelectGameObject(target, true); var enclosingAssetInstanceId = CSAssetTools.GetMainAssetInstanceID(path); EditorApplication.delayCall += () => { EditorGUIUtility.PingObject(enclosingAssetInstanceId); }; if (componentId != -1) { return(TryFoldAllComponentsExceptId(componentId)); } return(true); }
public static void Save() { if (cachedMap != null) { SaveMap(MapPath, cachedMap); } else { Debug.LogError(Maintainer.ConstructError("Can't save AssetsMap, no cache found!")); } }
public static Object GetInSceneRenderSettings() { var mi = CSReflectionTools.GetGetRenderSettingsMethodInfo(); if (mi != null) { return((Object)mi.Invoke(null, null)); } Debug.LogError(Maintainer.ConstructError("Can't retrieve RenderSettings object via reflection!")); return(null); }
public static int GetMainAssetInstanceID(string path) { var mi = CSReflectionTools.GetGetMainAssetInstanceIDMethodInfo(); if (mi != null) { return((int)mi.Invoke(null, new object[] { path })); } Debug.LogError(Maintainer.ConstructError("Can't retrieve InstanceID From path via reflection!")); return(-1); }
public static void EndVerticalSplit() { if (endVerticalSplitMethodInfo == null) { endVerticalSplitMethodInfo = splitterGUILayoutType.GetMethod("EndVerticalSplit", BindingFlags.Static | BindingFlags.Public); } if (endVerticalSplitMethodInfo == null) { Debug.LogError(Maintainer.ConstructError("Can't get SplitterGUILayout.EndVerticalSplit method!")); return; } endVerticalSplitMethodInfo.Invoke(null, null); }
private void Init(T[] newData) { if (newData == null) { throw new ArgumentNullException("newData", Maintainer.ConstructError("Input data is null. Ensure input is a non-null list!")); } data = newData; if (data.Length > 0) { root = TreeItemUtility.ArrayToTree(data); } maxID = data.Max(e => e.id); }
public static void BeginVerticalSplit(object spl, GUILayoutOption[] guiLayoutOptions) { if (beginVerticalSplitMethodInfo == null) { beginVerticalSplitMethodInfo = splitterGUILayoutType.GetMethod("BeginVerticalSplit", BindingFlags.Static | BindingFlags.Public, null, CallingConventions.Standard, new [] { splitterStateType, typeof(GUILayoutOption[]) }, null); } if (beginVerticalSplitMethodInfo == null) { Debug.LogError(Maintainer.ConstructError("Can't get SplitterGUILayout.BeginVerticalSplit method!")); return; } beginVerticalSplitMethodInfo.Invoke(null, new [] { spl, guiLayoutOptions }); }
public static UnityEventBase GetDummyEvent(SerializedProperty property) { if (getDummyEventMethodInfo == null) { getDummyEventMethodInfo = typeof(UnityEventDrawer).GetMethod("GetDummyEvent", BindingFlags.NonPublic | BindingFlags.Static); } if (getDummyEventMethodInfo == null) { Debug.LogError(Maintainer.ConstructError("Can't get UnityEventDrawer.GetDummyEvent method!")); return(null); } return((UnityEventBase)getDummyEventMethodInfo.Invoke(null, new [] { property })); }
private static void SaveInstance(ProjectSettings settingsInstance) { if (!System.IO.Directory.Exists(Directory)) { System.IO.Directory.CreateDirectory(Directory); } try { UnityEditorInternal.InternalEditorUtility.SaveToSerializedFileAndForget(new Object[] { settingsInstance }, Path, true); } catch (Exception ex) { Debug.LogError(Maintainer.ConstructError("Can't save settings!\n" + ex)); } }
public static EditorWindow GetInspectorWindow() { if (CSReflectionTools.inspectorWindowType == null) { Debug.LogError(Maintainer.ConstructError("Can't find UnityEditor.InspectorWindow type!")); return(null); } var inspectorWindow = EditorWindow.GetWindow(CSReflectionTools.inspectorWindowType); if (inspectorWindow == null) { Debug.LogError(Maintainer.ConstructError("Can't get an InspectorWindow!")); return(null); } return(inspectorWindow); }
protected override IList <TreeViewItem> BuildRows(TreeViewItem root) { if (TreeModel.root == null) { Debug.LogError(Maintainer.ConstructError("tree model root is null. did you call SetData()?")); return(rows); } rows.Clear(); if (TreeModel.root.HasChildren) { AddChildrenRecursive(TreeModel.root, 0, rows); } SetupParentsAndChildrenFromDepths(root, rows); SortIfNeeded(root, rows); return(rows); }
public void AddRoot(T newRoot) { if (newRoot == null) { throw new ArgumentNullException("newRoot", Maintainer.ConstructError("newRoot is null!")); } if (data == null) { throw new InvalidOperationException(Maintainer.ConstructError("Internal Error: data list is null!")); } if (data.Length != 0) { throw new InvalidOperationException(Maintainer.ConstructError("AddRoot is only allowed on empty data list!")); } newRoot.id = GenerateUniqueID(); newRoot.depth = -1; ArrayUtility.Add(ref data, newRoot); }
private static bool RevealAndSelectGameObjectInScene(string enclosingAssetPath, string transformPath, long objectId, long componentId) { var openResult = OpenSceneForReveal(enclosingAssetPath); if (!openResult.success) { return(false); } var target = CSObjectTools.FindGameObjectInScene(openResult.scene, objectId, transformPath); if (target == null) { Debug.LogError(Maintainer.ConstructError("Couldn't find target Game Object " + transformPath + " at " + enclosingAssetPath + " with ObjectID " + objectId + "!")); return(false); } // workaround for a bug when Unity doesn't expand hierarchy in scene EditorApplication.delayCall += () => { EditorGUIUtility.PingObject(target); }; CSObjectTools.SelectGameObject(target, true); var enclosingAssetInstanceId = CSAssetTools.GetMainAssetInstanceID(enclosingAssetPath); EditorApplication.delayCall += () => { EditorGUIUtility.PingObject(enclosingAssetInstanceId); }; if (componentId != -1) { return(TryFoldAllComponentsExceptId(componentId)); } return(true); }
public void RemoveElements(IList <T> elements) { foreach (var element in elements) { if (element == root) { throw new ArgumentException(Maintainer.ConstructError("It is not allowed to remove the root element!")); } } var commonAncestors = TreeItemUtility.FindCommonAncestorsWithinList(elements); foreach (var element in commonAncestors) { element.Parent.Children.Remove(element); element.Parent = null; } TreeItemUtility.TreeToList(root, data); Changed(); }