Skip to content

Commit 94280fb

Browse files
Support delegates and Unity 2017.2.
1 parent 36c601f commit 94280fb

10 files changed

Lines changed: 3932 additions & 358 deletions

File tree

Unity/Assets/NativeScript/Bindings.cs

Lines changed: 791 additions & 31 deletions
Large diffs are not rendered by default.

Unity/Assets/NativeScript/Editor/GenerateBindings.cs

Lines changed: 1791 additions & 306 deletions
Large diffs are not rendered by default.

Unity/Assets/NativeScriptTypes.json

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -523,5 +523,53 @@
523523
{
524524
"Type": "UnityEngine.GradientColorKey"
525525
}
526+
],
527+
"Delegates": [
528+
{
529+
"Type": "System.Action"
530+
},
531+
{
532+
"Type": "System.Action`1",
533+
"GenericParams": [
534+
{
535+
"Types": [
536+
"System.Single"
537+
]
538+
}
539+
]
540+
},
541+
{
542+
"Type": "System.Action`2",
543+
"GenericParams": [
544+
{
545+
"Types": [
546+
"System.Single",
547+
"System.Single"
548+
],
549+
"MaxSimultaneous": 100
550+
}
551+
]
552+
},
553+
{
554+
"Type": "System.Func`3",
555+
"GenericParams": [
556+
{
557+
"Types": [
558+
"System.Int32",
559+
"System.Single",
560+
"System.Double"
561+
],
562+
"MaxSimultaneous": 50
563+
},
564+
{
565+
"Types": [
566+
"System.Int16",
567+
"System.Int32",
568+
"System.String"
569+
],
570+
"MaxSimultaneous": 25
571+
}
572+
]
573+
}
526574
]
527575
}

Unity/CppSource/Game/Game.cpp

Lines changed: 69 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,87 @@
99
/// </license>
1010

1111
#include "Bindings.h"
12+
#include <stdio.h>
1213

1314
using namespace System;
1415
using namespace UnityEngine;
1516

1617
void PrintPlatformDefines();
1718

19+
struct PlainAction : System::Action
20+
{
21+
void operator()() override
22+
{
23+
Debug::Log(String("PlainAction invoked"));
24+
}
25+
};
26+
27+
struct FloatAction : System::Action1<float>
28+
{
29+
void operator()(float param) override
30+
{
31+
Debug::Log(String("FloatAction invoked"));
32+
}
33+
};
34+
35+
struct MyClickHandler : System::Action2<float, float>
36+
{
37+
void operator()(float x, float y) override
38+
{
39+
Debug::Log(String("clicked"));
40+
}
41+
};
42+
43+
struct MyIntFloatDoubleFunc : System::Func3<int32_t, float, double>
44+
{
45+
double operator()(int32_t i, float f) override
46+
{
47+
Debug::Log(String("int float double Func invoked"));
48+
return 2.34;
49+
}
50+
};
51+
52+
struct FuncReturningString : System::Func3<int16_t, int32_t, String>
53+
{
54+
String operator()(int16_t s, int32_t i) override
55+
{
56+
Debug::Log(String("returning a string"));
57+
return String("returned from Func");
58+
}
59+
};
60+
1861
// Called when the plugin is initialized
1962
// This is mostly full of test code. Feel free to remove it all.
2063
void PluginMain()
2164
{
2265
PrintPlatformDefines();
2366
Debug::Log(String("Game booted up"));
24-
25-
GameObject go("GameObject with a TestScript");
67+
68+
MyClickHandler mch1;
69+
MyClickHandler mch2;
70+
mch1 += mch2;
71+
mch1.Invoke(123, 456);
72+
Debug::Log(String("Removed"));
73+
mch1 -= mch2;
74+
mch1.Invoke(123, 456);
75+
76+
MyIntFloatDoubleFunc mifdf;
77+
double d = mifdf.Invoke(123, 3.14f);
78+
char buf[1024];
79+
sprintf(buf, "%lf", d);
80+
Debug::Log(String(buf));
81+
82+
FuncReturningString frs;
83+
String str = frs.Invoke(11, 22);
84+
Debug::Log(str);
85+
86+
FloatAction fa;
87+
fa.Invoke(3.14f);
88+
89+
PlainAction pa;
90+
pa.Invoke();
91+
92+
GameObject go(String("GameObject with a TestScript"));
2693
go.AddComponent<MyGame::MonoBehaviours::TestScript>();
2794
}
2895

0 commit comments

Comments
 (0)