added serialzed dictionary

This commit is contained in:
Sebastian Bularca
2026-03-19 22:20:34 +01:00
parent fedd1961a0
commit 9ec4c75ce2
129 changed files with 4088 additions and 0 deletions

View File

@@ -0,0 +1,23 @@
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace AYellowpaper.SerializedCollections.Editor
{
public struct GUIEnabledScope : IDisposable
{
public readonly bool PreviouslyEnabled;
public GUIEnabledScope(bool enabled)
{
PreviouslyEnabled = GUI.enabled;
GUI.enabled = enabled;
}
public void Dispose()
{
GUI.enabled = PreviouslyEnabled;
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: c671c12e9de22d042be004c62b6f4158
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,24 @@
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
namespace AYellowpaper.SerializedCollections.Editor
{
public struct LabelWidth : IDisposable
{
public float PreviousWidth { get; }
public LabelWidth(float width)
{
PreviousWidth = EditorGUIUtility.labelWidth;
EditorGUIUtility.labelWidth = width;
}
public void Dispose()
{
EditorGUIUtility.labelWidth = PreviousWidth;
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: fd75bc249daa39f4e832a3dab9c23c82
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,41 @@
using UnityEngine;
namespace AYellowpaper.SerializedCollections.Editor
{
public static class RectUtility
{
public static Rect WithX(this Rect rect, float x) => new Rect(x, rect.y, rect.width, rect.height);
public static Rect WithY(this Rect rect, float y) => new Rect(rect.x, y, rect.width, rect.height);
public static Rect WithWidth(this Rect rect, float width) => new Rect(rect.x, rect.y, width, rect.height);
public static Rect WithHeight(this Rect rect, float height) => new Rect(rect.x, rect.y, rect.width, height);
public static Rect WithPosition(this Rect rect, Vector2 position) => new Rect(position, rect.size);
public static Rect WithPosition(this Rect rect, float x, float y) => new Rect(new Vector2(x, y), rect.size);
public static Rect WithSize(this Rect rect, Vector2 size) => new Rect(rect.position, size);
public static Rect WithSize(this Rect rect, float width, float height) => new Rect(rect.position, new Vector2(width, height));
public static Rect WithXAndWidth(this Rect rect, float x, float width) => new Rect(x, rect.y, width, rect.height);
public static Rect WithYAndHeight(this Rect rect, float y, float height) => new Rect(rect.x, y, rect.width, height);
public static Rect AppendRight(this Rect rect, float width) => new Rect(rect.x + rect.width, rect.y, width, rect.height);
public static Rect AppendRight(this Rect rect, float width, float space) => new Rect(rect.x + rect.width + space, rect.y, width, rect.height);
public static Rect AppendLeft(this Rect rect, float width) => new Rect(rect.x - width, rect.y, width, rect.height);
public static Rect AppendLeft(this Rect rect, float width, float space) => new Rect(rect.x - space - width, rect.y, width, rect.height);
public static Rect AppendUp(this Rect rect, float height) => new Rect(rect.x, rect.y - height, rect.width, height);
public static Rect AppendUp(this Rect rect, float height, float space) => new Rect(rect.x, rect.y - space - height, rect.width, height);
public static Rect AppendDown(this Rect rect, float height) => new Rect(rect.x, rect.y + rect.height, rect.width, height);
public static Rect AppendDown(this Rect rect, float height, float space) => new Rect(rect.x, rect.y + rect.height + space, rect.width, height);
public static Rect CutLeft(this Rect rect, float width) => new Rect(rect.x + width, rect.y, rect.width - width, rect.height);
public static Rect CutRight(this Rect rect, float width) => new Rect(rect.x, rect.y, rect.width - width, rect.height);
public static Rect CutTop(this Rect rect, float height) => new Rect(rect.x, rect.y + height, rect.width, rect.height - height);
public static Rect CutBottom(this Rect rect, float height) => new Rect(rect.x, rect.y, rect.width, rect.height - height);
public static Rect CutHorizontal(this Rect rect, float leftAndRight) => CutHorizontal(rect, leftAndRight, leftAndRight);
public static Rect CutHorizontal(this Rect rect, float left, float right) => new Rect(rect.x + left, rect.y, rect.width - left - right, rect.height);
public static Rect CutVertical(this Rect rect, float topAndBottom) => CutVertical(rect, topAndBottom, topAndBottom);
public static Rect CutVertical(this Rect rect, float top, float bottom) => new Rect(rect.x, rect.y + top, rect.width, rect.height - top - bottom);
public static Rect Cut(this Rect rect, float topBottom, float leftRight) => Cut(rect, topBottom, leftRight, topBottom, leftRight);
public static Rect Cut(this Rect rect, float top, float right, float bottom, float left) => new Rect(rect.x + left, rect.y + top, rect.width - left - right, rect.height - top - bottom);
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 68261ebef89d61441a35961731b0a083
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,201 @@
using System;
using System.Collections.Generic;
using System.Reflection;
using UnityEditor;
using System.Linq;
using AYellowpaper.SerializedCollections.Editor.Data;
using UnityEngine;
using System.Collections;
namespace AYellowpaper.SerializedCollections.Editor
{
internal static class SCEditorUtility
{
public const string EditorPrefsPrefix = "SC_";
public const bool KeyFlag = true;
public const bool ValueFlag = false;
public static bool GetPersistentBool(string path, bool defaultValue)
{
return EditorPrefs.GetBool(EditorPrefsPrefix + path, defaultValue);
}
public static bool HasKey(string path)
{
return EditorPrefs.HasKey( EditorPrefsPrefix + path );
}
public static void SetPersistentBool(string path, bool value)
{
EditorPrefs.SetBool(EditorPrefsPrefix + path, value);
}
public static float CalculateHeight(SerializedProperty property, DisplayType displayType)
{
return CalculateHeight(property, displayType == DisplayType.List ? true : false);
}
public static float CalculateHeight(SerializedProperty property, bool drawAsList)
{
if (drawAsList)
{
float height = 0;
foreach (SerializedProperty child in GetChildren(property))
height += EditorGUI.GetPropertyHeight(child, true);
return height;
}
return EditorGUI.GetPropertyHeight(property, true);
}
public static IEnumerable<SerializedProperty> GetChildren(SerializedProperty property, bool recursive = false)
{
if (!property.hasVisibleChildren)
{
yield return property;
yield break;
}
SerializedProperty end = property.GetEndProperty();
property.NextVisible(true);
do
{
yield return property;
} while (property.NextVisible(recursive) && !SerializedProperty.EqualContents(property, end));
}
public static int GetActualArraySize(SerializedProperty arrayProperty)
{
return GetChildren(arrayProperty).Count() - 1;
}
public static PropertyData GetPropertyData(SerializedProperty property)
{
var data = new PropertyData();
var json = EditorPrefs.GetString(EditorPrefsPrefix + property.propertyPath, null);
if (json != null)
EditorJsonUtility.FromJsonOverwrite(json, data);
return data;
}
public static void SavePropertyData(SerializedProperty property, PropertyData propertyData)
{
var json = EditorJsonUtility.ToJson(propertyData);
EditorPrefs.SetString(EditorPrefsPrefix + property.propertyPath, json);
}
public static bool ShouldShowSearch(int pages)
{
var settings = EditorUserSettings.Get();
return settings.AlwaysShowSearch ? true : pages >= settings.PageCountForSearch;
}
public static bool HasDrawerForType(Type type, bool isPropertyManagedReferenceType)
{
Type attributeUtilityType = typeof(SerializedProperty).Assembly.GetType("UnityEditor.ScriptAttributeUtility");
if (attributeUtilityType == null)
return false;
var getDrawerMethod = attributeUtilityType.GetMethod("GetDrawerTypeForType", BindingFlags.Static | BindingFlags.NonPublic);
if (getDrawerMethod == null)
return false;
#if UNITY_2022_3_OR_NEWER
if (getDrawerMethod.GetParameters().Length == 2)
{
return getDrawerMethod.Invoke(type, new object[] { type, isPropertyManagedReferenceType }) != null;
} else if(getDrawerMethod.GetParameters().Length == 3)
{
return getDrawerMethod.Invoke(type, new object[] { type, new Type[0], isPropertyManagedReferenceType }) != null;
}
else
{
return getDrawerMethod.Invoke(type, new object[] { type }) != null;
}
#else
return getDrawerMethod.Invoke(type, new object[] { type }) != null;
#endif
}
internal static void AddGenericMenuItem(GenericMenu genericMenu, bool isOn, bool isEnabled, GUIContent content, GenericMenu.MenuFunction action)
{
if (isEnabled)
genericMenu.AddItem(content, isOn, action);
else
genericMenu.AddDisabledItem(content);
}
internal static void AddGenericMenuItem(GenericMenu genericMenu, bool isOn, bool isEnabled, GUIContent content, GenericMenu.MenuFunction2 action, object userData)
{
if (isEnabled)
genericMenu.AddItem(content, isOn, action, userData);
else
genericMenu.AddDisabledItem(content);
}
internal static bool TryGetTypeFromProperty(SerializedProperty property, out Type type)
{
try
{
var classType = typeof(EditorGUI).Assembly.GetType("UnityEditor.ScriptAttributeUtility");
var methodInfo = classType.GetMethod("GetFieldInfoFromProperty", BindingFlags.Static | BindingFlags.NonPublic);
var parameters = new object[] { property, null };
methodInfo.Invoke(null, parameters);
type = (Type) parameters[1];
return true;
}
catch
{
type = null;
return false;
}
}
public static object GetPropertyValue(SerializedProperty prop, object target)
{
var path = prop.propertyPath.Replace(".Array.data[", "[");
var elements = path.Split('.');
foreach (var element in elements.Take(elements.Length - 1))
{
if (element.Contains("["))
{
var elementName = element.Substring(0, element.IndexOf("["));
var index = Convert.ToInt32(element.Substring(element.IndexOf("[")).Replace("[", "").Replace("]", ""));
target = GetValue(target, elementName, index);
}
else
{
target = GetValue(target, element);
}
}
return target;
}
public static object GetValue(object source, string name)
{
if (source == null)
return null;
var type = source.GetType();
var f = type.GetField(name, BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);
if (f == null)
{
var p = type.GetProperty(name, BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance | BindingFlags.IgnoreCase);
if (p == null)
return null;
return p.GetValue(source, null);
}
return f.GetValue(source);
}
public static object GetValue(object source, string name, int index)
{
var enumerable = GetValue(source, name) as IEnumerable;
var enm = enumerable.GetEnumerator();
while (index-- >= 0)
enm.MoveNext();
return enm.Current;
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 138277cf2e7d2cd4e99c3cd7e2ecaaed
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,98 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using UnityEditor;
using UnityEngine;
namespace AYellowpaper.SerializedCollections.Editor
{
internal static class SCEnumUtility
{
private static Dictionary<Type, EnumCache> _cache = new Dictionary<Type, EnumCache>();
internal static EnumCache GetEnumCache(Type enumType)
{
if (_cache.TryGetValue(enumType, out var val))
return val;
try
{
var classType = typeof(EditorGUI).Assembly.GetType("UnityEditor.EnumDataUtility");
var methodInfo = classType.GetMethod("GetCachedEnumData", BindingFlags.Static | BindingFlags.NonPublic);
var parameters = new object[] { enumType, true };
var result = methodInfo.Invoke(null, parameters);
var flagValues = (int[])result.GetType().GetField("flagValues").GetValue(result);
var names = (string[])result.GetType().GetField("names").GetValue(result);
var cache = new EnumCache(enumType, flagValues, names);
_cache.Add(enumType, cache);
return cache;
}
catch
{
throw;
}
}
}
internal record EnumCache
{
public readonly Type Type;
public readonly bool IsFlag;
public readonly int Length;
public readonly int[] FlagValues;
public readonly string[] Names;
private readonly Dictionary<int, string[]> _namesByValue = new Dictionary<int, string[]>();
public EnumCache(Type type, int[] flagValues, string[] displayNames)
{
Type = type;
FlagValues = flagValues;
Names = displayNames;
Length = flagValues.Length;
IsFlag = Type.IsDefined(typeof(FlagsAttribute));
}
internal string[] GetNamesForValue(int value)
{
if (_namesByValue.TryGetValue(value, out var list))
return list;
string[] array = IsFlag ? GetFlagValues(value).ToArray() : new[] { GetEnumValue(value) };
_namesByValue.Add(value, array);
return array;
}
private string GetEnumValue(int value)
{
for (int i = 0; i < Length; i++)
{
if (FlagValues[i] == value)
return Names[i];
}
return null;
}
private IEnumerable<string> GetFlagValues(int flagValue)
{
if (flagValue == 0)
{
yield return FlagValues[0] == 0 ? Names[0] : "Nothing";
yield break;
}
for (int i = 0; i < Length; i++)
{
int fv = FlagValues[i];
if ((fv & flagValue) == fv && fv != 0)
yield return Names[i];
}
if (FlagValues[Length - 1] != -1 && flagValue == -1)
yield return "Everything";
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 76def6672c756704bac4efd5a3625113
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: