Last active
November 1, 2019 13:38
-
-
Save gatosyocora/f0fc1d939e4e34e91936c8aa6336849c to your computer and use it in GitHub Desktop.
任意のコンポーネントのプロパティ名一覧を取得する (同じオブジェクトにつけてReload->コンポーネント選択->Show Properties)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
using System.Linq; | |
#if UNITY_EDITOR | |
using UnityEditor; | |
#endif | |
public class ComponentProperties : MonoBehaviour { | |
[HideInInspector] | |
public Component[] components; | |
[HideInInspector] | |
public string[] componentNames; | |
// アタッチ時に実行 | |
public void Reset() | |
{ | |
var componentPropertiesTypeName = GetType().ToString(); | |
components = gameObject | |
.GetComponents<Component>() | |
.Where(x => x.GetType() != GetType()) | |
.ToArray(); | |
componentNames = components | |
.Select(x => x.GetType().Name) | |
.ToArray(); | |
} | |
} | |
#if UNITY_EDITOR | |
[CustomEditor(typeof(ComponentProperties))] | |
public class ComponentPropertiesEditor : Editor | |
{ | |
private int selectedComponent = 0; | |
private string[] propertyNames = null; | |
public override void OnInspectorGUI() | |
{ | |
base.OnInspectorGUI(); | |
var behaivor = target as ComponentProperties; | |
using (new EditorGUILayout.HorizontalScope()) | |
{ | |
GUILayout.FlexibleSpace(); | |
if (GUILayout.Button("Reload")) | |
{ | |
behaivor.Reset(); | |
} | |
} | |
EditorGUILayout.Space(); | |
selectedComponent = EditorGUILayout.Popup("Target", selectedComponent, behaivor.componentNames); | |
if (GUILayout.Button("Show Properties")) | |
{ | |
propertyNames = ShowComponentPropertices(behaivor.components[selectedComponent]); | |
} | |
if (propertyNames != null) | |
{ | |
foreach (var propertyName in propertyNames) | |
{ | |
EditorGUILayout.LabelField(propertyName); | |
} | |
} | |
} | |
// reference: http://light11.hatenadiary.com/entry/2018/03/15/225709 | |
private string[] ShowComponentPropertices(Component component) | |
{ | |
List<string> propertyNames = new List<string>(); | |
var serializedObject = new SerializedObject(component); | |
var iterator = serializedObject.GetIterator(); | |
while (iterator.NextVisible(true)) | |
{ | |
propertyNames.Add("("+ iterator.type + ") "+iterator.propertyPath); | |
} | |
return propertyNames.ToArray(); | |
} | |
} | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment