void ValueGUI(Rect position, SerializedProperty property, ValueProperty.PropertyType propertyType, string valueType) { SerializedProperty m_Target = property.FindPropertyRelative("m_Target"); SerializedProperty m_Value = property.FindPropertyRelative("m_Value"); Rect rect = new Rect(position.x + 40f, position.y, position.width - (40f), EditorGUIUtility.singleLineHeight); switch (propertyType) { case ValueProperty.PropertyType.Linked: EditorGUI.PropertyField(rect, m_Target, GUIContent.none); break; case ValueProperty.PropertyType.Custom: if (valueType == typeof(string).ToString()) { m_Value.stringValue = EditorGUI.TextField(rect, m_Value.stringValue); } else if (valueType == typeof(bool).ToString()) { if (m_Value.stringValue != "True" && m_Value.stringValue != "False") { m_Value.stringValue = "True"; } BoolPopup boolPopup = (BoolPopup)Enum.Parse(typeof(BoolPopup), m_Value.stringValue); boolPopup = (BoolPopup)EditorGUI.EnumPopup(rect, boolPopup); m_Value.stringValue = boolPopup.ToString(); } else if (valueType == typeof(int).ToString() || valueType == typeof(short).ToString() || valueType == typeof(long).ToString()) { var intValue = 0; int.TryParse(m_Value.stringValue, out intValue); intValue = EditorGUI.IntField(rect, intValue); m_Value.stringValue = intValue.ToString(); } else if (valueType == typeof(float).ToString() || valueType == typeof(double).ToString() || valueType == typeof(decimal).ToString()) { var floatValue = 0f; float.TryParse(m_Value.stringValue, out floatValue); floatValue = EditorGUI.FloatField(rect, floatValue); m_Value.stringValue = floatValue.ToString(); } else if (valueType == "enum") { m_Value.stringValue = CustomEnumPopup.Popup(rect, property); } break; case ValueProperty.PropertyType.Disabled: default: break; } }
string TypeGUI(Rect position, SerializedProperty property, ValueProperty.PropertyType propertyType) { float width = (position.width - 40f) * .5f; Rect rect = new Rect(position.x + 40f + width, position.y, width, EditorGUIUtility.singleLineHeight ); SerializedProperty m_ValueType = property.FindPropertyRelative("m_ValueType"); SerializedProperty m_FixedType = property.FindPropertyRelative("m_FixedType"); SerializedProperty m_Target = property.FindPropertyRelative("m_Target"); if (property.FindPropertyRelative("m_FixedType").boolValue) { var validTypeNames = m_Target.FindPropertyRelative("m_ValidTypeNames"); validTypeNames.ClearArray(); validTypeNames.InsertArrayElementAtIndex(0); var element = validTypeNames.GetArrayElementAtIndex(0); element.stringValue = m_ValueType.stringValue; m_Target.FindPropertyRelative("m_EnumType").stringValue = property.FindPropertyRelative("m_EnumType").stringValue; if (property.FindPropertyRelative("m_EnumTypeIsCustomizable").boolValue&& propertyType == ValueProperty.PropertyType.Linked) { validTypeNames.InsertArrayElementAtIndex(1); element = validTypeNames.GetArrayElementAtIndex(1); element.stringValue = typeof(string).ToString(); } } else { var validTypeNames = m_Target.FindPropertyRelative("m_ValidTypeNames"); validTypeNames.ClearArray(); validTypeNames.InsertArrayElementAtIndex(0); var element = validTypeNames.GetArrayElementAtIndex(0); element.stringValue = typeof(int).ToString(); validTypeNames.InsertArrayElementAtIndex(1); element = validTypeNames.GetArrayElementAtIndex(1); element.stringValue = typeof(bool).ToString(); validTypeNames.InsertArrayElementAtIndex(2); element = validTypeNames.GetArrayElementAtIndex(2); element.stringValue = typeof(decimal).ToString(); validTypeNames.InsertArrayElementAtIndex(3); element = validTypeNames.GetArrayElementAtIndex(3); element.stringValue = typeof(double).ToString(); validTypeNames.InsertArrayElementAtIndex(4); element = validTypeNames.GetArrayElementAtIndex(4); element.stringValue = typeof(float).ToString(); validTypeNames.InsertArrayElementAtIndex(5); element = validTypeNames.GetArrayElementAtIndex(5); element.stringValue = typeof(long).ToString(); validTypeNames.InsertArrayElementAtIndex(6); element = validTypeNames.GetArrayElementAtIndex(6); element.stringValue = typeof(short).ToString(); validTypeNames.InsertArrayElementAtIndex(7); element = validTypeNames.GetArrayElementAtIndex(7); element.stringValue = typeof(string).ToString(); if (propertyType == ValueProperty.PropertyType.Linked) { validTypeNames.InsertArrayElementAtIndex(8); element = validTypeNames.GetArrayElementAtIndex(8); element.stringValue = "enum"; } } string selectedType = m_Target.FindPropertyRelative("m_Type").stringValue; EditorGUI.BeginDisabledGroup(propertyType == ValueProperty.PropertyType.Linked || m_FixedType.boolValue); if (!string.IsNullOrEmpty(m_ValueType.stringValue) && CustomEnumPopup.GetEnumType(m_ValueType.stringValue) != null) { property.FindPropertyRelative("m_EnumType").stringValue = m_ValueType.stringValue; m_ValueType.stringValue = "enum"; } List <string> typePopupStrings = new List <string>(); var validTypes = m_Target.FindPropertyRelative("m_ValidTypeNames"); int selectedVal = 0; for (int i = 0; i < validTypes.arraySize; i++) { var t = validTypes.GetArrayElementAtIndex(i); var friendlyString = GetFriendlyStringFromTypeString(t.stringValue); if (!string.IsNullOrEmpty(friendlyString) && !typePopupStrings.Contains(friendlyString)) { typePopupStrings.Add(friendlyString); selectedVal = typePopupStrings.IndexOf(friendlyString); } } if (!string.IsNullOrEmpty(selectedType)) { selectedVal = typePopupStrings.IndexOf(GetFriendlyStringFromTypeString(selectedType)); } var newValIndex = EditorGUI.Popup(rect, selectedVal, typePopupStrings.ToArray()); if (newValIndex < 0 || newValIndex >= typePopupStrings.Count) { newValIndex = typePopupStrings.Count - 1; } if (typePopupStrings.Count > 0) { var newVal = typePopupStrings[newValIndex]; switch (newVal) { case "String": m_ValueType.stringValue = typeof(string).ToString(); break; case "Int": m_ValueType.stringValue = typeof(int).ToString(); break; case "Float": m_ValueType.stringValue = typeof(float).ToString(); break; case "Bool": m_ValueType.stringValue = typeof(bool).ToString(); break; case "Enum": m_ValueType.stringValue = "enum"; break; } } m_Target.FindPropertyRelative("m_Type").stringValue = m_ValueType.stringValue; EditorGUI.EndDisabledGroup(); return(m_ValueType.stringValue); }
void AddMenuItems(GenericMenu menu, Object originalTarget, object target, string[] validTypes, string baseMenuPath = null, int depth = 0) { var menuItemPath = new StringBuilder(); var members = Array.FindAll( target.GetType().GetMembers(), x => (x.GetType().Name == "MonoProperty" || x.GetType().Name == "MonoField") ); for (int i = 0; i < members.Length; i++) { var member = members[i]; var memberType = (member.GetType().Name == "MonoField") ? ((FieldInfo)member).FieldType : ((PropertyInfo)member).PropertyType; menuItemPath.Remove(0, menuItemPath.Length); if (!string.IsNullOrEmpty(baseMenuPath)) { menuItemPath.Append(baseMenuPath); menuItemPath.Append("/"); } menuItemPath.Append(member.Name); if (IsValidType(memberType)) { string typeStr = memberType.ToString(); if (memberType.IsEnum) { typeStr = "enum"; } if (isValidFromTypeArray(typeStr, validTypes)) { var memberPath = menuItemPath.ToString().Replace("/", "."); if (typeStr.Equals("enum")) { if (!string.IsNullOrEmpty(m_Prop.FindPropertyRelative("m_EnumType").stringValue)) { if (memberType.Equals(CustomEnumPopup.GetEnumType(m_Prop.FindPropertyRelative("m_EnumType").stringValue))) { menu.AddItem( new GUIContent(string.Concat(originalTarget.GetType().Name, "/", menuItemPath)), m_TargetProperty.objectReferenceValue == originalTarget && m_PathProperty.stringValue == memberPath, SetProperty, new PropertySetter(m_Prop, originalTarget, memberPath, typeStr) ); } } else { menu.AddItem( new GUIContent(string.Concat(originalTarget.GetType().Name, "/", menuItemPath)), m_TargetProperty.objectReferenceValue == originalTarget && m_PathProperty.stringValue == memberPath, SetPropertyWithEnum, new PropertySetterWithEnum(m_Prop, originalTarget, memberPath, typeStr, memberType.ToString()) ); } } else { menu.AddItem( new GUIContent(string.Concat(originalTarget.GetType().Name, "/", menuItemPath)), m_TargetProperty.objectReferenceValue == originalTarget && m_PathProperty.stringValue == memberPath, SetProperty, new PropertySetter(m_Prop, originalTarget, memberPath, typeStr) ); } } } else if (depth <= k_maxDepth && IsValidSubTarget(target, member)) { var memberValue = GetMemberValue(target, member); if (memberValue != null) { AddMenuItems(menu, originalTarget, memberValue, validTypes, menuItemPath.ToString(), depth + 1); } } } }