protected override void OnBound(MemberInfo variable) { base.OnBound(variable); isArray = BoundVariableType.IsArray; elementType = isArray ? BoundVariableType.GetElementType() : BoundVariableType.GetGenericArguments()[0]; }
private void OnSizeChange(ChangeEvent <string> input) { if (int.TryParse(input.newValue, out int size)) { if (size > 100) { UnityEngine.Debug.LogWarning("输入的数组长度过大"); sizeInput.value = input.previousValue; return; } if (size != Length && size >= 0) { int currLength = Length; if (IsArray) { Array array = (Array)Value; Array newArray = Array.CreateInstance(BoundVariableType.GetElementType(), size); if (size > currLength) { if (array != null) { Array.ConstrainedCopy(array, 0, newArray, 0, currLength); } for (int i = size - currLength; i < currLength; i++) { object v = Activator.CreateInstance(BoundVariableType); newArray.SetValue(v, i); } } else { Array.ConstrainedCopy(array, 0, newArray, 0, size); } Value = newArray; } else { IList list = (IList)Value; int differLength = size - currLength; if (differLength > 0) { if (list == null) { list = (IList)Activator.CreateInstance(typeof(List <>).MakeGenericType(BoundVariableType.GetGenericArguments()[0])); } for (int i = 0; i < differLength; i++) { list.Add(default);
private object GetTemplateElement(object value) { Array array = null; IList list = null; if (isArray) { array = (Array)value; } else { list = (IList)value; } object template = null; Type elementType = isArray ? BoundVariableType.GetElementType() : BoundVariableType.GetGenericArguments()[0]; #if UNITY_EDITOR || !NETFX_CORE if (elementType.IsValueType) #else if (elementType.GetTypeInfo().IsValueType) #endif { if (isArray && array != null && array.Length > 0) { template = array.GetValue(array.Length - 1); } else if (!isArray && list != null && list.Count > 0) { template = list[list.Count - 1]; } else { template = Activator.CreateInstance(elementType); } } else if (typeof(Object).IsAssignableFrom(elementType)) { if (isArray && array != null && array.Length > 0) { template = array.GetValue(array.Length - 1); } else if (!isArray && list != null && list.Count > 0) { template = list[list.Count - 1]; } else { template = null; } } else if (elementType.IsArray) { template = Array.CreateInstance(elementType, 0); } #if UNITY_EDITOR || !NETFX_CORE else if (elementType.IsGenericType && elementType.GetGenericTypeDefinition() == typeof(List <>)) #else else if (elementType.GetTypeInfo().IsGenericType&& elementType.GetGenericTypeDefinition() == typeof(List <>)) #endif { template = Activator.CreateInstance(typeof(List <>).MakeGenericType(elementType)); } else { template = elementType.Instantiate(); } return(template); }
private bool OnSizeChanged(BoundInputField source, string input) { int value; if (int.TryParse(input, NumberStyles.Integer, RuntimeInspectorUtils.numberFormat, out value) && value >= 0) { int currLength = Length; if (currLength != value) { if (isArray) { Array array = (Array)Value; Array newArray = Array.CreateInstance(BoundVariableType.GetElementType(), value); if (value > currLength) { if (array != null) { Array.ConstrainedCopy(array, 0, newArray, 0, currLength); } for (int i = currLength; i < value; i++) { object template = GetTemplateElement(array); if (template != null) { newArray.SetValue(template, i); } } } else { Array.ConstrainedCopy(array, 0, newArray, 0, value); } Value = newArray; } else { IList list = (IList)Value; int deltaLength = value - currLength; if (deltaLength > 0) { if (list == null) { list = (IList)Activator.CreateInstance(typeof(List <>).MakeGenericType(BoundVariableType.GetGenericArguments()[0])); } for (int i = 0; i < deltaLength; i++) { list.Add(GetTemplateElement(list)); } } else { for (int i = 0; i > deltaLength; i--) { list.RemoveAt(list.Count - 1); } } Value = list; } Inspector.RefreshDelayed(); } return(true); } return(false); }
protected override void OnBound() { base.OnBound(); elementType = IsArray ? BoundVariableType.GetElementType() : BoundVariableType.GetGenericArguments()[0]; sizeInput.value = Length.ToString(); }