docs.unity3d.com
Search Results for

    Show / Hide Table of Contents

    Calling Burst-compiled code

    You can call Burst-compiled methods direct from managed code. Calling generic methods or methods whose declaring type is generic isn't supported, otherwise the same rules as for function pointers apply. However, you don't need to worry about the extra boiler plate needed for function pointers.

    The following example shows a Burst-compiled utility class. Because it uses structs, it passes by reference per the function pointer rules.

    [BurstCompile]
    public static class MyBurstUtilityClass
    {
        [BurstCompile]
        public static void BurstCompiled_MultiplyAdd(in float4 mula, in float4 mulb, in float4 add, out float4 result)
        {
            result = mula * mulb + add;
        }
    }
    

    Use this method from managed code like so:

    public class MyMonoBehaviour : MonoBehaviour
    {
        void Start()
        {
            var mula = new float4(1, 2, 3, 4);
            var mulb = new float4(-1,1,-1,1);
            var add = new float4(99,0,0,0);
            MyBurstUtilityClass.BurstCompiled_MultiplyAdd(mula, mulb, add, out var result);
            Debug.Log(result);
        }
    }
    

    If you attach this script to an object and run it, float4(98f, 2f, -3f, 4f) is printed to the log.

    Code transformation

    Burst uses IL Post Processing to automatically transform the code into a function pointer and call. For more information, refer to the documentation on Function pointers.

    To disable the direct call transformation, addDisableDirectCall = true to the BurstCompile options. This prevents the Post Processor from running on the code:

    [BurstCompile]
    public static class MyBurstUtilityClass
    {
        [BurstCompile(DisableDirectCall = true)]
        public static void BurstCompiled_MultiplyAdd(in float4 mula, in float4 mulb, in float4 add, out float4 result)
        {
            result = mula * mulb + add;
        }
    }
    

    Additional resources

    • HPC# Overview
    • Function pointers
    In This Article
    Back to top
    Copyright © 2025 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)