public static void FoldAll(MenuCommand command) { ActiveEditorTracker editorTracker = ActiveEditorTracker.sharedTracker; Editor[] editors = editorTracker.activeEditors; bool areAllFolded = true; for (int i = 1; i < editors.Length; i++) { int getVisible = editorTracker.GetVisible(i); Editor edit = editors[i]; string name = edit.name; string target = edit.target.name; Type type = edit.target.GetType(); if (editorTracker.GetVisible(i) != 0) { areAllFolded = false; break; } } for (int i = 1; i < editors.Length; i++) { editorTracker.SetVisible(i, areAllFolded ? 1 : 0); } }
public static void FoldAll(MenuCommand command) { ActiveEditorTracker editorTracker = ActiveEditorTracker.sharedTracker; Editor[] editors = editorTracker.activeEditors; bool areAllFolded = true; for (int i = 1; i < editors.Length; i++) { if (editorTracker.GetVisible(i) > 0) { areAllFolded = false; } } for (int i = 1; i < editors.Length; i++) { if (editorTracker.GetVisible(i) < 0) { continue; } editorTracker.SetVisible(i, areAllFolded ? 1 : 0); InternalEditorUtility.SetIsInspectorExpanded(editors[i].target, areAllFolded); } }
public static void MoveTop(MenuCommand command) { Component selected = command.context as Component; if (selected == null) { return; } ActiveEditorTracker editorTracker = ActiveEditorTracker.sharedTracker; Editor[] editors = editorTracker.activeEditors; for (int i = 1; i < editors.Length; i++) { Editor edit = editors[i]; Type type = edit.target.GetType(); } for (int i = 0; i < editors.Length - 2; i++) { UnityEditorInternal.ComponentUtility.MoveComponentUp(selected); } }
protected bool DoDrawCustomIMGUIInspector(Object target) { if (!ActiveEditorTracker.HasCustomEditor(target)) { GUILayout.Label("No custom inspector.", EditorStyles.boldLabel); return(false); } var editor = GetActiveEditor(target); if (editor == null) { editor = Editor.CreateEditor(target); } EditorGUIUtility.wideMode = true; GUIStyle editorWrapper = (editor.UseDefaultMargins() ? EditorStyles.inspectorDefaultMargins : GUIStyle.none); EditorGUILayout.BeginVertical(editorWrapper); { GUI.changed = false; try { editor.OnInspectorGUI(); } catch (Exception e) { Debug.LogException(e); } } EditorGUILayout.EndVertical(); return(true); }
void DestroyPreviewObjectInternal() { if (m_PreviewObject != null) { if (Selection.Contains(m_PreviewObject.gameObject)) { MeshSelection.RemoveFromSelection(m_PreviewObject.gameObject); } if (m_PreviewObject.GetComponent <MeshFilter>().sharedMesh != null) { DestroyImmediate(m_PreviewObject.GetComponent <MeshFilter>().sharedMesh); } DestroyImmediate(m_PreviewObject); // When entering play mode the editor tracker isn't rebuilt before the Inspector redraws, meaning the // preview object is still assumed to be in the selection. Flushing the selection changes by rebuilding // active editor tracker fixes this. #if UNITY_2019_3_OR_NEWER ActiveEditorTracker.RebuildAllIfNecessary(); #else var rebuildAllTrackers = typeof(ActiveEditorTracker).GetMethod("Internal_RebuildAllIfNecessary", BindingFlags.Static | BindingFlags.NonPublic); if (rebuildAllTrackers != null) { rebuildAllTrackers.Invoke(null, null); } #endif } }
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); }
internal static void SaveUnappliedTextureImporterSettings() { InspectorWindow[] allInspectorWindows = InspectorWindow.GetAllInspectorWindows(); for (int i = 0; i < allInspectorWindows.Length; i++) { InspectorWindow inspectorWindow = allInspectorWindows[i]; ActiveEditorTracker tracker = inspectorWindow.tracker; Editor[] activeEditors = tracker.activeEditors; for (int j = 0; j < activeEditors.Length; j++) { Editor editor = activeEditors[j]; TextureImporterInspector textureImporterInspector = editor as TextureImporterInspector; if (!(textureImporterInspector == null)) { if (textureImporterInspector.HasModified()) { TextureImporter textureImporter = textureImporterInspector.target as TextureImporter; if (EditorUtility.DisplayDialog("Unapplied import settings", "Unapplied import settings for '" + textureImporter.assetPath + "'", "Apply", "Revert")) { textureImporterInspector.ApplyAndImport(); } } } } } }
/// <summary> /// 展开或关闭Inspector中某类基类组件 /// </summary> /// <param name="types">组件Editor的Type字符串列表</param> /// <param name="visible">是否展开</param> /// <param name="contraOther">是否将没有在types中的组件做相反的操作,传false保持不变</param> public static void SetInspectorTrackerVisible(string[] types, bool visible, bool contraOther = true) { var windowType = typeof(EditorWindow).Assembly.GetType(GetViewTypeStr(EditorViews.InspectorWindow)); var window = GetWindow(windowType); System.Reflection.FieldInfo info = windowType.GetField("m_Tracker", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance); ActiveEditorTracker tracker = info.GetValue(window) as ActiveEditorTracker; var editors = tracker.activeEditors; for (int i = 0; i < editors.Length; i++) { bool isSame = false; foreach (var type in types) { isSame = editors[i].GetType().ToString().Equals(type); if (isSame) { tracker.SetVisible(i, isSame && visible ? 1 : 0); break; } } if (!isSame && contraOther) { tracker.SetVisible(i, isSame && visible ? 1 : 0); } } }
public static void LockInspector() { ActiveEditorTracker tracker = ActiveEditorTracker.sharedTracker; tracker.isLocked = !tracker.isLocked; tracker.ForceRebuild(); }
static void ToggleConsole() { ActiveEditorTracker tracker = ActiveEditorTracker.sharedTracker; tracker.isLocked = !tracker.isLocked; tracker.ForceRebuild(); }
private VisualElement CreateCustomInspector(SerializedObject serializedObject) { var target = serializedObject?.targetObject; if (target == null) { return(null); } if (!ActiveEditorTracker.HasCustomEditor(target)) { return(null); } var activeEditors = ActiveEditorTracker.sharedTracker?.activeEditors; var editor = activeEditors?.FirstOrDefault((e) => e.target == target); if (editor == null) { editor = Editor.CreateEditor(target); } VisualElement customInspector = null; if (mode != Mode.IMGUI) { customInspector = (editor as UIElementsEditor)?.CreateInspectorGUI(); } if (customInspector == null) { customInspector = new IMGUIContainer(() => { var originalWideMode = EditorGUIUtility.wideMode; EditorGUIUtility.wideMode = true; GUIStyle editorWrapper = (editor.UseDefaultMargins() ? EditorStyles.inspectorDefaultMargins : GUIStyle.none); EditorGUILayout.BeginVertical(editorWrapper); { GUI.changed = false; try { editor.OnInspectorGUI(); } catch (Exception e) { Debug.LogException(e); } } EditorGUILayout.EndVertical(); EditorGUIUtility.wideMode = originalWideMode; }); } customInspector.AddToClassList(s_CustumInspectorClassName); return(customInspector); }
bool AssetHasCustomEditor(SerializedProperty property) { ScriptableObject asset = property.objectReferenceValue as ScriptableObject; if (asset != null) { return(ActiveEditorTracker.HasCustomEditor(asset)); } return(false); }
bool CollectCustomEditorToolsFromTracker(ActiveEditorTracker tracker, List <EditorTool> list) { list.Clear(); var activeIsCustomEditorTool = IsCustomEditorTool(m_ActiveTool); var preservedActiveTool = false; EditorToolUtility.GetEditorToolsForTracker(tracker, s_CustomEditorTools); foreach (var customEditorTool in s_CustomEditorTools) { var toolType = customEditorTool.editorToolType; var toolOwner = customEditorTool.owner; EditorTool customEditorToolInstance; // The only case where a custom editor tool is serialized is when it is the active tool. All other // instances are discarded and rebuilt on any tracker rebuild. if (activeIsCustomEditorTool && CustomEditorToolIsMatch(toolOwner, toolType, m_ActiveTool)) { preservedActiveTool = true; m_ActiveTool.m_Targets = toolOwner.targets; m_ActiveTool.m_Target = toolOwner.target; // domain reload - the owning editor was destroyed and therefore we need to reset the EditMode active if (m_ActiveTool is EditModeTool && toolOwner.GetInstanceID() != UnityEditorInternal.EditMode.ownerID) { UnityEditorInternal.EditMode.EditModeToolStateChanged(toolOwner, ((EditModeTool)m_ActiveTool).editMode); } customEditorToolInstance = m_ActiveTool; } else { customEditorToolInstance = (EditorTool)CreateInstance(toolType, x => { ((EditorTool)x).m_Targets = toolOwner.targets; ((EditorTool)x).m_Target = toolOwner.target; }); customEditorToolInstance.hideFlags = HideFlags.DontSave; } list.Add(customEditorToolInstance); var editModeTool = customEditorToolInstance as EditModeTool; if (editModeTool != null) { editModeTool.owner = toolOwner; } } return(preservedActiveTool); }
protected bool DoDrawNormalIMGUIInspector(Object target) { if (ActiveEditorTracker.HasCustomEditor(target)) { return(DoDrawCustomIMGUIInspector(target)); } else { return(DoDrawDefaultIMGUIInspector(target)); } }
static GameObject GetLockedGameObject() //return locked gameobject from Inspector Window or null value { Type type = typeof(EditorWindow).Assembly.GetType("UnityEditor.InspectorWindow"); EditorWindow window = EditorWindow.GetWindow(type); FieldInfo fieldInfo = type.GetField("m_Tracker", BindingFlags.NonPublic | BindingFlags.Instance); ActiveEditorTracker tracker = fieldInfo.GetValue(window) as ActiveEditorTracker; MethodInfo methodInfo = tracker.GetType().GetMethod("GetObjectsLockedByThisTracker", BindingFlags.NonPublic | BindingFlags.Instance); List <UnityEngine.Object> lockedObjects = new List <UnityEngine.Object>(); methodInfo.Invoke(tracker, new object[] { lockedObjects }); return((lockedObjects.Count > 0) ? (GameObject)lockedObjects[0] : null); }
private static void CollapseComponents(MenuCommand command) { // Credit: https://forum.unity.com/threads/is-it-possible-to-fold-a-component-from-script-inspector-view.296333/#post-2353538 ActiveEditorTracker tracker = ActiveEditorTracker.sharedTracker; for (int i = 0, length = tracker.activeEditors.Length; i < length; i++) { tracker.SetVisible(i, 0); } EditorWindow.focusedWindow.Repaint(); }
static void expand() { EditorApplication.ExecuteMenuItem("Window/General/Inspector"); ActiveEditorTracker tracker = ActiveEditorTracker.sharedTracker; for (int i = 0, length = tracker.activeEditors.Length; i < length; i++) { tracker.SetVisible(i, 1); UnityEditorInternal.InternalEditorUtility.SetIsInspectorExpanded(tracker.activeEditors[i].serializedObject.targetObject, true); } EditorWindow.focusedWindow.Repaint(); }
internal static void GetEditorToolsForTracker(ActiveEditorTracker tracker, List <CustomEditorTool> tools) { tools.Clear(); s_TrackerSelectedTypes.Clear(); var editors = tracker.activeEditors; for (int i = 0, c = editors.Length; i < c; i++) { var editor = editors[i]; if (editor == null || editor.target == null) { continue; } var targetType = editor.target.GetType(); // Some components can be added to a GameObject multiple times. Prevent them from creating multiple tools. if (s_TrackerSelectedTypes.Add(targetType)) { var eligibleToolTypes = GetCustomEditorToolsForType(targetType); foreach (var type in eligibleToolTypes) { tools.Add(new CustomEditorTool(editor, type)); } } else { foreach (var tool in tools) { if (tool.owner.target.GetType() == targetType) { if (tool.additionalEditors == null) { tool.additionalEditors = new List <Editor>() { editor } } ; else { tool.additionalEditors.Add(editor); } } } } } s_TrackerSelectedTypes.Clear(); }
static void Shrinkage() { var type = typeof(EditorWindow).Assembly.GetType("UnityEditor.InspectorWindow"); var window = EditorWindow.GetWindow(type); FieldInfo info = type.GetField("m_Tracker", BindingFlags.NonPublic | BindingFlags.Instance); ActiveEditorTracker tracker = info.GetValue(window) as ActiveEditorTracker; for (int i = 0; i < tracker.activeEditors.Length; i++) { //这里1就是展开,0就是合起来 tracker.SetVisible(i, 0); } }
public static void ToggleInspectorDebugMode() { //determine if normal mode or debug mode is presently selected System.Type inspectorType = inspectorWindow.GetType(); ActiveEditorTracker tracker = ActiveEditorTracker.sharedTracker; bool isNormal = tracker.inspectorMode == InspectorMode.Normal; string methodName = isNormal ? "SetDebug" : "SetNormal"; //use reflection to enable normal/debug mode BindingFlags attr = BindingFlags.NonPublic | BindingFlags.Instance; MethodInfo methodInfo = inspectorType.GetMethod(methodName, attr); methodInfo.Invoke(inspectorWindow, null); tracker.ForceRebuild(); }
/// <summary> /// 获取到 Inspector面板 /// </summary> public void TestInspector() { Type type = typeof(EditorWindow).Assembly.GetType("UnityEditor.InspectorWindow"); EditorWindow window = EditorWindow.GetWindow(type); FieldInfo info = type.GetField("m_Tracker", BindingFlags.NonPublic | BindingFlags.Instance); ActiveEditorTracker tracker = info.GetValue(window) as ActiveEditorTracker; for (int i = 0; i < tracker.activeEditors.Length; i++) { if (tracker.activeEditors[i].target.GetType().Name == "BoxCollider") { Debug.Log("这是BoxCollider,在第 " + i + " 个"); } } }
protected void OpenComponent(string name) { var type = typeof(EditorWindow).Assembly.GetType("UnityEditor.InspectorWindow"); var window = EditorWindow.GetWindow(type); FieldInfo info = type.GetField("m_Tracker", BindingFlags.NonPublic | BindingFlags.Instance); ActiveEditorTracker tracker = info.GetValue(window) as ActiveEditorTracker; for (int i = 0; i < tracker.activeEditors.Length; i++) { //可以通过名子单独判断组件展开或不展开 if (tracker.activeEditors[i].target.GetType().Name == name) { //这里1就是展开,0就是合起来 tracker.SetVisible(i, 1); } } }
static void Expansion() { var type = typeof(EditorWindow).Assembly.GetType("UnityEditor.InspectorWindow"); var window = EditorWindow.GetWindow(type); FieldInfo info = type.GetField("m_Tracker", BindingFlags.NonPublic | BindingFlags.Instance); ActiveEditorTracker tracker = info.GetValue(window) as ActiveEditorTracker; for (int i = 0; i < tracker.activeEditors.Length; i++) { ////可以通过名子单独判断组件展开或不展开 //if (tracker.activeEditors[i].target.GetType().Name != "NewBehaviourScript") //{ //这里1就是展开,0就是合起来 tracker.SetVisible(i, 1); //} } }
public ComponentEditor(EditorTypeAssociation typeAssociation, ActiveEditorTracker tracker, Editor inspector) { if (typeAssociation.editor == null) { throw new ArgumentNullException("typeAssociation"); } if (!typeof(IEditor).IsAssignableFrom(typeAssociation.editor) || !typeof(UnityObject).IsAssignableFrom(typeAssociation.editor)) { throw new ArgumentException("Tool type must implement UnityEngine.ScriptableObject, IEditor.", "typeAssociation"); } this.tracker = tracker; m_Inspector = inspector; m_LockedInspector = tracker.isLocked; m_EditorTypeAssociation = typeAssociation; m_EditorToolScope = EditorToolScope.NotInitialized; }
public InspectorTab(EditorElement editor) { this.editor = editor; this.tracker = PropertyEditorRef.GetTracker(inspector.propertyEditor); name = GetTitle(); RestoreTabState(); AddToClassList(EditorTabClass); AddToClassList("toolbar-button"); RegisterCallbacks(); preview = AssetPreview.GetAssetPreview(target); thumbnail = AssetPreview.GetMiniThumbnail(target); icon = new FluentUITK.Icon(); Add(icon); this.Query(className: "unity-toggle__input").First().RemoveFromHierarchy(); }
public static Editor GetInspectorEditor(string type) { var windowType = typeof(EditorWindow).Assembly.GetType(GetViewTypeStr(EditorViews.InspectorWindow)); var window = GetWindow(windowType); System.Reflection.FieldInfo info = windowType.GetField("m_Tracker", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance); ActiveEditorTracker tracker = info.GetValue(window) as ActiveEditorTracker; var editors = tracker.activeEditors; for (int i = 0; i < editors.Length; i++) { bool isSame = false; Editor editor = editors[i]; isSame = editor.GetType().ToString().Equals(type); if (isSame) { return(editor); } } return(null); }
// ---------------------------------------------------------------------------- // fix missing component // ---------------------------------------------------------------------------- private static void FixMissingComponents(GameObjectIssueRecord issue, GameObject go) { CSObjectTools.SelectGameObject(go, issue.location); ActiveEditorTracker tracker = CSEditorTools.GetActiveEditorTrackerForSelectedObject(); tracker.RebuildIfNecessary(); bool touched = false; Editor[] activeEditors = tracker.activeEditors; for (int i = activeEditors.Length - 1; i >= 0; i--) { Editor editor = activeEditors[i]; if (CSObjectTools.GetLocalIdentifierInFileForObject(editor.serializedObject.targetObject) == issue.componentId) { Object.DestroyImmediate(editor.target, true); touched = true; } } if (touched) { #if UNITY_5_0_PLUS if (issue.location == RecordLocation.Scene) { CSSceneTools.MarkSceneDirty(); } else { EditorUtility.SetDirty(go); } #else EditorUtility.SetDirty(go); #endif } //CSObjectTools.SelectGameObject(null, issue.location); }
public static void FoldAll(MenuCommand command) { ActiveEditorTracker editorTracker = ActiveEditorTracker.sharedTracker; Editor[] editors = editorTracker.activeEditors; bool areAllFolded = true; for (int i = 1; i < editors.Length; i++) { if (editorTracker.GetVisible(i) != 0) { areAllFolded = false; break; } } for (int i = 1; i < editors.Length; i++) { editorTracker.SetVisible(i, areAllFolded ? 1 : 0); } }
internal static void GetEditorToolsForTracker(ActiveEditorTracker tracker, List <CustomEditorTool> tools) { tools.Clear(); var editors = tracker.activeEditors; for (int i = 0, c = editors.Length; i < c; i++) { var editor = editors[i]; if (editor == null || editor.target == null) { continue; } var targetType = editor.target.GetType(); var eligibleToolTypes = GetCustomEditorToolsForType(targetType); foreach (var type in eligibleToolTypes) { tools.Add(new CustomEditorTool(editor, type)); } } }
static bool ApplyImportSettingsBeforeSavingPreset(ref Preset preset, Object target) { // make sure modifications to importer get applied before creating preset. foreach (InspectorWindow i in InspectorWindow.GetAllInspectorWindows()) { ActiveEditorTracker activeEditor = i.tracker; foreach (Editor e in activeEditor.activeEditors) { var editor = e as AssetImporterEditor; if (editor != null && editor.target == target && editor.HasModified()) { if (EditorUtility.DisplayDialog("Unapplied import settings", "Apply settings before creating a new preset", "Apply", "Cancel")) { editor.SaveChanges(); // after reimporting, the target object has changed, so update the preset with the newly imported values. preset.UpdateProperties(editor.target); return(false); } return(true); } } } return(false); }