docs.unity3d.com
Search Results for

    Show / Hide Table of Contents

    Create a shared component

    You can create both managed and unmanaged shared components.

    Create an unmanaged shared component

    To create an unmanaged shared component, create a struct that implements the marker interface ISharedComponentData.

    The following code sample shows an unmanaged shared component:

    public struct ExampleUnmanagedSharedComponent : ISharedComponentData
    {
        public int Value;
    }
    

    Create a managed shared component

    To create a managed shared component, create a struct that implements the marker interface ISharedComponentData and IEquatable<>, and ensure public override int GetHashCode() is implemented. The equality methods are necessary to ensure comparisons do not generate managed allocations unnecessarily due to implict boxing when using the default Equals and GetHashCode implementations.

    The following code sample shows a managed shared component:

    public struct ExampleManagedSharedComponent : ISharedComponentData, IEquatable<ExampleManagedSharedComponent>
    {
        public string Value; // A managed field type
    
        public bool Equals(ExampleManagedSharedComponent other)
        {
            return Value.Equals(other.Value);
        }
    
        public override int GetHashCode()
        {
            return Value.GetHashCode();
        }
    }
    
    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)