Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit ac7bb11

Browse files
Support type overloading and decimal
1 parent f3f0139 commit ac7bb11

File tree

6 files changed

+4309
-1008
lines changed

6 files changed

+4309
-1008
lines changed

‎README.md

Lines changed: 44 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,11 @@ While IL2CPP transforms C# into C++ already, it generates a lot of overhead. The
5858

5959
## Industry Standard Language
6060

61-
C++ is the standard language for video games as well as many other fields. By programming in C++ you can more easily transfer your skills and code to and from non-Unity projects. For example, you can avoid lock-in by using the same language (C++) that you'd use in the Unreal or Lumberyard engines.
61+
C++ is the standard language for video games as well as many other fields. By programming in C++ you can more easily transfer your skills and code to and from non-Unity projects. For example, you can avoid lock-in by using the same language (C++) that you'd use in the [Unreal](https://www.unrealengine.com) or [Lumberyard](https://aws.amazon.com/lumberyard/) engines.
6262

6363
# UnityNativeScripting Features
6464

65+
* Code generator exposes any C# API to C++
6566
* Supports Windows, macOS, Linux, iOS, and Android (editor and standalone)
6667
* Works with Unity 2017.x and 5.x
6768
* Plays nice with other C# scripts- no need to use 100% C++
@@ -85,26 +86,48 @@ C++ is the standard language for video games as well as many other fields. By pr
8586

8687
* Platform-dependent compilation via the [usual flags](https://docs.unity3d.com/Manual/PlatformDependentCompilation.html) (e.g. `#if UNITY_EDITOR`)
8788
* [CMake](https://cmake.org/) build system sets up any IDE project or command-line build
88-
* Code generator exposes any C# API (Unity, .NET, custom DLLs) with a simple JSON config file and runs from a menu in the Unity editor. It supports a wide range of features:
89-
* Class types
90-
* Struct types
91-
* Enumeration types
92-
* Base classes
93-
* Constructors
94-
* Methods
95-
* Fields
96-
* Properties (getters and setters)
97-
* `out` and `ref` parameters
98-
* Exceptions
99-
* Overloaded operators
100-
* Arrays (single- and multi-dimensional)
101-
* Delegates
102-
* Events
103-
* Boxing and unboxing (e.g. casting `int` to `object` and visa versa)
104-
* Implementing C# interfaces with C++ classes
105-
* Deriving from C# classes with C++ classes
106-
* Default parameters
107-
* Generic types and methods
89+
90+
# Code Generator
91+
92+
The core of this project is a code generator. It generates C# and C++ code called "bindings" that make C# APIs available to C++ game code. It supports a wide range of language features:
93+
94+
* Types
95+
* `class`
96+
* `struct`
97+
* `enum`
98+
* Arrays (single- and multi-dimensional)
99+
* Delegates (e.g. `Action`)
100+
* `decimal`
101+
* Type Contents
102+
* Constructors
103+
* Methods
104+
* Fields
105+
* Properties (`get` and `set` like `obj.x`)
106+
* Indexers (`get` and `set` like `obj[x]`)
107+
* Events (`add` and `remove` delegates)
108+
* Overloaded operators
109+
* Boxing and unboxing (e.g. casting `int` to `object` and visa versa)
110+
* Function Features
111+
* `out` and `ref` parameters
112+
* Generic types and methods
113+
* Default parameters
114+
* Cross-Language Features
115+
* Exceptions (C# to C++ and C++ to C#)
116+
* Implementing C# interfaces with C++ classes
117+
* Deriving from C# classes with C++ classes
118+
119+
Note that the code generator does not yet support:
120+
121+
* `Array`, `string`, and `object` methods (e.g. `GetHashCode`)
122+
* Non-null string default parameters and null non-string default parameters
123+
* Implicit `params` parameter (a.k.a. "var args") passing
124+
* C# pointers
125+
* Nested types
126+
* Down-casting
127+
128+
To configure the code generator, open `Unity/Assets/NativeScriptTypes.json` and notice the existing examples. Add on to this file to expose more C# APIs from Unity, .NET, or custom DLLs to your C++ code.
129+
130+
To run the code generator, choose `NativeScript > Generate Bindings` from the Unity editor.
108131

109132
# Performance
110133

@@ -194,20 +217,6 @@ With C++, the workflow looks like this:
194217
6. The build scripts or IDE project files are now generated in your build directory
195218
7. Build as appropriate for your generator. For example, execute `make` if you chose `Unix Makefiles` as your generator.
196219

197-
# The Code Generator
198-
199-
To run the code generator, choose `NativeScript > Generate Bindings` from the Unity editor.
200-
201-
To configure the code generator, open `NativeScriptTypes.json` and notice the existing examples. Add on to this file to expose more C# APIs from Unity, .NET, or custom DLLs to your C++ code.
202-
203-
Note that the code generator does not support (yet):
204-
205-
* `Array`, `string`, and `object` methods (e.g. `GetHashCode`)
206-
* Non-null string default parameters and null non-string default parameters
207-
* Implicit `params` parameter (a.k.a. "var args") passing
208-
* `decimal`
209-
* C# pointers
210-
211220
# Updating To A New Version
212221

213222
To update to a new version of this project, overwrite your Unity project's `Assets/NativeScript` directory with this project's `Unity/Assets/NativeScript` directory and re-run the code generator.

‎Unity/Assets/NativeScript/Bindings.cs

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,11 @@ delegate void InitDelegate(
334334
IntPtr enumerableGetEnumerator,
335335
/*BEGIN INIT PARAMS*/
336336
int maxManagedObjects,
337+
IntPtr releaseSystemDecimal,
338+
IntPtr systemDecimalConstructorSystemDouble,
339+
IntPtr systemDecimalConstructorSystemUInt64,
340+
IntPtr boxDecimal,
341+
IntPtr unboxDecimal,
337342
IntPtr unityEngineVector3ConstructorSystemSingle_SystemSingle_SystemSingle,
338343
IntPtr unityEngineVector3Methodop_AdditionUnityEngineVector3_UnityEngineVector3,
339344
IntPtr boxVector3,
@@ -498,6 +503,11 @@ static extern void Init(
498503
IntPtr enumerableGetEnumerator,
499504
/*BEGIN INIT PARAMS*/
500505
int maxManagedObjects,
506+
IntPtr releaseSystemDecimal,
507+
IntPtr systemDecimalConstructorSystemDouble,
508+
IntPtr systemDecimalConstructorSystemUInt64,
509+
IntPtr boxDecimal,
510+
IntPtr unboxDecimal,
501511
IntPtr unityEngineVector3ConstructorSystemSingle_SystemSingle_SystemSingle,
502512
IntPtr unityEngineVector3Methodop_AdditionUnityEngineVector3_UnityEngineVector3,
503513
IntPtr boxVector3,
@@ -570,6 +580,11 @@ IntPtr unboxDouble
570580
delegate int EnumerableGetEnumeratorDelegate(int handle);
571581

572582
/*BEGIN DELEGATE TYPES*/
583+
delegate void ReleaseSystemDecimalDelegate(int handle);
584+
delegate int SystemDecimalConstructorSystemDoubleDelegate(double value);
585+
delegate int SystemDecimalConstructorSystemUInt64Delegate(ulong value);
586+
delegate int BoxDecimalDelegate(int valHandle);
587+
delegate int UnboxDecimalDelegate(int valHandle);
573588
delegate UnityEngine.Vector3 UnityEngineVector3ConstructorSystemSingle_SystemSingle_SystemSingleDelegate(float x, float y, float z);
574589
delegate UnityEngine.Vector3 UnityEngineVector3Methodop_AdditionUnityEngineVector3_UnityEngineVector3Delegate(ref UnityEngine.Vector3 a, ref UnityEngine.Vector3 b);
575590
delegate int BoxVector3Delegate(ref UnityEngine.Vector3 val);
@@ -641,6 +656,7 @@ public static void Open(int memorySize)
641656
{
642657
/*BEGIN STORE INIT CALLS*/
643658
NativeScript.Bindings.ObjectStore.Init(1000);
659+
NativeScript.Bindings.StructStore<System.Decimal>.Init(1000);
644660
/*END STORE INIT CALLS*/
645661

646662
// Allocate unmanaged memory
@@ -744,6 +760,11 @@ private static void OpenPlugin(InitMode initMode)
744760
Marshal.GetFunctionPointerForDelegate(new EnumerableGetEnumeratorDelegate(EnumerableGetEnumerator)),
745761
/*BEGIN INIT CALL*/
746762
1000,
763+
Marshal.GetFunctionPointerForDelegate(new ReleaseSystemDecimalDelegate(ReleaseSystemDecimal)),
764+
Marshal.GetFunctionPointerForDelegate(new SystemDecimalConstructorSystemDoubleDelegate(SystemDecimalConstructorSystemDouble)),
765+
Marshal.GetFunctionPointerForDelegate(new SystemDecimalConstructorSystemUInt64Delegate(SystemDecimalConstructorSystemUInt64)),
766+
Marshal.GetFunctionPointerForDelegate(new BoxDecimalDelegate(BoxDecimal)),
767+
Marshal.GetFunctionPointerForDelegate(new UnboxDecimalDelegate(UnboxDecimal)),
747768
Marshal.GetFunctionPointerForDelegate(new UnityEngineVector3ConstructorSystemSingle_SystemSingle_SystemSingleDelegate(UnityEngineVector3ConstructorSystemSingle_SystemSingle_SystemSingle)),
748769
Marshal.GetFunctionPointerForDelegate(new UnityEngineVector3Methodop_AdditionUnityEngineVector3_UnityEngineVector3Delegate(UnityEngineVector3Methodop_AdditionUnityEngineVector3_UnityEngineVector3)),
749770
Marshal.GetFunctionPointerForDelegate(new BoxVector3Delegate(BoxVector3)),
@@ -918,6 +939,118 @@ static int EnumerableGetEnumerator(int handle)
918939
}
919940

920941
/*BEGIN FUNCTIONS*/
942+
[MonoPInvokeCallback(typeof(ReleaseSystemDecimalDelegate))]
943+
static void ReleaseSystemDecimal(int handle)
944+
{
945+
try
946+
{
947+
if (handle != 0)
948+
{
949+
NativeScript.Bindings.StructStore<System.Decimal>.Remove(handle);
950+
}
951+
}
952+
catch (System.NullReferenceException ex)
953+
{
954+
UnityEngine.Debug.LogException(ex);
955+
NativeScript.Bindings.SetCsharpExceptionSystemNullReferenceException(NativeScript.Bindings.ObjectStore.Store(ex));
956+
}
957+
catch (System.Exception ex)
958+
{
959+
UnityEngine.Debug.LogException(ex);
960+
NativeScript.Bindings.SetCsharpException(NativeScript.Bindings.ObjectStore.Store(ex));
961+
}
962+
}
963+
964+
[MonoPInvokeCallback(typeof(SystemDecimalConstructorSystemDoubleDelegate))]
965+
static int SystemDecimalConstructorSystemDouble(double value)
966+
{
967+
try
968+
{
969+
var returnValue = NativeScript.Bindings.StructStore<System.Decimal>.Store(new System.Decimal(value));
970+
return returnValue;
971+
}
972+
catch (System.NullReferenceException ex)
973+
{
974+
UnityEngine.Debug.LogException(ex);
975+
NativeScript.Bindings.SetCsharpExceptionSystemNullReferenceException(NativeScript.Bindings.ObjectStore.Store(ex));
976+
return default(int);
977+
}
978+
catch (System.Exception ex)
979+
{
980+
UnityEngine.Debug.LogException(ex);
981+
NativeScript.Bindings.SetCsharpException(NativeScript.Bindings.ObjectStore.Store(ex));
982+
return default(int);
983+
}
984+
}
985+
986+
[MonoPInvokeCallback(typeof(SystemDecimalConstructorSystemUInt64Delegate))]
987+
static int SystemDecimalConstructorSystemUInt64(ulong value)
988+
{
989+
try
990+
{
991+
var returnValue = NativeScript.Bindings.StructStore<System.Decimal>.Store(new System.Decimal(value));
992+
return returnValue;
993+
}
994+
catch (System.NullReferenceException ex)
995+
{
996+
UnityEngine.Debug.LogException(ex);
997+
NativeScript.Bindings.SetCsharpExceptionSystemNullReferenceException(NativeScript.Bindings.ObjectStore.Store(ex));
998+
return default(int);
999+
}
1000+
catch (System.Exception ex)
1001+
{
1002+
UnityEngine.Debug.LogException(ex);
1003+
NativeScript.Bindings.SetCsharpException(NativeScript.Bindings.ObjectStore.Store(ex));
1004+
return default(int);
1005+
}
1006+
}
1007+
1008+
[MonoPInvokeCallback(typeof(BoxDecimalDelegate))]
1009+
static int BoxDecimal(int valHandle)
1010+
{
1011+
try
1012+
{
1013+
var val = (System.Decimal)NativeScript.Bindings.StructStore<System.Decimal>.Get(valHandle);
1014+
var returnValue = NativeScript.Bindings.ObjectStore.Store((object)val);
1015+
return returnValue;
1016+
}
1017+
catch (System.NullReferenceException ex)
1018+
{
1019+
UnityEngine.Debug.LogException(ex);
1020+
NativeScript.Bindings.SetCsharpExceptionSystemNullReferenceException(NativeScript.Bindings.ObjectStore.Store(ex));
1021+
return default(int);
1022+
}
1023+
catch (System.Exception ex)
1024+
{
1025+
UnityEngine.Debug.LogException(ex);
1026+
NativeScript.Bindings.SetCsharpException(NativeScript.Bindings.ObjectStore.Store(ex));
1027+
return default(int);
1028+
}
1029+
}
1030+
1031+
[MonoPInvokeCallback(typeof(UnboxDecimalDelegate))]
1032+
static int UnboxDecimal(int valHandle)
1033+
{
1034+
try
1035+
{
1036+
var val = NativeScript.Bindings.ObjectStore.Get(valHandle);
1037+
var returnValue = NativeScript.Bindings.StructStore<System.Decimal>.Store((System.Decimal)val);
1038+
return returnValue;
1039+
}
1040+
catch (System.NullReferenceException ex)
1041+
{
1042+
UnityEngine.Debug.LogException(ex);
1043+
NativeScript.Bindings.SetCsharpExceptionSystemNullReferenceException(NativeScript.Bindings.ObjectStore.Store(ex));
1044+
return default(int);
1045+
}
1046+
catch (System.Exception ex)
1047+
{
1048+
UnityEngine.Debug.LogException(ex);
1049+
NativeScript.Bindings.SetCsharpException(NativeScript.Bindings.ObjectStore.Store(ex));
1050+
return default(int);
1051+
}
1052+
}
1053+
9211054
[MonoPInvokeCallback(typeof(UnityEngineVector3ConstructorSystemSingle_SystemSingle_SystemSingleDelegate))]
9221055
static UnityEngine.Vector3 UnityEngineVector3ConstructorSystemSingle_SystemSingle_SystemSingle(float x, float y, float z)
9231056
{

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /