Powerhoof

SelectionLog

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

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×