This library utilizes Roslyn Source Generator to generate type wrappers for any C# type.
- Any C# type can be wrapped, with an exception of
dynamic
. - Wrappers can be a
struct
, aclass
, or arecord
.- A
record
wrapper can also be generic if needed.
- A
- Wrappers have
implicit
andexplicit
operators to convert between itself and the original type. - All public members of the original type are exposed in the wrapper. Supported members are fields, properties, events, indexers, methods, and operators.
IEquatable<T>
andIComparable<T>
are implemented for the wrapper if the original type allows them.- Equality operators (
==
,!=
) are overloaded for the wrapper if the original type allows them.
- Unity 2022.3 or later
-
Open menu
Window
->Package Manager
. -
Click the
+
button at the top-left corner, then chooseAdd package from git URL...
. -
Enter the package URL
https://github.com/laicasaane/TypeWrap/tree/main/Packages/TypeWrap
.
- Install OpenUPM CLI.
- Run the following command in your Unity project root directory:
openupm add com.laicasaane.typewrap
- Use this attribute if the wrapper itself is either a
struct
or aclass
.- In case of a
class
, it must not inherit from any other class. - The wrapper must be
partial
.
- In case of a
- By default, the underlying type name is
value
. You can change it by specifying thememberName
argument. - By default, a type converter is generated for the wrapper. You can exclude it by specifying the
ExcludeConverter
property.
[WrapType(typeof(int))]
public partial struct IntWrapper { }
[WrapType(typeof(List<int>), memberName: "wrappedList")]
public partial class ListInt { }
[WrapType(typeof(IDisposable), ExcludeConverter = true)]
public readonly partial struct DisposableObject { }
- Use this attribute if the wrapper itself is either a
record struct
or arecord class
.- In case of a
record class
, it must not inherit from any other class. - The wrapper must be
partial
.
- In case of a
- The primary constructor of the record must have exactly 1 parameter.
- By default, a type converter is generated for the wrapper. You can exclude it by specifying the
ExcludeConverter
property.
[WrapRecord(ExcludeConverter = true)]
public partial record struct IntWrapper(int Value);
[WrapRecord]
public partial record class ListT<T>(List<T> _);
[WrapRecord]
public readonly partial record struct Coord2D(Vector2Int _);
-
To have
record
in Unity, you need to use Unity 2022.3 or later, and enableC# 10
feature by placing thiscsc.rsp
file in yourAssets
folder: https://github.com/laicasaane/TypeWrap/blob/main/Assets/csc.rsp -
To have
readonly record
in any assembly (or.asmdef
), you have to declare this class inside that assembly:namespace System.Runtime.CompilerServices { [ComponentModel.EditorBrowsable(ComponentModel.EditorBrowsableState.Never)] class IsExternalInit { } }
-
You might also want to place this file in your
Assets
and the root of your project to enable theC# 10
feature for the code editor: https://github.com/laicasaane/TypeWrap/blob/main/Directory.Build.props