Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by VildNinja · Jun 13, 2017 at 02:46 PM · platformsunsafe

platform dependencies using InteropServices.StructLayout Explicit

Hi devs, my code works perfectly fine, but I'm concerned whether this will break when being compiled to multiple platforms.

I have a 2D byte vector used as a lookup in a map, that I am doubling as an index for faster comparison in my path finding. My solution is to mark the v2b struct as explicit, letting a ushort index overlap the two bytes, so that setting either the index or the coordinates will update the others:

  [System.Runtime.InteropServices.StructLayout(System.Runtime.InteropServices.LayoutKind.Explicit)]
     public struct v2b
     {
         [System.Runtime.InteropServices.FieldOffset(0)]
         public ushort index;
         [System.Runtime.InteropServices.FieldOffset(0)]
         public byte x;
         [System.Runtime.InteropServices.FieldOffset(1)]
         public byte y;
     }

Everything works as intended, there might be better solutions, but right now I would just like to know if anyone knows of any issues with explicit layouts on any platforms?

Thanks in advance.

/jannek

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image VildNinja · Jun 14, 2017 at 11:25 AM 1
Share

Just had a talk with one of the engine coders, and seems worst case would be differences in endians, though that will probably not happen, since it's c#. I would still love to hear if anyone has any additional knowledge of this :)

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by clancy1066 · Apr 13, 2020 at 10:17 PM

If I have a class A that has a member that is like yours

 public class A : Monobehaviour
 {
   v2b  myVal;
 }

If I do:

 A myA = Instantiate(<valid "A" instance));
 
 myA.y = 1;

, when I Instantiate() an"A", the "MyA" structure is garbage(non-null) that the compiler nor runtime exception handlers catch. " myA.y = 1;" crashes Unity hard.

Not sure if this is the same thing you might be seeing. I made a separate ticket for it. I'll let you know if I get a solid answer

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bunny83 · Apr 14, 2020 at 01:58 AM 0
Share

Uhm your code doesn't make much sense. Your class A does not have a field called "y". I used struct layout explicit in the past without any issues. I'm also not sure what you mean by

the "$$anonymous$$yA" structure is garbage

it's a struct that therefore either part of the memory of the containing class or allocated on the stack. So the struct itself can not be null or "garbage". It can not be garbage if you still have access to it.


To me it seems your description is missing important details on your case. Also it seems that you used your answer just to put your own question forward. You should have asked your own question and add more details. Also a code example that actually makes sense would help to follow what your issue might be. It would also help to know a bit more about the background (Unity version, scripting backend, target platform) since certain platforms have various restrictions


edit
I just found your question and it's a very different case than the one in question here. The short answer is yes, you can not have reference types and value types share the same memory space. Last time I tried this the compiler complained about that. This is also verified through other peoples over here.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

67 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to compensate for moving platforms that move side to side 2 Answers

How can I create an animation of platforms 1 Answer

toggle rigidbody constraints? 1 Answer

Sync assets that are built in different platforms 0 Answers

How can I pass pointers to DLL functions? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges