Skip to content

Instantly share code, notes, and snippets.

@Rovsau
Last active June 29, 2023 19:53
Show Gist options
  • Save Rovsau/3d7982a235446fa3ea2b13e50005f726 to your computer and use it in GitHub Desktop.
Save Rovsau/3d7982a235446fa3ea2b13e50005f726 to your computer and use it in GitHub Desktop.

This can be used from any other script, and returns with all the Asset GUIDs which were selected in the editor window.
It is an early version. The next one will be able to pass in pre-existing selections.

using System;
using System.Linq;
using UnityEditor;
using UnityEditorInternal;
using UnityEngine;

namespace Rovsau.Unity.SceneViewBookmarks
{
    // TODO:  Type[] targetTypes, string[] targetFolders
    public class SelectAssetWindow : EditorWindow
    {
        private static SelectAssetWindow _window;
        private static bool _isOpen;

        /// <summary>
        /// Returns GUIDs of selected assets. 
        /// </summary>
        private static Action<string[]> OnSelectionAssigned;
        private static Type _targetType;
        private static string _buttonText;
        private static int _requiredSelectionCount;

        private string[] _targetGUIDs;// Debug (make local)
        private string[] _targetPaths;// Debug (make local)
        private string[] _targetNames;// Debug (make local)
        private ReorderableList _reorderedAssets;

        /// <summary>
        /// Opens an Asset Selection Window, which lists all assets found of a single Type within the project.
        /// </summary>
        /// <returns>
        /// The GUIDs of the selected assets.
        /// </returns>
        /// <param name="onSelectionAssigned">A method which takes a string array, to receive the GUIDs from the selection.</param>
        /// <param name="targetType"></param>
        /// <param name="windowTitle"></param>
        /// <param name="buttonText"></param>
        /// <param name="requiredSelectionCount"></param>
        public static void Open(Action<string[]> onSelectionAssigned, Type targetType, string windowTitle, string buttonText, int requiredSelectionCount)
        {
            _window = GetWindow<SelectAssetWindow>(windowTitle);

            if (_isOpen)
            {
                _window.Close();
            }
            else
            {
                _isOpen = true;
                OnSelectionAssigned += onSelectionAssigned;
                _targetType = targetType;
                _buttonText = buttonText;
                _requiredSelectionCount = requiredSelectionCount;
                _window.Show();
            }
        }

        private void OnEnable()
        {
            // Find all assets of target type.
            _targetGUIDs = AssetDatabase.FindAssets("t:" + _targetType);
            _targetPaths = _targetGUIDs.Select(guid => AssetDatabase.GUIDToAssetPath(guid)).ToArray();
            _targetNames = _targetPaths.Select(path => AssetDatabase.LoadAssetAtPath(path, typeof(UnityEngine.Object)).name).ToArray();

            _reorderedAssets = new ReorderableList(_targetNames, typeof(string))
            {
                draggable = false,
                showDefaultBackground = true,
                headerHeight = 0,
                displayAdd = false,
                displayRemove = false,
                drawHeaderCallback = (Rect rect) => { },
            };
        }

        private void OnDisable()
        {
            _window = null;
            _isOpen = false;

            OnSelectionAssigned = null;
            _targetType = null;
            _buttonText = null;
            _requiredSelectionCount = 0;
        }

        private void OnGUI()
        {
            EditorGUILayout.BeginVertical(Style.MainArea);

            EditorGUILayout.LabelField("Select Scene", Style.Title);
            GUILayout.Space(4f);

            _reorderedAssets.DoLayoutList();

            #region Button
            GUI.enabled = _reorderedAssets.selectedIndices.Count >= _requiredSelectionCount;
            if (GUILayout.Button(_buttonText, Style.Button))
            {
                // Check zero selections. <---------------------------------------------------------

                // Get scene GUIDs. 
                int[] indexes = _reorderedAssets.selectedIndices.Select(index => index).ToArray();
                string[] guids = indexes.Select(index => _targetGUIDs[index]).ToArray();

                // Invoke event. 
                OnSelectionAssigned?.Invoke(guids);

                // Close window. 
                _window.Close();
            }
            GUI.enabled = true;
            #endregion

            EditorGUILayout.EndVertical();//Style.MainArea
        }
    }
    
    public static class Style
    {
        public static GUIStyle Title { get; private set; } = new GUIStyle(GUI.skin.label)
        {
            fontSize = 18,
            fontStyle = FontStyle.Bold,
        };
        public static GUIStyle ContainerTitle { get; private set; } = new GUIStyle(GUI.skin.label)
        {
            //fontSize = 18,
            fontStyle = FontStyle.Bold,
        };

        public static GUIStyle Button { get; private set; } = new GUIStyle(GUI.skin.button)
        {
            fontSize = 12,
            //fontStyle = FontStyle.Bold,
            //stretchWidth = false,
            //alignment = TextAnchor.MiddleRight,
            padding = new RectOffset(8, 8, 8, 8),

        };

        public static GUIStyle MainArea { get; private set; } = new GUIStyle()
        {
            padding = new RectOffset(8, 8, 8, 8),
            margin = new RectOffset(4, 4, 4, 4),
        };
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment