mirror of
https://github.com/BepInEx/BepInEx.git
synced 2026-10-19 15:51:45 +00:00
Add TomlTypeConverter for UnityEngine.Rect
Originally pull request #510 Co-authored-by: ManlyMarco <39247311+manlymarco@users.noreply.github.com>
This commit is contained in:
1 parent
6e846fc70a
commit
8597ce7ebf
1 file changed
+43
@@ -1,4 +1,5 @@
|
||||
using System;
|
||||
using System.Globalization;
|
||||
using System.Runtime.CompilerServices;
|
||||
using BepInEx.Configuration;
|
||||
using UnityEngine;
|
||||
@@ -36,5 +37,47 @@ internal static class UnityTomlTypeConverters
|
||||
TomlTypeConverter.AddConverter(typeof(Vector3), jsonConverter);
|
||||
TomlTypeConverter.AddConverter(typeof(Vector4), jsonConverter);
|
||||
TomlTypeConverter.AddConverter(typeof(Quaternion), jsonConverter);
|
||||
TomlTypeConverter.AddConverter(typeof(Rect), new TypeConverter
|
||||
{
|
||||
ConvertToObject = StringToRect,
|
||||
ConvertToString = RectToString
|
||||
});
|
||||
}
|
||||
|
||||
private static object StringToRect(string s, Type type)
|
||||
{
|
||||
// JsonUtility doesn't work with Rect on all Unity versions, so parse it manually
|
||||
Rect result = default(Rect);
|
||||
|
||||
if (s == null)
|
||||
return result;
|
||||
|
||||
string cleaned = s.Trim('{', '}').Replace(" ", "");
|
||||
foreach (string part in cleaned.Split(','))
|
||||
{
|
||||
string[] parts = part.Split(':');
|
||||
if (parts.Length != 2 || !float.TryParse(parts[1], out float value))
|
||||
continue;
|
||||
|
||||
string id = parts[0].Trim('"');
|
||||
if (id == "x")
|
||||
result.x = value;
|
||||
else if (id == "y")
|
||||
result.y = value;
|
||||
else if (id == "width" || id == "z")
|
||||
result.width = value;
|
||||
else if (id == "height" || id == "w")
|
||||
result.height = value;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
private static string RectToString(object o, Type type)
|
||||
{
|
||||
Rect rect = (Rect)o;
|
||||
return string.Format(CultureInfo.InvariantCulture,
|
||||
"{{ \"x\":{0}, \"y\":{1}, \"width\":{2}, \"height\":{3} }}",
|
||||
rect.x, rect.y, rect.width, rect.height);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user