Forums › 💬 NodeCanvas › 🗨️ General Discussion › Getter and Setter extension › Reply To: Getter and Setter extension
HeyGavalakis,
I need getter/setter for my custom types so I implement a simple way to add it.
Add this lines at the beginning of TypeConverter.cs:
|
1 2 |
public delegate Func<object, object> CustomConverter(Type fromType, Type toType); public static CustomConverter customConverter; |
And in public static Func<object, object> Get(Type fromType, Type toType) method:
|
1 2 3 4 5 6 |
if (customConverter != null){ Func<object, object> converter = customConverter(fromType, toType); if (converter != null){ return converter; } } |
Now, in your custom classes you can add this to auto convert your custom types:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
[InitializeOnLoadMethod] [RuntimeInitializeOnLoadMethod] public static void InitConverter() { ParadoxNotion.TypeConverter.customConverter += Converter; } private static Func<object, object> Converter(Type _fromType, Type _toType) { if (_fromType == typeof(MyCustomType)) { if (_toType == typeof(Vector3)) { return (value) => { MyCustomType myType = (MyCustomType) value; return myType!=null?myType.position:Vector3.zero; }; } //... } else if (_toType == typeof(MyCustomType)) { if (_fromType == typeof(Vector3)) { //Warning: This can generate garbage if MyCustomType is a class return (value) => new MyCustomType((Vector3)value); } //... } return null; } |
Sounds good to you ?
