public override void DrawProperty(BaseMightyMember mightyMember, SerializedProperty property, BaseDrawerAttribute baseAttribute) { if (property.propertyType != SerializedPropertyType.Float && property.propertyType != SerializedPropertyType.Integer) { EditorDrawUtility.DrawHelpBox($"Field {property.name} is not a number"); return; } var value = property.propertyType == SerializedPropertyType.Integer ? property.intValue : property.floatValue; var valueFormatted = property.propertyType == SerializedPropertyType.Integer ? value.ToString(CultureInfo.InvariantCulture) : $"{value:0.00}"; var progressBarAttribute = (ProgressBarAttribute)baseAttribute; var position = EditorGUILayout.GetControlRect(); var maxValue = progressBarAttribute.MaxValue; var lineHeight = EditorGUIUtility.singleLineHeight; var barPosition = new Rect(position.position.x, position.position.y, position.size.x, lineHeight); var fillPercentage = value / maxValue; var barLabel = $"{(!string.IsNullOrEmpty(progressBarAttribute.Name) ? $"[{progressBarAttribute.Name}] " : "")}{valueFormatted}/{maxValue}"; if (!m_progressBarCache.Contains(mightyMember)) { InitDrawer(mightyMember, baseAttribute); } var color = m_progressBarCache[mightyMember].Value ?? Color.white; var color2 = Color.white; DrawBar(barPosition, Mathf.Clamp01(fillPercentage), barLabel, color, color2, (ProgressBarAttribute)baseAttribute); }
private void PopulateScriptIconFromType(Assembly assembly, Type type, string iconPath, int priority, Dictionary <Type, ScriptIcon> scriptIconByType) { var ignored = type.GetCustomAttribute(typeof(IgnoreScriptIconAttribute), true) != null; if (scriptIconByType.TryGetValue(type, out var scriptIcon) && scriptIcon.priority <= priority) { scriptIcon.ignored = ignored; scriptIcon.SetValid(); return; } if (scriptIcon == null) { scriptIcon = new ScriptIcon(); } scriptIcon.SetAssembly(assembly); scriptIcon.SetType(type); scriptIcon.icon = EditorDrawUtility.GetTexture(iconPath); scriptIcon.priority = priority; scriptIcon.ignored = ignored; scriptIcon.SetValid(); scriptIconByType[type] = scriptIcon; }
public override void EndDraw(BaseMightyMember mightyMember, BaseElementDecoratorAttribute baseAttribute, Action <BaseMightyMember, SerializedProperty, BaseDrawerAttribute> propertyDrawCallback, BaseDrawerAttribute drawerAttribute = null) { var property = mightyMember.Property; if (!property.isArray) { EndDraw(mightyMember, baseAttribute); return; } if (!property.isExpanded) { return; } EditorDrawUtility.DrawArrayBody(property, index => { BeginDrawElement(mightyMember, index, baseAttribute); propertyDrawCallback?.Invoke(mightyMember, property.GetArrayElementAtIndex(index), drawerAttribute); EndDrawElement(mightyMember, index, baseAttribute); }); EditorGUI.indentLevel--; EndDrawArray(mightyMember, baseAttribute); }
private void DrawPreview(SerializedProperty property, ShowAssetPreviewAttribute attribute) { if (property.propertyType == SerializedPropertyType.ObjectReference && property.objectReferenceValue != null) { Texture2D previewTexture = AssetPreview.GetAssetPreview(property.objectReferenceValue); if (previewTexture != null) { var width = Mathf.Clamp(attribute.Size, 0, previewTexture.width); var height = Mathf.Clamp(attribute.Size, 0, previewTexture.height); GUILayout.BeginVertical(); EditorDrawUtility.DrawWithAlign(attribute.Align, () => { if (previewTexture != null) { GUILayout.Label(previewTexture, GUILayout.MaxWidth(width), GUILayout.MaxHeight(height)); } }); GUILayout.EndVertical(); } else { EditorDrawUtility.DrawHelpBox($"{property.name} doesn't have an asset preview"); } } else { EditorDrawUtility.DrawHelpBox($"{property.name} doesn't have an asset preview"); } }
private static void NotString(ValueWrapper wrapper) { string warning = typeof(ResizableTextAreaAttribute).Name + " can only be used on string fields"; EditorDrawUtility.DrawHelpBox(warning, MessageType.Warning); wrapper.DrawDefaultField(); }
/// <inheritdoc /> public override void DrawDefaultField() { var value = this.GetValue(); value = EditorDrawUtility.DrawPropertyField(value, this.Type, this.DisplayName); this.SetValue(value); }
public override void DrawMethod(MightyMember <MethodInfo> mightyMember, BaseMethodAttribute baseAttribute) { var methodInfo = mightyMember.MemberInfo; if (methodInfo.GetParameters().Length == 0) { var buttonAttribute = (ButtonAttribute)baseAttribute; var buttonText = string.IsNullOrEmpty(buttonAttribute.Text) ? methodInfo.Name.DrawPrettyName() : buttonAttribute.Text; var enabled = GUI.enabled; if (!m_buttonCache.Contains(mightyMember)) { InitDrawer(mightyMember, baseAttribute); } GUI.enabled = m_buttonCache[mightyMember].Value && (buttonAttribute.ExecuteInPlayMode || !EditorApplication.isPlaying); if (GUILayout.Button(buttonText, GUILayout.Height(buttonAttribute.Height))) { methodInfo.Invoke(mightyMember.Target, null); } GUI.enabled = enabled; } else { EditorDrawUtility.DrawHelpBox($"{typeof(ButtonAttribute).Name} works only on methods with no parameters"); } }
public void DrawField(string fieldName, Object context, object target) { var field = target.GetField(fieldName); var attribute = field.GetCustomAttribute <EditorSerializeAttribute>(); if (attribute.OldName != null) { EditorFieldsDatabase.RenameField(context, attribute.OldName, field.Name); } var editorField = EditorFieldsDatabase.GetEditorField(context, field.Name); var value = field.GetValue(target); Deserialize(attribute, editorField, target, field, ref value); EditorGUI.BeginChangeCheck(); value = EditorDrawUtility.DrawLayoutField(field, context, target, value, !attribute.Options.Contains(EditorFieldOption.DontFold), attribute.Options.Contains(EditorFieldOption.Asset)); if (EditorGUI.EndChangeCheck()) { Serialize(attribute, editorField, value, field.FieldType); } }
private void OnGUI() { titleContent = new GUIContent(EditorDrawUtility.DrawIcon(IconName.SETTINGS)) { text = " Mighty Settings" }; minSize = new Vector2(300, 120); GUILayout.Space(10); MightySettingsServices.Activated = EditorGUILayout.Toggle("Activated", MightySettingsServices.Activated); GUILayout.Space(10); MightySettingsServices.AutoValuesOnPlay = EditorGUILayout.Toggle("Auto Values On Play", MightySettingsServices.AutoValuesOnPlay); GUILayout.Space(10); MightySettingsServices.AutoValuesOnPlay = EditorGUILayout.Toggle("Auto Values On Build", MightySettingsServices.AutoValuesOnBuild); GUILayout.Space(10); if (GUILayout.Button("Apply Auto Values")) { MightyAutoValues.ApplyAutoValuesAsync(); } }
public void DrawLayerField(Rect rect, SerializedProperty property, BaseDrawerAttribute attribute, GUIContent label = null) { if (property.propertyType != SerializedPropertyType.Integer) { EditorDrawUtility.DrawPropertyField(rect, property); rect.y += 18; rect.height -= 24; EditorDrawUtility.DrawHelpBox(rect, $"\"{property.displayName}\" should be of type int"); return; } int layer; if (attribute.Option.Contains(FieldOption.HideLabel)) { layer = EditorGUI.LayerField(rect, property.intValue); } else if (attribute.Option.Contains(FieldOption.BoldLabel)) { layer = EditorGUI.LayerField(rect, label ?? EditorGUIUtility.TrTextContent(property.displayName), property.intValue, EditorStyles.boldLabel); } else { layer = EditorGUI.LayerField(rect, label ?? EditorGUIUtility.TrTextContent(property.displayName), property.intValue); } property.intValue = layer; }
private static void ValidateProperty(ValueWrapper wrapper, ValidateInputAttribute attribute) { var validationCallback = ReflectionUtility.GetMethod(wrapper.Target, attribute.CallbackName); if (validationCallback == null || validationCallback.ReturnType != typeof(bool) || validationCallback.GetParameters().Length != 1) { WrongType(); return; } var fieldType = wrapper.Type; Type parameterType = validationCallback.GetParameters()[0].ParameterType; if (fieldType != parameterType) { FieldMismatch(); return; } if (!(bool)validationCallback.Invoke(wrapper.Target, new[] { wrapper.GetValue() })) { if (string.IsNullOrEmpty(attribute.Message)) { EditorDrawUtility.DrawHelpBox(wrapper.DisplayName + " is not valid", MessageType.Error); } else { EditorDrawUtility.DrawHelpBox(attribute.Message, MessageType.Error); } } }
public override void BeginDraw(BaseMightyMember mightyMember, BaseElementDecoratorAttribute baseAttribute, Action <BaseMightyMember, SerializedProperty, BaseDrawerAttribute> propertyDrawCallback, BaseDrawerAttribute drawerAttribute = null) { var property = mightyMember.Property; if (!property.isArray) { BeginDraw(mightyMember, baseAttribute); propertyDrawCallback?.Invoke(mightyMember, property, drawerAttribute); return; } BeginDrawArray(mightyMember, baseAttribute); BeginDrawHeader(mightyMember, baseAttribute); if (!EditorDrawUtility.DrawFoldout(property)) { EndDrawHeader(mightyMember, baseAttribute); EndDrawArray(mightyMember, baseAttribute); return; } EditorGUI.indentLevel++; EditorDrawUtility.DrawArraySizeField(property); EndDrawHeader(mightyMember, baseAttribute); }
public void Serialize(object value, Type type) { var wrapper = type.GetWrapperForType(); if (wrapper != null) { // if (type.IsEnum) // value = Enum.ToObject(type, value); Serialize(m_path, m_fileName, wrapper, value); return; } if (type.GetCustomAttribute(typeof(SerializableAttribute), true) == null) { return; } wrapper = typeof(bool).GetWrapperForType(); wrapper.SetValue(EditorDrawUtility.GetFoldout(m_fileName)); WriteFile(m_path, wrapper); foreach (var field in type.GetSerializableFields()) { EditorFieldsDatabase.GetEditorField($"{m_fileName}.{field.Name}").Serialize(field.GetValue(value), field.FieldType); } }
private static void NotObject(ValueWrapper wrapper) { string warning = typeof(ShowAssetPreviewAttribute).Name + " can only be used on Object fields"; EditorDrawUtility.DrawHelpBox(warning, MessageType.Warning); wrapper.DrawDefaultField(); }
public override void ValidateProperty(BaseMightyMember mightyMember, BaseValidatorAttribute baseAttribute) { var property = mightyMember.Property; var requiredAttribute = (RequiredAttribute)baseAttribute; if (property.propertyType == SerializedPropertyType.ObjectReference) { if (property.objectReferenceValue != null) { return; } var errorMessage = $"{property.name} is required"; if (!string.IsNullOrEmpty(requiredAttribute.Message)) { errorMessage = requiredAttribute.Message; } EditorDrawUtility.DrawHelpBox(errorMessage, MessageType.Error, property.GetTargetObject(), true); } else { EditorDrawUtility.DrawHelpBox($"{typeof(RequiredAttribute).Name} works only on reference types"); } }
private static void NotIntFloat(ValueWrapper wrapper) { string warning = typeof(SliderAttribute).Name + " can only be used on int or float fields"; EditorDrawUtility.DrawHelpBox(warning, MessageType.Warning); wrapper.DrawDefaultField(); }
/// <inheritdoc /> protected override bool CanDrawProperty(AttributeWrapper wrapper, HideIfAttribute attribute) { var target = wrapper.Target; FieldInfo conditionField = ReflectionUtility.GetField(target, attribute.ConditionName); if (conditionField != null && conditionField.FieldType == typeof(bool)) { return(!(bool)conditionField.GetValue(target)); } MethodInfo conditionMethod = ReflectionUtility.GetMethod(target, attribute.ConditionName); if (conditionMethod != null && conditionMethod.ReturnType == typeof(bool) && conditionMethod.GetParameters().Length == 0) { return(!(bool)conditionMethod.Invoke(target, null)); } string warning = attribute.GetType().Name + " needs a valid boolean condition field or method name to work"; EditorDrawUtility.DrawHelpBox(warning, MessageType.Warning); return(true); }
public override void ValidateProperty(BaseMightyMember mightyMember, BaseValidatorAttribute baseAttribute) { var property = mightyMember.Property; var maxValueAttribute = (MaxValueAttribute)baseAttribute; switch (property.propertyType) { case SerializedPropertyType.Integer: if (property.intValue > maxValueAttribute.MaxValue) { property.intValue = (int)maxValueAttribute.MaxValue; } break; case SerializedPropertyType.Float: if (property.floatValue > maxValueAttribute.MaxValue) { property.floatValue = maxValueAttribute.MaxValue; } break; default: EditorDrawUtility.DrawHelpBox($"{typeof(MaxValueAttribute).Name} can be used only on int or float fields"); break; } }
public static void DrawLine(ColorValue color = ColorValue.Grey) { var previousColor = GUI.color; GUI.color = EditorDrawUtility.GetColor(color); GUILayout.Box(GUIContent.none, GUIStyleUtility.HorizontalLine(false)); GUI.color = previousColor; }
public static void OnReloadScript() { foreach (var drawer in SpecialDrawersDatabase.GetChildrenDrawers <BaseReloadScriptDrawer>()) { drawer.OnReloadScript(); } EditorDrawUtility.MightyDebug("Apply Script Reload"); }
private object InvokeDrawer(MightyMethod <object> drawerMethod, string signature, params object[] parameters) { if (drawerMethod != null) { return(drawerMethod.Invoke(parameters)); } EditorDrawUtility.DrawHelpBox($"Callback is invalid, it should be like this: \"{signature}\""); return(null); }
public override void InitDrawer(BaseMightyMember mightyMember, BaseMightyAttribute mightyAttribute) { var attribute = (ProgressBarAttribute)mightyAttribute; var target = mightyMember.InitAttributeTarget <ProgressBarAttribute>(); var colorInfo = EditorDrawUtility.GetColorInfo(mightyMember.Property, target, attribute.ColorName, attribute.Color); m_progressBarCache[mightyMember] = colorInfo; }
public override void InitDrawer(BaseMightyMember mightyMember, BaseMightyAttribute mightyAttribute) { var attribute = (StyleAttribute)mightyAttribute; var target = mightyMember.InitAttributeTarget <StyleAttribute>(); var styleInfo = EditorDrawUtility.GetStyleInfo(mightyMember.Property, target, attribute.StyleName, attribute.EditorStyle); m_styleCache[mightyMember] = styleInfo; }
private void DrawArea(Rect rect, SerializedProperty property, BaseDrawerAttribute attribute) { if (property.propertyType != SerializedPropertyType.Vector4) { EditorDrawUtility.DrawHelpBox(rect, $"{property.name} should be of type UnityEngine.Vector4"); return; } property.vector4Value = DrawArea(rect, EditorGUIUtility.TrTextContent(property.displayName), property.vector4Value, attribute); }
public override void DrawProperty(BaseMightyMember mightyMember, SerializedProperty property, BaseDrawerAttribute baseAttribute) { if (property.isArray && property.propertyType != SerializedPropertyType.String) { EditorDrawUtility.DrawArray(property, index => DrawElement(mightyMember, index, baseAttribute)); return; } DrawTextArea(property, (ResizableTextAreaAttribute)baseAttribute); }
public override void DrawProperty(BaseMightyMember mightyMember, SerializedProperty property, BaseDrawerAttribute baseAttribute) { if (property.isArray) { EditorDrawUtility.DrawArray(property, index => DrawElement(mightyMember, index, baseAttribute)); return; } DrawRotation2D(property, baseAttribute); }
private static void DrawRequiredBox(SerializedPropertyAttributeWrapper wrapper, RequiredAttribute attribute) { string errorMessage = wrapper.DisplayName + " is required"; if (!string.IsNullOrEmpty(attribute.Message)) { errorMessage = attribute.Message; } EditorDrawUtility.DrawHelpBox(errorMessage, MessageType.Error); }
private void DrawMargins(SerializedProperty property, BaseDrawerAttribute attribute, GUIContent label = null) { if (property.propertyType != SerializedPropertyType.Vector4) { EditorDrawUtility.DrawHelpBox($"{property.name} should be of type UnityEngine.Vector4"); return; } property.vector4Value = DrawMargins(EditorGUILayout.GetControlRect(true, 32), label ?? EditorGUIUtility.TrTextContent(property.displayName), property.vector4Value, attribute); }
private void DrawSlider(SerializedProperty property, MinMaxSliderAttribute attribute, GUIContent label = null) { if (property.propertyType == SerializedPropertyType.Vector2) { Draw(EditorGUILayout.GetControlRect(), property, attribute, label); } else { EditorDrawUtility.DrawPropertyField(property, label); EditorDrawUtility.DrawHelpBox($"{typeof(MinMaxSliderAttribute).Name} can be used only on Vector2 fields"); } }
public static void ApplyAutoValues() { foreach (var script in SerializedPropertyUtility.FindAllObjects <MonoBehaviour>()) { if (script.CreateEditor(out var mightyEditor)) { mightyEditor.ApplyAutoValues(); } } EditorDrawUtility.MightyDebug("Auto Values Applied"); }