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); } } } }