As explained earlier, “value ports can only be connected to other value ports of the same or of assignable types”.
What this basically means, is that you are able to connect certain value type ports together even though they are not of the exact same type, if they can be converted or casted to that type. This is handled automatically for you and saves you a lot of time. In practice, if the editor allows you to connect 2 ports together then it means it’s possible and you have nothing more to worry about. When such a conversion is taking place, the connection will display an icon indicating the fact. In the following example, a float has been connected to a boolean. As a result, this conversion will result to false since the value is 0 (and true if the value was 1+).
For your convenience, here are the supported conversions by FlowCanvas and how they are done.
From | To | Through |
---|---|---|
Any Primitive | Any Primitive | .ConvertTo() |
GameObject | Any Component | .GetComponent |
GameObject | Transform | .transform |
Any Component | GameObject | .gameObject |
GameObject | Vector3 | .transform.position |
AnyComponent | Vector3 | .transform.position |
A Child Type | Base Type | upcasting |
A Base Type | Child Type | downcasting |
Anything | String | .ToString() |
You can also create custom converters from one type to another through code by subscribing to the ‘TypeConverter.customConverter’ event. Following is code on how to utilize this event as well as converting the Unity ‘Light’ type to a ‘float’ type by returning the Light.intensity for the shake of this example. Please note that this is an advanced topic.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
public static class CustomConverters { [RuntimeInitializeOnLoadMethod] #if UNITY_EDITOR [UnityEditor.InitializeOnLoadMethod] #endif static void Init() { TypeConverter.customConverter += OnCustomConverter; } static ValueHandler<object> OnCustomConverter(Type sourceType, Type targetType, ValueHandler<object> func) { if ( sourceType == typeof(Light) && targetType == typeof(float) ) { return () => ( func() as Light ).intensity; } //add more... return null; } } |
© Paradox Notion 2015-2024. All rights reserved.