Advertisement
Powerhoof

SelectionLog

Nov 29th, 2017
3,684
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.06 KB | None | 0 0
  1. //-----------------------------------------------------------------
  2. // Selection Log - Keeps history of your selected objects in unity
  3. //
  4. //  To use, stick this under a folder named "Editor"
  5. //   then select "Window->Selection Log" from toolbar
  6. //
  7. //  Feel free to use/adapt however you want
  8. //
  9. //   More info:
  10. //     http://tools.powerhoof.com
  11. //     @DuzzOnDrums
  12. //
  13. //-----------------------------------------------------------------
  14.  
  15. using UnityEditor;
  16. using UnityEngine;
  17. using System.Collections;
  18. using System.Collections.Generic;
  19.  
  20. namespace PowerTools
  21. {
  22.  
  23. public class SelectionLog : EditorWindow
  24. {
  25.     #region Definitions
  26.  
  27.     [System.Serializable]
  28.     class SelectedObject
  29.     {
  30.         public bool m_locked = false;
  31.         public Object m_object = null;
  32.         public bool m_inScene = false;
  33.     }
  34.  
  35.     static readonly int MAX_ITEMS = 50;
  36.     static readonly string STRING_INSCENE = "*";
  37.  
  38.     #endregion
  39.     #region Vars: Private
  40.  
  41.     [SerializeField] SelectedObject m_selectedObject = null;
  42.     [SerializeField] List<SelectedObject> m_selectedObjects = new List<SelectedObject>();
  43.  
  44.     [SerializeField] Vector2 m_scrollPosition = Vector2.zero;
  45.  
  46.     [System.NonSerialized]
  47.     GUIStyle m_lockButtonStyle;
  48.     [System.NonSerialized]
  49.     GUIContent m_searchButtonContent = null;
  50.  
  51.     #endregion
  52.     #region Funcs: Private
  53.  
  54.     // Add menu item named "Super Animation Editor" to the Window menu
  55.     [MenuItem("Window/Selection Log")]
  56.     static void ShowWindow()
  57.     {
  58.         //Show existing window instance. If one doesn't exist, make one.
  59.         EditorWindow.GetWindow<SelectionLog>("Selection Log");
  60.     }
  61.  
  62.     /// Called by unity when selection changes
  63.     void OnSelectionChange()
  64.     {
  65.         if ( Selection.activeObject == null )
  66.         {
  67.             m_selectedObject = null;
  68.         }
  69.         else if ( m_selectedObject == null || m_selectedObject.m_object != Selection.activeObject )
  70.         {
  71.             // If the object's in the list, select it, and if not locked, move it to the top of the list
  72.             m_selectedObject = m_selectedObjects.Find(item => item.m_object == Selection.activeObject);
  73.  
  74.             if ( m_selectedObject == null )
  75.             {
  76.                 // Insert a new selected object
  77.                 m_selectedObject = new SelectedObject()
  78.                     {
  79.                         m_object = Selection.activeObject,
  80.                         m_inScene = AssetDatabase.Contains(Selection.activeInstanceID) == false
  81.                     };
  82.                 InsertInList(m_selectedObject);
  83.             }
  84.             else if ( m_selectedObject.m_locked == false )
  85.             {
  86.                 // If object is already in the list, move it to the top (unless it's locked)
  87.                 m_selectedObjects.Remove( m_selectedObject );
  88.                 InsertInList(m_selectedObject);
  89.             }
  90.  
  91.             // Cap number of items to reasonable amount
  92.             while ( m_selectedObjects.Count > MAX_ITEMS )
  93.                 m_selectedObjects.RemoveAt(0);
  94.  
  95.             Repaint();
  96.         }
  97.     }
  98.  
  99.     void OnGUI ()
  100.     {
  101.         m_scrollPosition = EditorGUILayout.BeginScrollView(m_scrollPosition);
  102.  
  103.         GUILayout.Space(5);
  104.  
  105.         bool shownClear = false;
  106.         bool processingLocked = false;
  107.         for ( int i = m_selectedObjects.Count-1; i >= 0; --i)
  108.         {
  109.             if ( m_selectedObjects[i].m_locked == false )
  110.             {
  111.                 if ( processingLocked )
  112.                 {
  113.                     // First non-locked. so add Clear button
  114.  
  115.                     shownClear = true;
  116.                     if ( LayoutClearButton() )
  117.                         break;
  118.                     processingLocked = false;
  119.                 }
  120.             }
  121.             else
  122.             {
  123.                 processingLocked = true;
  124.             }
  125.  
  126.             LayoutItem(i, m_selectedObjects[i] );
  127.         }
  128.  
  129.         // If clear button hasn't shown above (ie: no locked items), show it below
  130.         if ( shownClear == false )
  131.             LayoutClearButton();
  132.  
  133.         EditorGUILayout.EndScrollView();
  134.  
  135.     }
  136.  
  137.     bool LayoutClearButton()
  138.     {
  139.         GUILayout.Space(5);
  140.  
  141.         bool clear = GUILayout.Button("Clear", EditorStyles.miniButton);
  142.         if ( clear )
  143.         {
  144.             for ( int j = m_selectedObjects.Count-1; j >= 0; --j)
  145.             {
  146.                 if ( m_selectedObjects[j].m_locked == false )
  147.                     m_selectedObjects.RemoveAt(j);
  148.             }
  149.         }
  150.  
  151.         GUILayout.Space(5);
  152.         return clear;
  153.     }
  154.  
  155.     void LayoutItem(int i, SelectedObject obj)
  156.     {
  157.  
  158.         // Lazy create and cache lock button style
  159.         if (m_lockButtonStyle == null)
  160.         {
  161.             GUIStyle temp = "IN LockButton";
  162.             m_lockButtonStyle = new GUIStyle(temp);
  163.             m_lockButtonStyle.margin.top = 3;
  164.             m_lockButtonStyle.margin.left = 10;
  165.             m_lockButtonStyle.margin.right = 10;
  166.         }
  167.  
  168.         GUIStyle style = EditorStyles.miniButtonLeft;
  169.         style.alignment = TextAnchor.MiddleLeft;
  170.  
  171.         if (  obj != null && obj.m_object != null)
  172.         {
  173.             GUILayout.BeginHorizontal();
  174.  
  175.             bool wasLocked = obj.m_locked;
  176.             obj.m_locked = GUILayout.Toggle( obj.m_locked, GUIContent.none, m_lockButtonStyle);
  177.             if ( wasLocked != obj.m_locked )
  178.             {
  179.                 m_selectedObjects.Remove(obj);
  180.                 InsertInList(obj);
  181.             }
  182.  
  183.             /* // Enable "up arrow" that lets you move item to top of list. Not really that useful now there's the "lock"
  184.             if ( GUILayout.Button( "\u25B2", EditorStyles.miniButtonLeft, GUILayout.MaxWidth(20) ) )
  185.             {
  186.                 m_selectedObject = obj;
  187.                 Selection.activeObject = obj.m_object;
  188.  
  189.                 // Move to top
  190.                 m_selectedObjects.Remove(obj);
  191.                 int firstNonLocked = ( m_selectedObjects.FindIndex(item => item.m_locked == true) );
  192.                 if ( obj.m_locked == false && firstNonLocked >= 0 )
  193.                 {
  194.                     m_selectedObjects.Insert(firstNonLocked, obj );
  195.                 }
  196.                 else
  197.                 {
  198.                     m_selectedObjects.Add( obj );
  199.                 }
  200.             }
  201.  
  202.             style = EditorStyles.miniButtonMid;
  203.             style.alignment = TextAnchor.MiddleLeft;
  204.             */
  205.  
  206.             if ( obj == m_selectedObject )
  207.             {
  208.                 GUI.enabled = false;
  209.             }
  210.  
  211.             string objName = obj.m_object.name;
  212.             if ( obj.m_inScene )
  213.                 objName += STRING_INSCENE; // Append string to scene instances to easily tell them apart
  214.             if ( GUILayout.Button( objName, style ) )
  215.             {
  216.                 m_selectedObject = obj;
  217.  
  218.                 // If it's a scene, load the scene
  219.                 if ( obj.m_object is SceneAsset )
  220.                 {
  221.                     UnityEditor.SceneManagement.EditorSceneManager.SaveCurrentModifiedScenesIfUserWantsTo();
  222.                     UnityEditor.SceneManagement.EditorSceneManager.OpenScene( AssetDatabase.GetAssetPath(obj.m_object) );
  223.                 }
  224.                 else
  225.                 {  
  226.                     // Not a scene, so select the object
  227.                     Selection.activeObject = obj.m_object;
  228.                 }
  229.             }
  230.  
  231.             GUI.enabled = true;
  232.  
  233.             // Lazy find and cache Search button
  234.             if ( m_searchButtonContent == null )
  235.                 m_searchButtonContent = EditorGUIUtility.IconContent("d_ViewToolZoom");
  236.  
  237.             if ( GUILayout.Button( m_searchButtonContent, EditorStyles.miniButtonRight, GUILayout.MaxWidth(25), GUILayout.MaxHeight(15) ) )
  238.             {
  239.                 EditorGUIUtility.PingObject(obj.m_object);
  240.             }
  241.  
  242.             GUILayout.EndHorizontal();
  243.         }
  244.     }
  245.  
  246.     // Adds the SelectedObject after the first non-locked object
  247.     void InsertInList(SelectedObject obj)
  248.     {
  249.         // Insert after the first non-locked object
  250.         int firstNonLocked = ( m_selectedObjects.FindIndex(item => item.m_locked == true) );
  251.         if ( firstNonLocked >= 0 )
  252.             m_selectedObjects.Insert(firstNonLocked, obj );
  253.         else
  254.             m_selectedObjects.Add( obj );
  255.     }
  256.  
  257.     #endregion
  258.  
  259. }
  260.  
  261. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement