LoginSignup
2
0

More than 1 year has passed since last update.

Unity Editor拡張 選択履歴の戻る・進む

Last updated at Posted at 2019-06-09

こちらの記事の選択戻るが便利でした
[Unity] Editor拡張でInspectorの"戻る"を実現する

修正をしまして、履歴内「進む」もできるようにしました

AssetStoreを利用するのを勧められていましたが
私にとっては、これぐらいの機能でちょうど良いと感じています

実行方法

実行環境

Unity 2019.1

コンパイル後

メインメニューに
Editor/SelectionHistory/Back
Editor/SelectionHistory/Forward
が作成されます

メニューからの起動では、便利ではない(むしろ面倒くさい)ので
ショートカットキーを割当ててください

コード

using System.Collections.Generic;
using System.Linq;
using UnityEditor;
using UnityEngine;

namespace SelectionHistory.Editor
{
    public static class SelectionHistory
    {
        private static List<Object[]> _history;
        private static int _currentPosition;

        [InitializeOnLoadMethod]
        public static void Init()
        {
            _history = new List<Object[]>();
            _currentPosition = -1;
            Selection.selectionChanged += () => Set(Selection.objects);
        }

        private static void Set(Object[] objects)
        {
            if (objects.Length == 0) return;

            // 同じなら追加しない
            // Objects内容が同じでも順番が異なる場合は違うものとして判定される
            if (_currentPosition != -1 && objects.SequenceEqual(_history[_currentPosition]))
            {
                return;
            }

            // 追加する場合
            // カレントが途中にある場合、最後まで削除する
            if (_currentPosition != _history.Count - 1)
            {
                // 削除
                var n = _history.Count - (_currentPosition + 1);
                _history.RemoveRange(_currentPosition + 1, n);
            }

            _history.Add(objects);
            _currentPosition++;

            // 100より大きい場合、最初を削除する
            if (_currentPosition > 100)
            {
                _history.RemoveAt(0);
                _currentPosition--;
            }
        }

        [MenuItem("Edit/SelectionHistory/Back")]
        public static void Back()
        {
            if (_currentPosition <= 0) return;
            _currentPosition--;
            Move(_history[_currentPosition]);
        }

        [MenuItem("Edit/SelectionHistory/Forward")]
        public static void Forward()
        {
            if (_currentPosition >= _history.Count - 1) return;
            _currentPosition++;
            Move(_history[_currentPosition]);
        }

        private static void Move(Object[] objects)
        {
            Selection.objects = objects;
        }
    }
}
2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0