12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697 |
- namespace assetBundleUtility
- {
- using UnityEditor;
- using System;
- using System.Linq;
- using System.Reflection;
- using System.Collections;
- using System.Collections.Generic;
-
- 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;
- }
- }
- }
|