protected void RenderForeignKeyField <T>(FieldInfo field, DataField attribute) where T : IGameDataModel { T[] allValues = DataRererence.GetModels <T>(); List <string> strings = new List <string>(); List <int> values = new List <int>(); foreach (T value in allValues) { strings.Add(value.ListName); values.Add(value.ID); } if (field.FieldType.IsArray) { if (field.FieldType.GetElementType().Equals(typeof(int))) { RenderArrayField(field, attribute, strings.ToArray(), values.ToArray(), typeof(T)); } else { Debug.LogError("Only int fields are current supported as foreign keys"); } } else { if (field.FieldType.Equals(typeof(int))) { int currentID = 0; int.TryParse(field.GetValue(this).ToString(), out currentID); EditorGUILayout.BeginHorizontal(); currentID = EditorGUILayout.IntPopup(attribute.EditorLabel, currentID, strings.ToArray(), values.ToArray()); field.SetValue(this, currentID); T[] models = DataRererence.GetModels <T>(); foreach (T model in models) { if (model.ID.Equals(currentID)) { model.RenderPreviewForForeignKey(); } } if (GUILayout.Button("Open", GUILayout.MaxWidth(40))) { DataRererence.RegisterOpenModelRequest <T>(currentID); } EditorGUILayout.EndHorizontal(); } else { Debug.LogError("Only int fields are current supported as foreign keys"); } } }
protected void RenderArrayField(FieldInfo field, DataField attribute, string[] options = null, int[] values = null, System.Type modelType = null) { System.Array array = field.GetValue(this) as System.Array; System.Type arrayElementType = field.FieldType.GetElementType(); if (array == null) { array = System.Array.CreateInstance(arrayElementType, 0); } var listType = typeof(List <>).MakeGenericType(arrayElementType); IList list = (IList)System.Activator.CreateInstance(listType); for (int i = 0; i < array.Length; i++) { list.Add(array.GetValue(i)); } if (list.Count == 0) { EditorGUILayout.BeginHorizontal(); EditorGUILayout.PrefixLabel(attribute.EditorLabel + string.Format(" ({0:N0})", list.Count)); } int indexToRemove = -1; string label = ""; for (int i = 0; i < list.Count; i++) { label = string.Format("Element {0:D}", i); EditorGUILayout.BeginHorizontal(); if (i == 0) { EditorGUILayout.PrefixLabel(attribute.EditorLabel + string.Format(" ({0:N0})", list.Count)); } else { GUILayout.Space(EditorGUIUtility.labelWidth); } if (arrayElementType == typeof(string)) { list[i] = EditorGUILayout.TextField(label, list[i] as string); } else if (arrayElementType == typeof(Color)) { list[i] = EditorGUILayout.ColorField(label, (Color)list[i]); } else if (arrayElementType == typeof(int)) { if (options != null && values != null) { int newValue = EditorGUILayout.IntPopup(label, int.Parse(list[i].ToString()), options, values); list[i] = newValue; MethodInfo method = DataRererence.GetType().GetMethod("GetModels", BindingFlags.Instance | BindingFlags.Public); method = method.MakeGenericMethod(modelType); System.Array models = method.Invoke(DataRererence, new object[] { }) as System.Array; for (int index = 0; index < models.Length; index++) { IGameDataModel model = models.GetValue(index) as IGameDataModel; if (model != null && model.ID.Equals(newValue)) { model.RenderPreviewForForeignKey(); } } if (GUILayout.Button("Open", GUILayout.MaxWidth(40))) { method = DataRererence.GetType().GetMethod("RegisterOpenModelRequest", BindingFlags.Instance | BindingFlags.Public); method = method.MakeGenericMethod(modelType); method.Invoke(DataRererence, new object[] { newValue }); } } else { UnityEngine.RangeAttribute rangeAttr = GetAttribute <UnityEngine.RangeAttribute>(field); if (rangeAttr != null) { list[i] = EditorGUILayout.IntSlider(label, (int)list[i], Mathf.FloorToInt(rangeAttr.min), Mathf.FloorToInt(rangeAttr.max)); } else { list[i] = EditorGUILayout.IntField(label, (int)list[i]); } } } else if (arrayElementType == typeof(float)) { UnityEngine.RangeAttribute rangeAttr = GetAttribute <UnityEngine.RangeAttribute>(field); if (rangeAttr != null) { list[i] = EditorGUILayout.Slider(label, (float)list[i], rangeAttr.min, rangeAttr.max); } else { list[i] = EditorGUILayout.FloatField(label, (float)list[i]); } } else if (arrayElementType == typeof(bool)) { list[i] = EditorGUILayout.Toggle(label, (bool)list[i]); } else if (arrayElementType == typeof(AnimationCurve)) { list[i] = EditorGUILayout.CurveField(label, (AnimationCurve)list[i]); } else if (arrayElementType.IsEnum) { System.Enum newValue = EditorGUILayout.EnumPopup(label, (System.Enum)System.Enum.Parse(list[i].GetType(), list[i].ToString())); list[i] = System.Convert.ChangeType(newValue, System.Enum.GetUnderlyingType(list[i].GetType())); } else if (arrayElementType.GetInterface(typeof(IGameDataModel).Name) != null) { EditorGUILayout.LabelField(label, EditorStyles.boldLabel); IGameDataModel model = list[i] as IGameDataModel; model.SetDataReference(DataRererence); EditorGUILayout.BeginVertical(); model.RenderForm(true); EditorGUILayout.EndVertical(); } else { EditorGUILayout.LabelField(label, "Field type is not supported"); } if (GUILayout.Button("-", GUILayout.MaxWidth(20))) { if (EditorUtility.DisplayDialog("Warning!", "Are you sure?", "Yes", "No")) { indexToRemove = i; } } EditorGUILayout.EndHorizontal(); } if (indexToRemove >= 0) { list.RemoveAt(indexToRemove); } if (list.Count == 0) { if (GUILayout.Button("+", GUILayout.MaxWidth(20))) { list.Add(System.Activator.CreateInstance(arrayElementType)); } EditorGUILayout.EndHorizontal(); } else { EditorGUILayout.BeginHorizontal(); EditorGUILayout.Space(); if (GUILayout.Button("+", GUILayout.MaxWidth(20))) { list.Add(System.Activator.CreateInstance(arrayElementType)); } EditorGUILayout.EndHorizontal(); } EditorGUILayout.Space(); System.Array newArray = System.Array.CreateInstance(arrayElementType, list.Count); for (int i = 0; i < list.Count; i++) { newArray.SetValue(list[i], i); } field.SetValue(this, newArray); }