private void RenderIGameDataModelField(FieldInfo field, DataField attribute, int fieldCount) { if (fieldCount > 0) { EditorGUILayout.Space(); } //EditorGUILayout.Foldout(true, "hello"); EditorGUILayout.LabelField(attribute.EditorLabel, EditorStyles.boldLabel); IGameDataModel model = field.GetValue(this) as IGameDataModel; if (model == null) { model = System.Activator.CreateInstance(field.FieldType) as IGameDataModel; } if (model != null) { model.RenderForm(true); } //EditorGUILayout.EndToggleGroup(); }
private void RenderModelAdd() { if (_model != null) { //EditorGUILayout.LabelField(""); //EditorGUILayout.Space(); _model.RenderForm(false); //EditorGUILayout.Space(); EditorGUILayout.EndScrollView(); EditorGUILayout.BeginHorizontal(); GUILayout.FlexibleSpace(); if (GUILayout.Button("Add", GUILayout.Width(100))) { GUI.FocusControl(null); if (_data.AddModelData(_currentModelID, _model)) { EditorUtility.SetDirty(_data); _model = null; _state = State.Blank; } else { EditorUtility.DisplayDialog("Error", "Model data with this ID already exists", "OK"); } } else if (GUILayout.Button("Cancel", GUILayout.Width(100))) { GUI.FocusControl(null); _model = null; _state = State.Blank; } EditorGUILayout.EndHorizontal(); } }
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); }