Skip to content

Commit a66912f

Browse files
authored
Add a way to easily add ToString converters (#88)
* Add a way to easily add ToString converters * Allow replacing converters
1 parent 70e439b commit a66912f

File tree

3 files changed

+15
-2
lines changed

3 files changed

+15
-2
lines changed

RuntimeUnityEditor.Core/Utils/TomlTypeConverter.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Globalization;
4+
using RuntimeUnityEditor.Core.Inspector;
45
using RuntimeUnityEditor.Core.Utils.Abstractions;
56
using UnityEngine;
67
using ColorUtility = RuntimeUnityEditor.Core.Utils.Abstractions.ColorUtility;
@@ -178,6 +179,7 @@ public static bool AddConverter(Type type, TypeConverter converter)
178179
}
179180

180181
TypeConverters.Add(type, converter);
182+
ToStringConverter._canCovertCache[type] = true;
181183
return true;
182184
}
183185

RuntimeUnityEditor.Core/Windows/Inspector/ToStringConverter.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,11 @@ public static class ToStringConverter
1717

1818
public static void AddConverter<TObj>(Func<TObj, string> objectToString)
1919
{
20-
_toStringConverters.Add(typeof(TObj), o => objectToString.Invoke((TObj)o));
20+
if (objectToString == null) throw new ArgumentNullException(nameof(objectToString));
21+
22+
var type = typeof(TObj);
23+
_toStringConverters[type] = o => objectToString.Invoke((TObj)o);
24+
_canCovertCache[type] = true;
2125
}
2226

2327
public static string ObjectToString(object value)
@@ -108,7 +112,7 @@ internal static string EventEntryToString(UnityEventBase eventObj, int i)
108112
return $"{eventObj.GetPersistentTarget(i)?.GetType().GetSourceCodeRepresentation() ?? "[NULL]"}.{eventObj.GetPersistentMethodName(i)}";
109113
}
110114

111-
private static readonly Dictionary<Type, bool> _canCovertCache = new Dictionary<Type, bool>();
115+
internal static readonly Dictionary<Type, bool> _canCovertCache = new Dictionary<Type, bool>();
112116

113117
public static bool CanEditValue(ICacheEntry field, object value)
114118
{

RuntimeUnityEditor.Core/Windows/REPL/REPL.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Reflection;
66
using System.Text;
77
using Mono.CSharp;
8+
using RuntimeUnityEditor.Core.Inspector;
89
using RuntimeUnityEditor.Core.Inspector.Entries;
910
using RuntimeUnityEditor.Core.ObjectTree;
1011
using RuntimeUnityEditor.Core.Utils.Abstractions;
@@ -261,6 +262,12 @@ public static string dump(object @object)
261262
return Dumper.DumpToTempFile(@object, "REPL_OBJECT");
262263
}
263264

265+
[Documentation("registerToString<TObj>(Func<TObj, string>) - add a ToString converter for a Type to change how it's displayed in Inspector and some other places.")]
266+
public static void registerToString<TObj>(Func<TObj, string> objectToString)
267+
{
268+
ToStringConverter.AddConverter(objectToString);
269+
}
270+
264271
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Property)]
265272
private class DocumentationAttribute : Attribute
266273
{

0 commit comments

Comments
 (0)