LoginSignup
0
0

More than 3 years have passed since last update.

シーン上で使われているスクリプトを確認

Last updated at Posted at 2019-04-25

プロジェクト合流時にオブジェクティブなものに出会ったので作りました。
コピペしてeditorフォルダなりに投入すれば
前のシーンとのスクリプト差分が確認できます。

HierarchyScriptViewerWindow.cs

namespace HierarchyScriptViewer
{
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using UnityEditor;
    using UnityEngine;

    /// <summary>
    /// シーン内でアタッチされているすべてのスクリプトを表示するEditorWindow
    /// </summary>
        public class HierarchyScriptViewerWindow : EditorWindow

        {

            Vector2 scrollPosition = Vector2.zero;

            MonoScript[] preScripts = new MonoScript[0];

            MonoScript[] scripts = new MonoScript[0];

            Dictionary<MonoScript, bool> dicScripts = new Dictionary<MonoScript, bool>();

            string scenescript;







            [MenuItem("Tools/Hierarchy Script Viewer")]

            static void Open()

            {

                GetWindow<HierarchyScriptViewerWindow>();

            }





            void OnGUI()

            {

                GUILayout.Label("ヒエラルキー上で使用されている全スクリプトを表示します");

                GUILayout.Space(2f);





                // ボタン表示

                if (GUILayout.Button("スクリプト一覧 表示"))

                {

                    SetDisctionary();

                }





                this.scrollPosition = EditorGUILayout.BeginScrollView(this.scrollPosition);





                foreach (var key in dicScripts.Keys)

                {

                    if (dicScripts[key])

                    {

                        GUI.backgroundColor = Color.red;

                    }

                    else

                    {

                        GUI.backgroundColor = Color.white;

                    }

                    EditorGUILayout.ObjectField(key, typeof(MonoScript), false);

                }



                EditorGUILayout.EndScrollView();





                GUI.backgroundColor = Color.white;

                //2019/02/20 保存ウィンドウ

                if (GUILayout.Button("保存"))

                {

                    // 保存先のファイルパスを取得する

                    var filePath = EditorUtility.SaveFilePanel("Save", "Assets", "default_name", "txt");





                    this.scenescript = "";





                    // スクリプト一覧表示

                    if (dicScripts != null && dicScripts.Count != 0)

                    {

                        foreach (var key in dicScripts.Keys)

                        {

                            if (dicScripts[key])

                            {

                                scenescript += "<color=red>" + key.name + "</color>" + "\r\n";

                            }

                            else

                            {

                                scenescript += key.name + "\r\n";

                            }

                        }

                    }





                    // パスが入っていれば選択されたということ(キャンセルされたら入ってこない)

                    if (!string.IsNullOrEmpty(filePath))

                    {

                        // 保存処理

                        textSave(scenescript, filePath);

                    }

                }

            }





            /// <summary>

            /// シーン内でアタッチされているすべての自作スクリプトを取得する

            /// </summary>

            private IEnumerable<MonoScript> GetScripts()

            {

                var gameObjects = (GameObject[])UnityEngine.Object.FindObjectsOfType(typeof(GameObject)); // シーン内の全てのGameObject

                var monoScripts = Resources.FindObjectsOfTypeAll<MonoScript>(); // プロジェクト内の全てのスクリプト





                foreach (var monoScript in monoScripts)

                {

                    var classType = monoScript.GetClass();

                    if (classType == null) { continue; }

                    if (classType.Module.Name != "Assembly-CSharp.dll") { continue; } // 自作クラスかどうか

                    if (!classType.IsSubclassOf(typeof(MonoBehaviour))) { continue; } // MonoBehaviour継承クラスかどうか

                    if (gameObjects.Any(go => go.GetComponent(classType) != null)) // アタッチされているGameObjectが存在するか

                    {

                        yield return monoScript;

                    }

                }

            }





            private void SetDisctionary()

            {

                dicScripts.Clear();





                if (scripts != null && scripts.Length != 0)

                {

                    preScripts = new MonoScript[scripts.Length];

                    Array.Copy(scripts, preScripts, scripts.Length);

                }

                this.scripts = this.GetScripts().ToArray();





                foreach (var item in this.scripts)

                {

                    bool isAlreadyExist = false;

                    if (preScripts != null && preScripts.Length != 0)

                    {

                        if (preScripts.Contains(item))

                        {

                            isAlreadyExist = true;

                        }

                    }

                    dicScripts.Add(item, isAlreadyExist);

                }

            }





            public void textSave(string txt, string filepath)

            {

                StreamWriter sw = new StreamWriter(filepath, false); //true=追記 false=上書き

                sw.WriteLine(txt);

                sw.Flush();

                sw.Close();

            }

        }

    }


0
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
0
0