123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100 |
- 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<string> infos = property.propertyPath.Split('.').ToList();
- List<RelativeObject> relativeObjects = new List<RelativeObject>();
- 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<T>(FieldInfo fieldInfo, SerializedProperty property) where T : class
- {
- return GetObject(fieldInfo, property) as T;
- }
- }
- }
|