Unityエディタ拡張でシーン内のオブジェクトに関連付けられたコンポーネント内の変数を取得する
タイトルめっちゃ長い
シーン内の変数一覧が欲しくなったのでエディタ拡張から取得出来ないか頑張ってみました
本当は値も欲しかったのだけど、今回とれたのは変数名一覧だけでした
using UnityEngine; using UnityEditor; using System; using System.Collections; using System.Collections.Generic; using System.Reflection; public class EditorExWindow : EditorWindow { [MenuItem("Window/EditorEx")] static void Open() { EditorWindow.GetWindow<EditorExWindow>( "EditorEx" ); } string textArea = ""; void OnGUI() { if( GUILayout.Button("Caputure") ) Find(); GUILayout.Label( "TextArea" ); textArea = GUILayout.TextArea( textArea ); } private void Find (){ // TextAreaクリア textArea = ""; List<GameObject> AllObjects = new List<GameObject>(); List<MonoBehaviour> allMonobehaviourList = new List<MonoBehaviour> (); foreach (GameObject obj in UnityEngine.Resources.FindObjectsOfTypeAll(typeof(GameObject))) { // アセットからパスを取得.シーン上に存在するオブジェクトの場合,シーンファイル(.unity)のパスを取得. string path = AssetDatabase.GetAssetOrScenePath(obj); // シーン上に存在するオブジェクトかどうか文字列で判定. bool isScene = path.Contains(".unity"); // シーン上に存在するオブジェクトならば処理. if (isScene) { AllObjects.Add (obj); } } allMonobehaviourList.AddRange (GetComponentsInList<MonoBehaviour> (AllObjects)); foreach(var componentData in allMonobehaviourList){ foreach(MemberInfo mi in componentData.GetType().GetMembers(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly)){ textArea += mi.Name + "\n"; } } } public static IEnumerable<T> GetComponentsInList<T> (IEnumerable<GameObject> gameObjects ) where T : Component { var componentList = new List<T> (); foreach (var obj in gameObjects) { var components = obj.GetComponents<T> (); if (components != null) { foreach(var component in components){ componentList.Add (component); } } } return componentList; } }
変数名が取れたんだから中身も取れると思うんだけど...
もうちょっとがんばろう