|
| 1 | +// Sourced from: https://github.com/dbrizov/NaughtyAttributes |
| 2 | + |
| 3 | +// MIT License |
| 4 | +// |
| 5 | +// Copyright (c) 2017 Denis Rizov |
| 6 | +// |
| 7 | +// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 8 | +// of this software and associated documentation files (the "Software"), to deal |
| 9 | +// in the Software without restriction, including without limitation the rights |
| 10 | +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 11 | +// copies of the Software, and to permit persons to whom the Software is |
| 12 | +// furnished to do so, subject to the following conditions: |
| 13 | +// |
| 14 | +// The above copyright notice and this permission notice shall be included in all |
| 15 | +// copies or substantial portions of the Software. |
| 16 | +// |
| 17 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 18 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 20 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 21 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 22 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 23 | +// SOFTWARE. |
| 24 | + |
| 25 | +using System; |
| 26 | +using System.Collections; |
| 27 | +using System.Reflection; |
| 28 | +using UnityEditor; |
| 29 | + |
| 30 | +namespace TNRD.Utilities |
| 31 | +{ |
| 32 | + public static class SerializedPropertyUtilities |
| 33 | + { |
| 34 | + /// <summary> |
| 35 | + /// Gets the value of a serialized property |
| 36 | + /// </summary> |
| 37 | + /// <param name="property">The property to get the value from</param> |
| 38 | + public static object GetValue(SerializedProperty property) |
| 39 | + { |
| 40 | + string path = property.propertyPath.Replace(".Array.data[", "["); |
| 41 | + object targetObject = property.serializedObject.targetObject; |
| 42 | + string[] elements = path.Split('.'); |
| 43 | + |
| 44 | + for (int i = 0; i < elements.Length - 1; i++) |
| 45 | + { |
| 46 | + string element = elements[i]; |
| 47 | + if (element.Contains("[")) |
| 48 | + { |
| 49 | + string elementName = element.Substring(0, element.IndexOf("[", StringComparison.OrdinalIgnoreCase)); |
| 50 | + int index = Convert.ToInt32(element |
| 51 | + .Substring(element.IndexOf("[", StringComparison.OrdinalIgnoreCase)) |
| 52 | + .Replace("[", string.Empty) |
| 53 | + .Replace("]", string.Empty)); |
| 54 | + targetObject = GetValue(targetObject, elementName, index); |
| 55 | + } |
| 56 | + else |
| 57 | + { |
| 58 | + targetObject = GetValue(targetObject, element); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + return targetObject; |
| 63 | + } |
| 64 | + |
| 65 | + private static object GetValue(object source, string name, int index) |
| 66 | + { |
| 67 | + IEnumerable enumerable = GetValue(source, name) as IEnumerable; |
| 68 | + if (enumerable == null) |
| 69 | + return null; |
| 70 | + |
| 71 | + IEnumerator enumerator = enumerable.GetEnumerator(); |
| 72 | + for (int i = 0; i <= index; i++) |
| 73 | + { |
| 74 | + if (!enumerator.MoveNext()) |
| 75 | + return null; |
| 76 | + } |
| 77 | + |
| 78 | + return enumerator.Current; |
| 79 | + } |
| 80 | + |
| 81 | + private static object GetValue(object source, string name) |
| 82 | + { |
| 83 | + if (source == null) |
| 84 | + return null; |
| 85 | + |
| 86 | + const BindingFlags fieldFlags = BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance; |
| 87 | + const BindingFlags propertyFlags = fieldFlags | BindingFlags.IgnoreCase; |
| 88 | + |
| 89 | + Type type = source.GetType(); |
| 90 | + |
| 91 | + while (type != null) |
| 92 | + { |
| 93 | + FieldInfo field = type.GetField(name, fieldFlags); |
| 94 | + if (field != null) |
| 95 | + return field.GetValue(source); |
| 96 | + |
| 97 | + PropertyInfo property = type.GetProperty(name, propertyFlags); |
| 98 | + if (property != null) |
| 99 | + return property.GetValue(source, null); |
| 100 | + |
| 101 | + type = type.BaseType; |
| 102 | + } |
| 103 | + |
| 104 | + return null; |
| 105 | + } |
| 106 | + } |
| 107 | +} |
0 commit comments