Skip to content

Commit 9141a06

Browse files
committed
Put the whole package in a namespace
Everything is now inside of a SerializableCallbacks namespace
1 parent 7ff19f1 commit 9141a06

11 files changed

+1153
-842
lines changed

Editor/SerializableCallbackDrawer.cs

Lines changed: 318 additions & 264 deletions
Large diffs are not rendered by default.
Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
using UnityEngine;
22
using System;
33

4-
/// <summary> Add to fields of your class extending SerializableCallbackBase<T,..> to limit which types can be assigned to it. </summary>
5-
public class TargetConstraintAttribute : PropertyAttribute {
6-
public Type targetType;
7-
4+
namespace SerializableCallbacks
5+
{
86
/// <summary> Add to fields of your class extending SerializableCallbackBase<T,..> to limit which types can be assigned to it. </summary>
9-
public TargetConstraintAttribute(Type targetType) {
10-
this.targetType = targetType;
7+
public class TargetConstraintAttribute : PropertyAttribute
8+
{
9+
public Type targetType;
10+
11+
/// <summary> Add to fields of your class extending SerializableCallbackBase<T,..> to limit which types can be assigned to it. </summary>
12+
public TargetConstraintAttribute(Type targetType)
13+
{
14+
this.targetType = targetType;
15+
}
1116
}
12-
}
17+
}

Runtime/InvokableCallback.cs

Lines changed: 126 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -3,126 +3,164 @@
33
using System.Collections.Generic;
44
using UnityEngine;
55

6-
public class InvokableCallback<TReturn> : InvokableCallbackBase<TReturn> {
6+
namespace SerializableCallbacks
7+
{
8+
public class InvokableCallback<TReturn> : InvokableCallbackBase<TReturn>
9+
{
710

8-
public Func<TReturn> func;
11+
public Func<TReturn> func;
912

10-
public TReturn Invoke() {
11-
return func();
12-
}
13+
public TReturn Invoke()
14+
{
15+
return func();
16+
}
1317

14-
public override TReturn Invoke(params object[] args) {
15-
return func();
16-
}
18+
public override TReturn Invoke(params object[] args)
19+
{
20+
return func();
21+
}
1722

18-
/// <summary> Constructor </summary>
19-
public InvokableCallback(object target, string methodName) {
20-
if (target == null || string.IsNullOrEmpty(methodName)) {
21-
func = () => default(TReturn);
22-
} else {
23-
func = (System.Func<TReturn>) System.Delegate.CreateDelegate(typeof(System.Func<TReturn>), target, methodName);
23+
/// <summary> Constructor </summary>
24+
public InvokableCallback(object target, string methodName)
25+
{
26+
if (target == null || string.IsNullOrEmpty(methodName))
27+
{
28+
func = () => default(TReturn);
29+
}
30+
else
31+
{
32+
func = (System.Func<TReturn>)System.Delegate.CreateDelegate(typeof(System.Func<TReturn>), target, methodName);
33+
}
2434
}
2535
}
26-
}
2736

28-
public class InvokableCallback<T0, TReturn> : InvokableCallbackBase<TReturn> {
37+
public class InvokableCallback<T0, TReturn> : InvokableCallbackBase<TReturn>
38+
{
2939

30-
public Func<T0, TReturn> func;
40+
public Func<T0, TReturn> func;
3141

32-
public TReturn Invoke(T0 arg0) {
33-
return func(arg0);
34-
}
42+
public TReturn Invoke(T0 arg0)
43+
{
44+
return func(arg0);
45+
}
3546

36-
public override TReturn Invoke(params object[] args) {
37-
// Convert from special "unity-nulls" to true null
38-
if (args[0] is UnityEngine.Object && (UnityEngine.Object) args[0] == null) args[0] = null;
39-
return func((T0) args[0]);
40-
}
47+
public override TReturn Invoke(params object[] args)
48+
{
49+
// Convert from special "unity-nulls" to true null
50+
if (args[0] is UnityEngine.Object && (UnityEngine.Object)args[0] == null) args[0] = null;
51+
return func((T0)args[0]);
52+
}
4153

42-
/// <summary> Constructor </summary>
43-
public InvokableCallback(object target, string methodName) {
44-
if (target == null || string.IsNullOrEmpty(methodName)) {
45-
func = x => default(TReturn);
46-
} else {
47-
func = (System.Func<T0, TReturn>) System.Delegate.CreateDelegate(typeof(System.Func<T0, TReturn>), target, methodName);
54+
/// <summary> Constructor </summary>
55+
public InvokableCallback(object target, string methodName)
56+
{
57+
if (target == null || string.IsNullOrEmpty(methodName))
58+
{
59+
func = x => default(TReturn);
60+
}
61+
else
62+
{
63+
func = (System.Func<T0, TReturn>)System.Delegate.CreateDelegate(typeof(System.Func<T0, TReturn>), target, methodName);
64+
}
4865
}
4966
}
50-
}
5167

52-
public class InvokableCallback<T0, T1, TReturn> : InvokableCallbackBase<TReturn> {
68+
public class InvokableCallback<T0, T1, TReturn> : InvokableCallbackBase<TReturn>
69+
{
5370

54-
public Func<T0, T1, TReturn> func;
71+
public Func<T0, T1, TReturn> func;
5572

56-
public TReturn Invoke(T0 arg0, T1 arg1) {
57-
return func(arg0, arg1);
58-
}
73+
public TReturn Invoke(T0 arg0, T1 arg1)
74+
{
75+
return func(arg0, arg1);
76+
}
5977

60-
public override TReturn Invoke(params object[] args) {
61-
// Convert from special "unity-nulls" to true null
62-
if (args[0] is UnityEngine.Object && (UnityEngine.Object) args[0] == null) args[0] = null;
63-
if (args[1] is UnityEngine.Object && (UnityEngine.Object) args[1] == null) args[1] = null;
64-
return func((T0) args[0], (T1) args[1]);
65-
}
78+
public override TReturn Invoke(params object[] args)
79+
{
80+
// Convert from special "unity-nulls" to true null
81+
if (args[0] is UnityEngine.Object && (UnityEngine.Object)args[0] == null) args[0] = null;
82+
if (args[1] is UnityEngine.Object && (UnityEngine.Object)args[1] == null) args[1] = null;
83+
return func((T0)args[0], (T1)args[1]);
84+
}
6685

67-
/// <summary> Constructor </summary>
68-
public InvokableCallback(object target, string methodName) {
69-
if (target == null || string.IsNullOrEmpty(methodName)) {
70-
func = (x, y) => default(TReturn);
71-
} else {
72-
func = (System.Func<T0, T1, TReturn>) System.Delegate.CreateDelegate(typeof(System.Func<T0, T1, TReturn>), target, methodName);
86+
/// <summary> Constructor </summary>
87+
public InvokableCallback(object target, string methodName)
88+
{
89+
if (target == null || string.IsNullOrEmpty(methodName))
90+
{
91+
func = (x, y) => default(TReturn);
92+
}
93+
else
94+
{
95+
func = (System.Func<T0, T1, TReturn>)System.Delegate.CreateDelegate(typeof(System.Func<T0, T1, TReturn>), target, methodName);
96+
}
7397
}
7498
}
75-
}
7699

77-
public class InvokableCallback<T0, T1, T2, TReturn> : InvokableCallbackBase<TReturn> {
100+
public class InvokableCallback<T0, T1, T2, TReturn> : InvokableCallbackBase<TReturn>
101+
{
78102

79-
public Func<T0, T1, T2, TReturn> func;
103+
public Func<T0, T1, T2, TReturn> func;
80104

81-
public TReturn Invoke(T0 arg0, T1 arg1, T2 arg2) {
82-
return func(arg0, arg1, arg2);
83-
}
105+
public TReturn Invoke(T0 arg0, T1 arg1, T2 arg2)
106+
{
107+
return func(arg0, arg1, arg2);
108+
}
84109

85-
public override TReturn Invoke(params object[] args) {
86-
// Convert from special "unity-nulls" to true null
87-
if (args[0] is UnityEngine.Object && (UnityEngine.Object) args[0] == null) args[0] = null;
88-
if (args[1] is UnityEngine.Object && (UnityEngine.Object) args[1] == null) args[1] = null;
89-
if (args[2] is UnityEngine.Object && (UnityEngine.Object) args[2] == null) args[2] = null;
90-
return func((T0) args[0], (T1) args[1], (T2) args[2]);
91-
}
110+
public override TReturn Invoke(params object[] args)
111+
{
112+
// Convert from special "unity-nulls" to true null
113+
if (args[0] is UnityEngine.Object && (UnityEngine.Object)args[0] == null) args[0] = null;
114+
if (args[1] is UnityEngine.Object && (UnityEngine.Object)args[1] == null) args[1] = null;
115+
if (args[2] is UnityEngine.Object && (UnityEngine.Object)args[2] == null) args[2] = null;
116+
return func((T0)args[0], (T1)args[1], (T2)args[2]);
117+
}
92118

93-
/// <summary> Constructor </summary>
94-
public InvokableCallback(object target, string methodName) {
95-
if (target == null || string.IsNullOrEmpty(methodName)) {
96-
func = (x, y, z) => default(TReturn);
97-
} else {
98-
func = (System.Func<T0, T1, T2, TReturn>) System.Delegate.CreateDelegate(typeof(System.Func<T0, T1, T2, TReturn>), target, methodName);
119+
/// <summary> Constructor </summary>
120+
public InvokableCallback(object target, string methodName)
121+
{
122+
if (target == null || string.IsNullOrEmpty(methodName))
123+
{
124+
func = (x, y, z) => default(TReturn);
125+
}
126+
else
127+
{
128+
func = (System.Func<T0, T1, T2, TReturn>)System.Delegate.CreateDelegate(typeof(System.Func<T0, T1, T2, TReturn>), target, methodName);
129+
}
99130
}
100131
}
101-
}
102132

103-
public class InvokableCallback<T0, T1, T2, T3, TReturn> : InvokableCallbackBase<TReturn> {
133+
public class InvokableCallback<T0, T1, T2, T3, TReturn> : InvokableCallbackBase<TReturn>
134+
{
104135

105-
public Func<T0, T1, T2, T3, TReturn> func;
136+
public Func<T0, T1, T2, T3, TReturn> func;
106137

107-
public TReturn Invoke(T0 arg0, T1 arg1, T2 arg2, T3 arg3) {
108-
return func(arg0, arg1, arg2, arg3);
109-
}
138+
public TReturn Invoke(T0 arg0, T1 arg1, T2 arg2, T3 arg3)
139+
{
140+
return func(arg0, arg1, arg2, arg3);
141+
}
110142

111-
public override TReturn Invoke(params object[] args) {
112-
// Convert from special "unity-nulls" to true null
113-
if (args[0] is UnityEngine.Object && (UnityEngine.Object) args[0] == null) args[0] = null;
114-
if (args[1] is UnityEngine.Object && (UnityEngine.Object) args[1] == null) args[1] = null;
115-
if (args[2] is UnityEngine.Object && (UnityEngine.Object) args[2] == null) args[2] = null;
116-
if (args[3] is UnityEngine.Object && (UnityEngine.Object) args[3] == null) args[3] = null;
117-
return func((T0) args[0], (T1) args[1], (T2) args[2], (T3) args[3]);
118-
}
143+
public override TReturn Invoke(params object[] args)
144+
{
145+
// Convert from special "unity-nulls" to true null
146+
if (args[0] is UnityEngine.Object && (UnityEngine.Object)args[0] == null) args[0] = null;
147+
if (args[1] is UnityEngine.Object && (UnityEngine.Object)args[1] == null) args[1] = null;
148+
if (args[2] is UnityEngine.Object && (UnityEngine.Object)args[2] == null) args[2] = null;
149+
if (args[3] is UnityEngine.Object && (UnityEngine.Object)args[3] == null) args[3] = null;
150+
return func((T0)args[0], (T1)args[1], (T2)args[2], (T3)args[3]);
151+
}
119152

120-
/// <summary> Constructor </summary>
121-
public InvokableCallback(object target, string methodName) {
122-
if (target == null || string.IsNullOrEmpty(methodName)) {
123-
func = (x, y, z, w) => default(TReturn);
124-
} else {
125-
func = (System.Func<T0, T1, T2, T3, TReturn>) System.Delegate.CreateDelegate(typeof(System.Func<T0, T1, T2, T3, TReturn>), target, methodName);
153+
/// <summary> Constructor </summary>
154+
public InvokableCallback(object target, string methodName)
155+
{
156+
if (target == null || string.IsNullOrEmpty(methodName))
157+
{
158+
func = (x, y, z, w) => default(TReturn);
159+
}
160+
else
161+
{
162+
func = (System.Func<T0, T1, T2, T3, TReturn>)System.Delegate.CreateDelegate(typeof(System.Func<T0, T1, T2, T3, TReturn>), target, methodName);
163+
}
126164
}
127165
}
128166
}

Runtime/InvokableCallbackBase.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1-
public abstract class InvokableCallbackBase<TReturn> {
2-
public abstract TReturn Invoke(params object[] args);
1+
namespace SerializableCallbacks
2+
{
3+
public abstract class InvokableCallbackBase<TReturn>
4+
{
5+
public abstract TReturn Invoke(params object[] args);
6+
}
37
}

0 commit comments

Comments
 (0)