浏览代码

Updated NaughtyAttributes / Fixed File names in Game Save Manager

/main
Thomas ICHÉ 6 年前
当前提交
5c52b390
共有 25 个文件被更改,包括 179 次插入137 次删除
  1. 13
      NaughtyAttributes/Core/DrawConditionAttributes/HideIfAttribute.cs
  2. 15
      NaughtyAttributes/Core/DrawConditionAttributes/ShowIfAttribute.cs
  3. 13
      NaughtyAttributes/Core/DrawerAttributes/DisableIfAttribute.cs
  4. 15
      NaughtyAttributes/Core/DrawerAttributes/EnableIfAttribute.cs
  5. 4
      NaughtyAttributes/Editor/FieldDrawers/ShowNonSerializedFieldFieldDrawer.cs
  6. 2
      NaughtyAttributes/Editor/MethodDrawers/ButtonMethodDrawer.cs
  7. 4
      NaughtyAttributes/Editor/NativePropertyDrawers/ShowNativePropertyNativePropertyDrawer.cs
  8. 28
      NaughtyAttributes/Editor/PropertyDrawConditions/HideIfPropertyDrawCondition.cs
  9. 61
      NaughtyAttributes/Editor/PropertyDrawConditions/ShowIfPropertyDrawCondition.cs
  10. 41
      NaughtyAttributes/Editor/PropertyDrawers/DisableIfPropertyDrawer.cs
  11. 61
      NaughtyAttributes/Editor/PropertyDrawers/EnableIfPropertyDrawer.cs
  12. 2
      NaughtyAttributes/Editor/PropertyDrawers/MinMaxSliderPropertyDrawer.cs
  13. 2
      NaughtyAttributes/Editor/PropertyDrawers/ReorderableListPropertyDrawer.cs
  14. 2
      NaughtyAttributes/Editor/PropertyDrawers/ResizableTextAreaPropertyDrawer.cs
  15. 4
      NaughtyAttributes/Editor/PropertyDrawers/ShowAssetPreviewPropertyDrawer.cs
  16. 2
      NaughtyAttributes/Editor/PropertyDrawers/SliderPropertyDrawer.cs
  17. 2
      NaughtyAttributes/Editor/PropertyMetas/InfoBoxPropertyMeta.cs
  18. 2
      NaughtyAttributes/Editor/PropertyValidators/MaxValuePropertyValidator.cs
  19. 2
      NaughtyAttributes/Editor/PropertyValidators/MinValuePropertyValidator.cs
  20. 4
      NaughtyAttributes/Editor/PropertyValidators/RequiredPropertyValidator.cs
  21. 8
      NaughtyAttributes/Editor/PropertyValidators/ValidateInputPropertyValidator.cs
  22. 2
      NaughtyAttributes/Editor/Utility/EditorDrawUtility.cs
  23. 6
      Runtime/Managers/Implementations/GameSaveManager.cs
  24. 10
      NaughtyAttributes/Core/ConditionOperator.cs
  25. 11
      NaughtyAttributes/Core/ConditionOperator.cs.meta

13
NaughtyAttributes/Core/DrawConditionAttributes/HideIfAttribute.cs


namespace NaughtyAttributes
{
[AttributeUsage(AttributeTargets.Field, AllowMultiple = false, Inherited = true)]
public class HideIfAttribute : DrawConditionAttribute
public class HideIfAttribute : ShowIfAttribute
public string ConditionName { get; private set; }
public HideIfAttribute(string condition)
: base(condition)
{
Reversed = true;
}
public HideIfAttribute(string conditionName)
public HideIfAttribute(ConditionOperator conditionOperator, params string[] conditions)
: base(conditionOperator, conditions)
this.ConditionName = conditionName;
Reversed = true;
}
}
}

15
NaughtyAttributes/Core/DrawConditionAttributes/ShowIfAttribute.cs


[AttributeUsage(AttributeTargets.Field, AllowMultiple = false, Inherited = true)]
public class ShowIfAttribute : DrawConditionAttribute
{
public string ConditionName { get; private set; }
public string[] Conditions { get; private set; }
public ConditionOperator ConditionOperator { get; private set; }
public bool Reversed { get; protected set; }
public ShowIfAttribute(string condition)
{
ConditionOperator = ConditionOperator.And;
Conditions = new string[1] { condition };
}
public ShowIfAttribute(string conditionName)
public ShowIfAttribute(ConditionOperator conditionOperator, params string[] conditions)
this.ConditionName = conditionName;
ConditionOperator = conditionOperator;
Conditions = conditions;
}
}
}

13
NaughtyAttributes/Core/DrawerAttributes/DisableIfAttribute.cs


namespace NaughtyAttributes
{
[AttributeUsage(AttributeTargets.Field, AllowMultiple = false, Inherited = true)]
public class DisableIfAttribute : DrawerAttribute
public class DisableIfAttribute : EnableIfAttribute
public string ConditionName { get; private set; }
public DisableIfAttribute(string condition)
: base(condition)
{
Reversed = true;
}
public DisableIfAttribute(string conditionName)
public DisableIfAttribute(ConditionOperator conditionOperator, params string[] conditions)
: base(conditionOperator, conditions)
this.ConditionName = conditionName;
Reversed = true;
}
}
}

15
NaughtyAttributes/Core/DrawerAttributes/EnableIfAttribute.cs


[AttributeUsage(AttributeTargets.Field, AllowMultiple = false, Inherited = true)]
public class EnableIfAttribute : DrawerAttribute
{
public string ConditionName { get; private set; }
public string[] Conditions { get; private set; }
public ConditionOperator ConditionOperator { get; private set; }
public bool Reversed { get; protected set; }
public EnableIfAttribute(string condition)
{
ConditionOperator = ConditionOperator.And;
Conditions = new string[1] { condition };
}
public EnableIfAttribute(string conditionName)
public EnableIfAttribute(ConditionOperator conditionOperator, params string[] conditions)
this.ConditionName = conditionName;
ConditionOperator = conditionOperator;
Conditions = conditions;
}
}
}

4
NaughtyAttributes/Editor/FieldDrawers/ShowNonSerializedFieldFieldDrawer.cs


if (value == null)
{
string warning = string.Format("{0} doesn't support {1} types", typeof(ShowNonSerializedFieldFieldDrawer).Name, "Reference");
EditorDrawUtility.DrawHelpBox(warning, MessageType.Warning, logToConsole: true, context: target);
EditorDrawUtility.DrawHelpBox(warning, MessageType.Warning, context: target);
EditorDrawUtility.DrawHelpBox(warning, MessageType.Warning, logToConsole: true, context: target);
EditorDrawUtility.DrawHelpBox(warning, MessageType.Warning, context: target);
}
}
}

2
NaughtyAttributes/Editor/MethodDrawers/ButtonMethodDrawer.cs


else
{
string warning = typeof(ButtonAttribute).Name + " works only on methods with no parameters";
EditorDrawUtility.DrawHelpBox(warning, MessageType.Warning, logToConsole: true, context: target);
EditorDrawUtility.DrawHelpBox(warning, MessageType.Warning, context: target);
}
}
}

4
NaughtyAttributes/Editor/NativePropertyDrawers/ShowNativePropertyNativePropertyDrawer.cs


if (value == null)
{
string warning = string.Format("{0} doesn't support {1} types", typeof(ShowNativePropertyNativePropertyDrawer).Name, "Reference");
EditorDrawUtility.DrawHelpBox(warning, MessageType.Warning, logToConsole: true, context: target);
EditorDrawUtility.DrawHelpBox(warning, MessageType.Warning, context: target);
EditorDrawUtility.DrawHelpBox(warning, MessageType.Warning, logToConsole: true, context: target);
EditorDrawUtility.DrawHelpBox(warning, MessageType.Warning, context: target);
}
}
}

28
NaughtyAttributes/Editor/PropertyDrawConditions/HideIfPropertyDrawCondition.cs


using System.Reflection;
public class HideIfPropertyDrawCondition : PropertyDrawCondition
public class HideIfPropertyDrawCondition : ShowIfPropertyDrawCondition
public override bool CanDrawProperty(SerializedProperty property)
{
HideIfAttribute hideIfAttribute = PropertyUtility.GetAttribute<HideIfAttribute>(property);
UnityEngine.Object target = PropertyUtility.GetTargetObject(property);
FieldInfo conditionField = ReflectionUtility.GetField(target, hideIfAttribute.ConditionName);
if (conditionField != null &&
conditionField.FieldType == typeof(bool))
{
return !(bool)conditionField.GetValue(target);
}
MethodInfo conditionMethod = ReflectionUtility.GetMethod(target, hideIfAttribute.ConditionName);
if (conditionMethod != null &&
conditionMethod.ReturnType == typeof(bool) &&
conditionMethod.GetParameters().Length == 0)
{
return !(bool)conditionMethod.Invoke(target, null);
}
string warning = hideIfAttribute.GetType().Name + " needs a valid boolean condition field or method name to work";
EditorDrawUtility.DrawHelpBox(warning, MessageType.Warning, logToConsole: true, context: target);
return true;
}
}
}

61
NaughtyAttributes/Editor/PropertyDrawConditions/ShowIfPropertyDrawCondition.cs


using System.Collections.Generic;
using System.Reflection;
using UnityEditor;

ShowIfAttribute showIfAttribute = PropertyUtility.GetAttribute<ShowIfAttribute>(property);
UnityEngine.Object target = PropertyUtility.GetTargetObject(property);
FieldInfo conditionField = ReflectionUtility.GetField(target, showIfAttribute.ConditionName);
if (conditionField != null &&
conditionField.FieldType == typeof(bool))
List<bool> conditionValues = new List<bool>();
foreach (var condition in showIfAttribute.Conditions)
return (bool)conditionField.GetValue(target);
FieldInfo conditionField = ReflectionUtility.GetField(target, condition);
if (conditionField != null &&
conditionField.FieldType == typeof(bool))
{
conditionValues.Add((bool)conditionField.GetValue(target));
}
MethodInfo conditionMethod = ReflectionUtility.GetMethod(target, condition);
if (conditionMethod != null &&
conditionMethod.ReturnType == typeof(bool) &&
conditionMethod.GetParameters().Length == 0)
{
conditionValues.Add((bool)conditionMethod.Invoke(target, null));
}
MethodInfo conditionMethod = ReflectionUtility.GetMethod(target, showIfAttribute.ConditionName);
if (conditionMethod != null &&
conditionMethod.ReturnType == typeof(bool) &&
conditionMethod.GetParameters().Length == 0)
if (conditionValues.Count > 0)
return (bool)conditionMethod.Invoke(target, null);
}
bool draw;
if (showIfAttribute.ConditionOperator == ConditionOperator.And)
{
draw = true;
foreach (var value in conditionValues)
{
draw = draw && value;
}
}
else
{
draw = false;
foreach (var value in conditionValues)
{
draw = draw || value;
}
}
string warning = showIfAttribute.GetType().Name + " needs a valid boolean condition field or method name to work";
EditorDrawUtility.DrawHelpBox(warning, MessageType.Warning, logToConsole: true, context: target);
if (showIfAttribute.Reversed)
{
draw = !draw;
}
return draw;
}
else
{
string warning = showIfAttribute.GetType().Name + " needs a valid boolean condition field or method name to work";
EditorDrawUtility.DrawHelpBox(warning, MessageType.Warning, context: target);
return true;
return true;
}
}
}
}

41
NaughtyAttributes/Editor/PropertyDrawers/DisableIfPropertyDrawer.cs


using System.Reflection;
using UnityEngine;
public class DisableIfPropertyDrawer : PropertyDrawer
public class DisableIfPropertyDrawer : EnableIfPropertyDrawer
public override void DrawProperty(SerializedProperty property)
{
bool drawDisabled = false;
bool validCondition = false;
DisableIfAttribute disableIfAttribute = PropertyUtility.GetAttribute<DisableIfAttribute>(property);
UnityEngine.Object target = PropertyUtility.GetTargetObject(property);
FieldInfo conditionField = ReflectionUtility.GetField(target, disableIfAttribute.ConditionName);
if (conditionField != null &&
conditionField.FieldType == typeof(bool))
{
drawDisabled = (bool)conditionField.GetValue(target);
validCondition = true;
}
MethodInfo conditionMethod = ReflectionUtility.GetMethod(target, disableIfAttribute.ConditionName);
if (conditionMethod != null &&
conditionMethod.ReturnType == typeof(bool) &&
conditionMethod.GetParameters().Length == 0)
{
drawDisabled = (bool)conditionMethod.Invoke(target, null);
validCondition = true;
}
if (validCondition)
{
GUI.enabled = !drawDisabled;
EditorDrawUtility.DrawPropertyField(property);
GUI.enabled = true;
}
else
{
string warning = disableIfAttribute.GetType().Name + " needs a valid boolean condition field or method name to work";
EditorDrawUtility.DrawHelpBox(warning, MessageType.Warning, logToConsole: true, context: target);
}
}
}
}

61
NaughtyAttributes/Editor/PropertyDrawers/EnableIfPropertyDrawer.cs


using System.Collections.Generic;
using System.Reflection;
using UnityEditor;
using UnityEngine;

{
public override void DrawProperty(SerializedProperty property)
{
bool drawEnabled = false;
bool validCondition = false;
FieldInfo conditionField = ReflectionUtility.GetField(target, enableIfAttribute.ConditionName);
if (conditionField != null &&
conditionField.FieldType == typeof(bool))
List<bool> conditionValues = new List<bool>();
foreach (var condition in enableIfAttribute.Conditions)
drawEnabled = (bool)conditionField.GetValue(target);
validCondition = true;
FieldInfo conditionField = ReflectionUtility.GetField(target, condition);
if (conditionField != null &&
conditionField.FieldType == typeof(bool))
{
conditionValues.Add((bool)conditionField.GetValue(target));
}
MethodInfo conditionMethod = ReflectionUtility.GetMethod(target, condition);
if (conditionMethod != null &&
conditionMethod.ReturnType == typeof(bool) &&
conditionMethod.GetParameters().Length == 0)
{
conditionValues.Add((bool)conditionMethod.Invoke(target, null));
}
MethodInfo conditionMethod = ReflectionUtility.GetMethod(target, enableIfAttribute.ConditionName);
if (conditionMethod != null &&
conditionMethod.ReturnType == typeof(bool) &&
conditionMethod.GetParameters().Length == 0)
if (conditionValues.Count > 0)
drawEnabled = (bool)conditionMethod.Invoke(target, null);
validCondition = true;
}
bool enabled;
if (enableIfAttribute.ConditionOperator == ConditionOperator.And)
{
enabled = true;
foreach (var value in conditionValues)
{
enabled = enabled && value;
}
}
else
{
enabled = false;
foreach (var value in conditionValues)
{
enabled = enabled || value;
}
}
if (validCondition)
{
GUI.enabled = drawEnabled;
if (enableIfAttribute.Reversed)
{
enabled = !enabled;
}
GUI.enabled = enabled;
EditorDrawUtility.DrawPropertyField(property);
GUI.enabled = true;
}

EditorDrawUtility.DrawHelpBox(warning, MessageType.Warning, logToConsole: true, context: target);
EditorDrawUtility.DrawHelpBox(warning, MessageType.Warning, context: target);
}
}
}

2
NaughtyAttributes/Editor/PropertyDrawers/MinMaxSliderPropertyDrawer.cs


else
{
string warning = minMaxSliderAttribute.GetType().Name + " can be used only on Vector2 fields";
EditorDrawUtility.DrawHelpBox(warning, MessageType.Warning, logToConsole: true, context: PropertyUtility.GetTargetObject(property));
EditorDrawUtility.DrawHelpBox(warning, MessageType.Warning, context: PropertyUtility.GetTargetObject(property));
EditorDrawUtility.DrawPropertyField(property);
}

2
NaughtyAttributes/Editor/PropertyDrawers/ReorderableListPropertyDrawer.cs


else
{
string warning = typeof(ReorderableListAttribute).Name + " can be used only on arrays or lists";
EditorDrawUtility.DrawHelpBox(warning, MessageType.Warning, logToConsole: true, context: PropertyUtility.GetTargetObject(property));
EditorDrawUtility.DrawHelpBox(warning, MessageType.Warning, context: PropertyUtility.GetTargetObject(property));
EditorDrawUtility.DrawPropertyField(property);
}

2
NaughtyAttributes/Editor/PropertyDrawers/ResizableTextAreaPropertyDrawer.cs


else
{
string warning = PropertyUtility.GetAttribute<ResizableTextAreaAttribute>(property).GetType().Name + " can only be used on string fields";
EditorDrawUtility.DrawHelpBox(warning, MessageType.Warning, logToConsole: true, context: PropertyUtility.GetTargetObject(property));
EditorDrawUtility.DrawHelpBox(warning, MessageType.Warning, context: PropertyUtility.GetTargetObject(property));
EditorDrawUtility.DrawPropertyField(property);
}

4
NaughtyAttributes/Editor/PropertyDrawers/ShowAssetPreviewPropertyDrawer.cs


else
{
string warning = property.name + " doesn't have an asset preview";
EditorDrawUtility.DrawHelpBox(warning, MessageType.Warning, logToConsole: true, context: PropertyUtility.GetTargetObject(property));
EditorDrawUtility.DrawHelpBox(warning, MessageType.Warning, context: PropertyUtility.GetTargetObject(property));
}
}
}

EditorDrawUtility.DrawHelpBox(warning, MessageType.Warning, logToConsole: true, context: PropertyUtility.GetTargetObject(property));
EditorDrawUtility.DrawHelpBox(warning, MessageType.Warning, context: PropertyUtility.GetTargetObject(property));
}
}
}

2
NaughtyAttributes/Editor/PropertyDrawers/SliderPropertyDrawer.cs


else
{
string warning = sliderAttribute.GetType().Name + " can be used only on int or float fields";
EditorDrawUtility.DrawHelpBox(warning, MessageType.Warning, logToConsole: true, context: PropertyUtility.GetTargetObject(property));
EditorDrawUtility.DrawHelpBox(warning, MessageType.Warning, context: PropertyUtility.GetTargetObject(property));
EditorDrawUtility.DrawPropertyField(property);
}

2
NaughtyAttributes/Editor/PropertyMetas/InfoBoxPropertyMeta.cs


}
string warning = infoBoxAttribute.GetType().Name + " needs a valid boolean condition field or method name to work";
EditorDrawUtility.DrawHelpBox(warning, MessageType.Warning, logToConsole: true, context: PropertyUtility.GetTargetObject(property));
EditorDrawUtility.DrawHelpBox(warning, MessageType.Warning, context: PropertyUtility.GetTargetObject(property));
}
else
{

2
NaughtyAttributes/Editor/PropertyValidators/MaxValuePropertyValidator.cs


else
{
string warning = maxValueAttribute.GetType().Name + " can be used only on int or float fields";
EditorDrawUtility.DrawHelpBox(warning, MessageType.Warning, logToConsole: true, context: PropertyUtility.GetTargetObject(property));
EditorDrawUtility.DrawHelpBox(warning, MessageType.Warning, context: PropertyUtility.GetTargetObject(property));
}
}
}

2
NaughtyAttributes/Editor/PropertyValidators/MinValuePropertyValidator.cs


else
{
string warning = minValueAttribute.GetType().Name + " can be used only on int or float fields";
EditorDrawUtility.DrawHelpBox(warning, MessageType.Warning, logToConsole: true, context: PropertyUtility.GetTargetObject(property));
EditorDrawUtility.DrawHelpBox(warning, MessageType.Warning, context: PropertyUtility.GetTargetObject(property));
}
}
}

4
NaughtyAttributes/Editor/PropertyValidators/RequiredPropertyValidator.cs


errorMessage = requiredAttribute.Message;
}
EditorDrawUtility.DrawHelpBox(errorMessage, MessageType.Error, logToConsole: true, context: PropertyUtility.GetTargetObject(property));
EditorDrawUtility.DrawHelpBox(errorMessage, MessageType.Error, context: PropertyUtility.GetTargetObject(property));
EditorDrawUtility.DrawHelpBox(warning, MessageType.Warning, logToConsole: true, context: PropertyUtility.GetTargetObject(property));
EditorDrawUtility.DrawHelpBox(warning, MessageType.Warning, context: PropertyUtility.GetTargetObject(property));
}
}
}

8
NaughtyAttributes/Editor/PropertyValidators/ValidateInputPropertyValidator.cs


{
if (string.IsNullOrEmpty(validateInputAttribute.Message))
{
EditorDrawUtility.DrawHelpBox(property.name + " is not valid", MessageType.Error, logToConsole: true, context: target);
EditorDrawUtility.DrawHelpBox(property.name + " is not valid", MessageType.Error, context: target);
EditorDrawUtility.DrawHelpBox(validateInputAttribute.Message, MessageType.Error, logToConsole: true, context: target);
EditorDrawUtility.DrawHelpBox(validateInputAttribute.Message, MessageType.Error, context: target);
}
}
}

EditorDrawUtility.DrawHelpBox(warning, MessageType.Warning, logToConsole: true, context: target);
EditorDrawUtility.DrawHelpBox(warning, MessageType.Warning, context: target);
}
}
else

" needs a callback with boolean return type and a single parameter of the same type as the field";
EditorDrawUtility.DrawHelpBox(warning, MessageType.Warning, logToConsole: true, context: target);
EditorDrawUtility.DrawHelpBox(warning, MessageType.Warning, context: target);
}
}
}

2
NaughtyAttributes/Editor/Utility/EditorDrawUtility.cs


return false;
}
public static void DrawHelpBox(string message, MessageType type, bool logToConsole = false, UnityEngine.Object context = null)
public static void DrawHelpBox(string message, MessageType type, UnityEngine.Object context = null, bool logToConsole = true)
{
EditorGUILayout.HelpBox(message, type);

6
Runtime/Managers/Implementations/GameSaveManager.cs


Dictionary<string, object> LoadFile(string fileName)
{
if(!System.IO.File.Exists(Application.dataPath+fileName))
if(!File.Exists(Application.dataPath + "/../" + fileName))
{
SaveFile(fileName, new Dictionary<string, object>());
}

string contents= File.ReadAllText(Application.dataPath + fileName);
string contents= File.ReadAllText(Application.dataPath + "/../" + fileName);
SerializableOutput data = JsonUtility.FromJson<SerializableOutput>(contents);

i++;
}
File.WriteAllText(Application.dataPath+filename, JsonUtility.ToJson(data));
File.WriteAllText(Application.dataPath + "/../" + filename, JsonUtility.ToJson(data));
}
[System.Serializable]

10
NaughtyAttributes/Core/ConditionOperator.cs


using System;
namespace NaughtyAttributes
{
public enum ConditionOperator
{
And,
Or
}
}

11
NaughtyAttributes/Core/ConditionOperator.cs.meta


fileFormatVersion: 2
guid: 8da69fd2354ea264bb1c54362ee02938
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
正在加载...
取消
保存