ボアブログ

へっぽこUnityエンジニアの色々

hierarchyでコンポーネントのActiveを切り替えるエディタ拡張

hierarchyでゲームオブジェクトのアクティブを切り替えるエディタ拡張を作ってみた - ボアブログ

この記事の派生系です。前回はGameObjectのアクティブを切り替えていましたが今回はコンポーネントです。これまた、あると便利系。

f:id:weakboar:20170918193255g:plain

画像ではColliderとRendererをオン/オフしてます。今回も面倒なので全文

using System.Linq;
using UnityEditor;
using UnityEngine;

public static class ComponentActiveInHierarchy
{
    private static readonly Color mDisabledColor = new Color(1, 1, 1, 0.5f);

    private const int WIDTH = 16;
    private const int HEIGHT = 16;
    private const int OFFSET = 2;

    [InitializeOnLoadMethod]
    private static void Init()
    {
        EditorApplication.hierarchyWindowItemOnGUI += OnGUI;
    }

    private static void OnGUI(int instanceId, Rect selectionRect)
    {
        var go = EditorUtility.InstanceIDToObject(instanceId) as GameObject;

        if (go == null)
        {
            return;
        }

        var pos = selectionRect;
        pos.x = pos.xMax - WIDTH * OFFSET;
        pos.width = WIDTH;
        pos.height = HEIGHT;

        var components = go
            .GetComponents<Component>()
            .Where(c => c != null)
            .Where(c => !(c is Transform))
            .Reverse();

        var current = Event.current;

        foreach (var c in components)
        {
            Texture image = AssetPreview.GetMiniThumbnail(c);

            if (image == null && c is MonoBehaviour)
            {
                var ms = MonoScript.FromMonoBehaviour(c as MonoBehaviour);
                var path = AssetDatabase.GetAssetPath(ms);
                image = AssetDatabase.GetCachedIcon(path);
            }

            if (image == null)
            {
                continue;
            }

            var color = GUI.color;
            GUI.color = c.IsEnabled() ? Color.white : mDisabledColor;
            bool enable = GUI.Toggle(pos, c.IsEnabled(), image, GUI.skin.label);
            c.SetEnable(enable);
            GUI.color = color;
            pos.x -= pos.width;
        }
    }

    public static bool IsEnabled(this Component self)
    {
        if (self == null)
        {
            return true;
        }

        var type = self.GetType();
        var property = type.GetProperty("enabled", typeof(bool));

        if (property == null)
        {
            return true;
        }

        return (bool)property.GetValue(self, null);
    }

    public static void SetEnable(this Component self, bool isEnabled)
    {
        if (self == null)
        {
            return;
        }

        var type = self.GetType();
        var property = type.GetProperty("enabled", typeof(bool));

        if (property == null)
        {
            return;
        }

        property.SetValue(self, isEnabled, null);
    }
}

【Unity】Hierarchyにゲームオブジェクトが持つコンポーネントの一覧を表示するエディタ拡張 - コガネブログ

ほぼ上記記事のままで作ってますが、アクティブ切り替え周りを変えています。

元のソースの場合画像を置いて、クリックした位置でオンオフを切り替えて居ますが、現状だと切替時にオブジェクトも一緒に選択されます。その点が若干不満になっていたので上記のソースではToggleにして、Toggleに画像を貼り付けています。こうするとトグルの当たり判定が優先されゲームオブジェクトは選択されないので個人的にこちら。という感じ。