Skip to content

Language & Interop Attributes

Serialization, deprecation, conditional calls and native interop in C#.

Reference baseline: Microsoft API documentation; Unity serialization is called out separately where relevant.

Attribute fragments belong on the declaration described by their requirements; alternatives are shown separately.

Mark a supported type as serializable for serializers that honor SerializableAttribute. Serialization behavior depends on the serializer.

[Serializable]

Requirements: System; supported class or struct; serializer-specific rules.

Primary documentation

Unity’s JsonUtility follows Unity serialization rules; this is not a promise of general .NET serializer behavior.

Mark a declaration as obsolete. The message-only constructor produces a warning; the Boolean argument selects an error.

[Obsolete("Use the replacement API.")]
// Alternative: report an error instead of a warning.
[Obsolete("Use the replacement API.", true)]

Requirements: System; supported declaration target.

Primary documentation

Include calls to a void method only when the named compile-time symbol is defined. The attribute conditions calls; it does not simply remove the method itself.

[Conditional("UNITY_EDITOR")]

Requirements: System.Diagnostics; void method; compile-time symbol.

Primary documentation

Declare a native function through P/Invoke on a static extern method. Replace the illustrative library name with a real native library that is available on the target platform.

[DllImport("NativeLibrary")]

Requirements: System.Runtime.InteropServices; static extern method; target-compatible native library.

Primary documentation