Is it legal to use fixed pointer in Unity for external use?

69d2483dc37cb1efa64715dffceacdb0


I know I can use nativeList but I want to know if this is allowed

This is only guaranteed to work when the variable you pin in the fixed statement doesn’t move. Examples of non-moving things would be manually allocated memory from UnsafeUtility.Malloc and a struct instance on the stack, both of which don’t require a fixed statement.

The main issue would be anything that lives in the managed heap. This includes all reference types, including managed arrays e.g. int[]). Things in the heap are not guaranteed to stay in the same place depending on the GC implementation. Unity’s current GC implementation doesn’t compact memory, but the potential update to CoreCLR would come with a compacting GC, meaning objects could be relocated. In that case, once you leave the fixed statement, the field will no longer be pinned, so pointers you stored while within the fixed statement are no longer guaranteed to be valid.

Can I understand it as legal under the current Unity Mono GC?

With the version of Mono technology that Unity is currently using in builds available to us, yes.