namespace labelUtility { using UnityEditor; using UnityEngine; using System; using System.IO; using System.Linq; using System.Reflection; using System.Collections; using System.Collections.Generic; using System.Text.RegularExpressions; public class InstanceUtility { private class RelativeObject { public int EnumerableIndex; public bool IsEnumerable; public string FieldName; public RelativeObject(string fieldName, string enumerableIndex = null) { FieldName = fieldName; if (enumerableIndex == null) { IsEnumerable = false; } else { EnumerableIndex = int.Parse(enumerableIndex.Substring(5, 1)); IsEnumerable = true; } } public object GetObject(object originObject) { object relativeObject = originObject.GetType().GetField(FieldName).GetValue(originObject); if (IsEnumerable) { int index = 0; foreach (var obj in relativeObject as IEnumerable) { if (index == EnumerableIndex) return obj; else index++; } } else { return relativeObject; } throw new Exception($"{FieldName} {IsEnumerable} {EnumerableIndex}"); } } public static object GetObject(FieldInfo fieldInfo, SerializedProperty property) { List infos = property.propertyPath.Split('.').ToList(); List relativeObjects = new List(); for (int i = 0; i < infos.Count; i++) { if (i < infos.Count - 1 && infos[i + 1] == "Array") { relativeObjects.Add(new RelativeObject(infos[i], infos[i + 2])); i += 2; } else { relativeObjects.Add(new RelativeObject(infos[i])); } } object obj = property.serializedObject.targetObject; foreach (var relativeObject in relativeObjects) { obj = relativeObject.GetObject(obj); } return obj; } public static T GetPropertyDrawerInstance(FieldInfo fieldInfo, SerializedProperty property) where T : class { return GetObject(fieldInfo, property) as T; } } }