Skip to content

Make Reinterop skip attributes with 'Ignore' attribute#665

Merged
david-lively merged 1 commit intomainfrom
ignore-attributes
Feb 26, 2026
Merged

Make Reinterop skip attributes with 'Ignore' attribute#665
david-lively merged 1 commit intomainfrom
ignore-attributes

Conversation

@j9liu
Copy link
Contributor

@j9liu j9liu commented Feb 25, 2026

Description

This PR supports #656 and enables the use of Unity's Color32 struct with Reinterop.

As @kring discovered, Color32 has an int field that doesn't actually contribute to the struct's size. Reinterop was turning this into an actual int field, preventing the actual r,g etc. bytes from storing value.

[StructLayout(LayoutKind.Explicit)]
[UsedByNativeCode]
public struct Color32 : IEquatable<Color32>, IFormattable
{
    [FieldOffset(0)]
    [Ignore(DoesNotContributeToSize = true)]
    private int rgba;

    //
    // Summary:
    //     Red component of the color.
    [FieldOffset(0)]
    public byte r;

    //
    // Summary:
    //     Green component of the color.
    [FieldOffset(1)]
    public byte g;

    //
    // Summary:
    //     Blue component of the color.
    [FieldOffset(2)]
    public byte b;

    //
    // Summary:
    //     Alpha component of the color.
    [FieldOffset(3)]
    public byte a;
 

The easiest way to avoid this is to tell Reinterop to ignore anything with that Ignore attribute. It hasn't resulted in any surprises in any other generated classes; I checked with diff tools.

Testing plan

  1. Rebuild Reinterop using dotnet publish Reinterop~ -o . before opening Unity.
  2. Open Unity, then add a line like Color32 color = new Color32(0, 0, 0, 1) to Reinterop
  3. Wait for Reinterop to complete, then navigate to native~/generated-Editor/include/DotNet/UnityEngine/Color32.h.
  4. Confirm that the uint8 r, g, b, and a fields show up, but not an int32 rgba field.

@j9liu j9liu requested a review from david-lively February 25, 2026 22:32
@j9liu j9liu self-assigned this Feb 25, 2026
@j9liu j9liu mentioned this pull request Feb 25, 2026
5 tasks
@j9liu j9liu added the reinterop Issues related to Reinterop label Feb 26, 2026
Copy link
Contributor

@david-lively david-lively left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Short and simple. Looks good to me.

@david-lively david-lively merged commit 7a2c62f into main Feb 26, 2026
13 of 14 checks passed
@david-lively david-lively deleted the ignore-attributes branch February 26, 2026 16:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

reinterop Issues related to Reinterop

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants