Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Feature Request Partial Structs

Discussion in 'Scripting' started by freakfallout, Feb 13, 2023.

  1. freakfallout

    freakfallout

    Joined:
    May 15, 2018
    Posts:
    5
    Hello everyone!

    I was wondering if there was a plan to have a partial struct added to unity, It doesn't throw any errors in Visual Studio so it seems as if c# normally supports it (I understand unity's mono is not the same as regular mono or Microsoft's C#), and I can have partial classes, but when I do partial structs and then go to the editor it says

    error CS0261: Partial declarations of 'TransformComponent' must be all classes,
    all record classes, all structs, all record structs, or all interfaces


    which means in theory that it should support partial structs as well.

    all declarations are as such
    public partial struct TransformComponent { }

    I'm trying to have partial structs so that I'm able to hide some auto-generated code away from user written code while still being able to use the data in jobs.
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,735
    As far as I know partial classes and partial structs should work fine in Unity. The error message you posted seems to imply that you used two different types for your object. e.g.:
    partial struct TransformComponent
    in one file and
    partial class TransformComponent
    in another file.
     
  3. AngryProgrammer

    AngryProgrammer

    Joined:
    Jun 4, 2019
    Posts:
    437
  4. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,571
    Splitting the fields of a struct into several partial declarations would cause this warning as the order in memory is not clear from the declaration. You could use a StructLayout(LayoutKind.Explicit) and give each field their own byte offset. This should work, though it has the potential risk of causing field overlaps which could cause complete data corruption. Here's an example that should not throw a warning:

    Code (CSharp):
    1. [StructLayout(LayoutKind.Explicit)]
    2. public partial struct TestStruct
    3. {
    4.     [FieldOffset(0)]
    5.     public int v1;
    6. }
    7.  
    8. public partial struct TestStruct
    9. {
    10.     [FieldOffset(4)]
    11.     public int v2;
    12. }
    13.  

    Of course you would need a
    using System.Runtime.InteropServices;
    at the top. When using an explicit layout, you have to specify the exact byte offset of each field manually. So while this may work, I would not recommend it, especially when you want to mix auto generated code with user generated code. The offsets have to be aware of each other. So you're not gaining anything. That's why Microsofts only recommendation is to put all field declarations into one of the two partial declarations. You can add additional methods to the other declaration.
     
  5. freakfallout

    freakfallout

    Joined:
    May 15, 2018
    Posts:
    5
    I made sure to that each of the structs are formatted as

    public partial struct TransformComponent { }

    so that isn't the issue sadly

    I also don't have any fields in either of the structs(but I did try adding some), they're both empty and in the same namespace I was also only planning on auto-generating methods, so that shouldn't be an issue, but thanks for letting me know! I tried the Explicit StructLayout but it also didn't help sadly :( still unable to do anything but partial classes

    I tried the exact same code in a different project on a different version of Unity and it seemed to work fine though, quite weird.

    Thanks for the quick help and responses!
     
  6. AngryProgrammer

    AngryProgrammer

    Joined:
    Jun 4, 2019
    Posts:
    437
    By the way, why are you creating a partial struct? Something like structure should be small, some even say it should not exceed 16 bytes.

    If you are creating something big, use classes, there may be partial classes as in the video I pasted above.

    In Unity, partial classes are also not particularly needed because scripts are components. So you put a move script, data script, skill script, etc. on some game object and you have access to each through GetComponent.
     
    Bunny83 likes this.
  7. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,571
    Well, you haven't mentioned any version as of now. Like it was mentioned above, the specific error you mentioned CS0261 indicates that you have multiple different types with the same name declared partial. Maybe you have changed a class to a struct but haven't saved the / all files yet?

    What Unity version do you use?
     
  8. freakfallout

    freakfallout

    Joined:
    May 15, 2018
    Posts:
    5
    I'm trying to have partial structs so that I'm able to hide some auto-generated code away from user written code while still being able to use the data in jobs for performance heavy parts of my code.

    I just realized that I actually did have one of the references as a class, oops! It was in a file I didn't think had anything in it, I did a ctrl-F through entire solution for it, it was user-error in the end of course, just needed to do a little more digging. Sorry about all this!
     
    chemicalcrux, PraetorBlue and Bunny83 like this.