[Unity6] シーン上の自身が作成したスクリプトのみ取得する方法。

(2025/02/14)
[Unity6] シーン上の自身が作成したスクリプトのみ取得する方法。

開発環境
  • Unityのバージョン : 6000.0.36f1
  • 全GameObjectから自作スクリプトをすべて取得する方法

    Unityでシーン上の自身が作成したスクリプトのみ取得するには、以下のコードのスクリプトを適当なゲームオブジェクトにアタッチします。 アタッチした状態でエディターを実行すると、コンソールに取得したスクリプト名とそのスクリプトがアタッチされているゲームオブジェクトの名前が出力されます。

    using UnityEngine;
    using System;
    using System.Collections.Generic;
    
    public class SampleScript : MonoBehaviour
    {
        void Start()
        {
            // シーン上のすべてのGameObjectを取得
            GameObject[] allGameObjects = FindObjectsByType<GameObject>(FindObjectsSortMode.None);
    
            // 自身で作成したスクリプトを格納するリスト (MonoBehaviour のみ)
            List<MonoBehaviour> customScripts = new List<MonoBehaviour>();
    
            // 各GameObjectにアタッチされているコンポーネントをチェック
            foreach (GameObject go in allGameObjects)
            {
                Component[] components = go.GetComponents<Component>();
                foreach (Component component in components)
                {
                    // コンポーネントの型がUnityのビルトイン型でなく、MonoBehaviourを継承しているかチェック
                    if (component != null && !IsBuiltInUnityType(component.GetType()) && component is MonoBehaviour monoBehaviour)
                    {
                        // 自身で作成したスクリプトとみなしてリストに追加
                        customScripts.Add(monoBehaviour);
                    }
                }
            }
    
            if (customScripts.Count > 0)
            {
                // 自身で作成したスクリプトを表示
                foreach (MonoBehaviour script in customScripts)
                {
                    Debug.Log($"- {script.GetType().Name} on GameObject: {script.gameObject.name}");
                }
            }
        }
    
        // 指定された型がUnityのビルトイン型かどうかを判定するメソッド
        private bool IsBuiltInUnityType(Type type)
        {
            return type.Namespace != null && type.Namespace.StartsWith("UnityEngine");
        }
    }
    

    自身以外のすべてのスクリプトを非アクティブにする方法。

    上記のコードのforeach (MonoBehaviour script in customScripts)の部分を変更することで、自身以外のスクリプトを非アクティブにすることができます。

    using UnityEngine;
    using System;
    using System.Collections.Generic;
    
    public class SampleScript : MonoBehaviour
    {
        void Start()
        {
            // シーン上のすべてのGameObjectを取得
            GameObject[] allGameObjects = FindObjectsByType<GameObject>(FindObjectsSortMode.None);
    
            // 自身で作成したスクリプトを格納するリスト (MonoBehaviour のみ)
            List<MonoBehaviour> customScripts = new List<MonoBehaviour>();
    
            // 各GameObjectにアタッチされているコンポーネントをチェック
            foreach (GameObject go in allGameObjects)
            {
                Component[] components = go.GetComponents<Component>();
                foreach (Component component in components)
                {
                    // コンポーネントの型がUnityのビルトイン型でなく、MonoBehaviourを継承しているかチェック
                    if (component != null && !IsBuiltInUnityType(component.GetType()) && component is MonoBehaviour monoBehaviour)
                    {
                        // 自身で作成したスクリプトとみなしてリストに追加
                        customScripts.Add(monoBehaviour);
                    }
                }
            }
    
            if (customScripts.Count > 0)
            {
                foreach (MonoBehaviour script in customScripts)
                {
                    // 自身以外のスクリプトを無効化
                    if (script != this)
                    {
                        script.enabled = false;
                    }
                }
            }
        }
    
        // 指定された型がUnityのビルトイン型かどうかを判定するメソッド
        private bool IsBuiltInUnityType(Type type)
        {
            return type.Namespace != null && type.Namespace.StartsWith("UnityEngine");
        }
    }