docs.unity3d.com
Search Results for

    Show / Hide Table of Contents

    PortConverter

    Port converters are used to convert PortTypes that are incompatible. When you create a PortConverter, it's possible to connect incompatible ports together. An implicit converter is created between the two ports, which is in charge of converting the values and propagating them to the next port. The converter adds some overhead, so it's best to avoid them when you can.

    For example, if you have a PortType<int> on one node, and a PortType<uint> on the other, they wouldn't be able to connect without a PortConverter.

    Definition of the converter

    Define your converter so that SystemGraph core can find it when attaching ports. Note that the converter is considered unidirectional, it will propagate both sides, however, it will always attach firstVar to the driver of the port. You must create 2 converters:

    • int -> uint
    • uint -> int.
    [PortConverter(typeof(int), typeof(uint))]
    public class PortConverterIntUint : PortConverter<int, uint>
    {
        public override void ConvertFirstToSecond()
        {
            secondVar.Write = (uint)firstVar.ReadRaw;
        }
        public override void ConvertSecondToFirst()
        {
            firstVar.Write = (int)secondVar.ReadRaw;
        }
    }
    
    [PortConverter(typeof(uint), typeof(int))]
    public class PortConverterUintInt : PortConverter<uint, int>
    {
        public override void ConvertFirstToSecond()
        {
            secondVar.Write = (int)firstVar.ReadRaw;
        }
        public override void ConvertSecondToFirst()
        {
            firstVar.Write = (uint)secondVar.ReadRaw;
        }
    }
    

    When you create this converter, you can now connect PortType<int> to PortType<uint> in the Editor window. A yellow circle on the edge of the connection indicates there's additional overhead created by the converter.

    Port converter in action

    Runtime

    At runtime, the port converter is instantiated when SystemGraph attempts to connect ports of different types. It then receives change events when the value changes on either side of the converter and synchronizes the converted value.

    In This Article
    Back to top
    Copyright © 2024 Unity Technologies — Trademarks and terms of use
    • Legal
    • Privacy Policy
    • Cookie Policy
    • Do Not Sell or Share My Personal Information
    • Your Privacy Choices (Cookie Settings)