Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Put the whole package in a namespace #31

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{
}
558 changes: 280 additions & 278 deletions Editor/SerializableCallbackDrawer.cs

Large diffs are not rendered by default.

16 changes: 9 additions & 7 deletions Runtime/Attributes/TargetConstraintAttribute.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
using UnityEngine;
using System;

/// <summary> Add to fields of your class extending SerializableCallbackBase<T,..> to limit which types can be assigned to it. </summary>
public class TargetConstraintAttribute : PropertyAttribute {
public Type targetType;

namespace SerializableCallback {
/// <summary> Add to fields of your class extending SerializableCallbackBase<T,..> to limit which types can be assigned to it. </summary>
public TargetConstraintAttribute(Type targetType) {
this.targetType = targetType;
public class TargetConstraintAttribute : PropertyAttribute {
public Type targetType;

/// <summary> Add to fields of your class extending SerializableCallbackBase<T,..> to limit which types can be assigned to it. </summary>
public TargetConstraintAttribute(Type targetType) {
this.targetType = targetType;
}
}
}
}
178 changes: 90 additions & 88 deletions Runtime/InvokableCallback.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,126 +3,128 @@
using System.Collections.Generic;
using UnityEngine;

public class InvokableCallback<TReturn> : InvokableCallbackBase<TReturn> {
namespace SerializableCallback {
public class InvokableCallback<TReturn> : InvokableCallbackBase<TReturn> {

public Func<TReturn> func;
public Func<TReturn> func;

public TReturn Invoke() {
return func();
}
public TReturn Invoke() {
return func();
}

public override TReturn Invoke(params object[] args) {
return func();
}
public override TReturn Invoke(params object[] args) {
return func();
}

/// <summary> Constructor </summary>
public InvokableCallback(object target, string methodName) {
if (target == null || string.IsNullOrEmpty(methodName)) {
func = () => default(TReturn);
} else {
func = (System.Func<TReturn>) System.Delegate.CreateDelegate(typeof(System.Func<TReturn>), target, methodName);
/// <summary> Constructor </summary>
public InvokableCallback(object target, string methodName) {
if (target == null || string.IsNullOrEmpty(methodName)) {
func = () => default(TReturn);
} else {
func = (System.Func<TReturn>) System.Delegate.CreateDelegate(typeof(System.Func<TReturn>), target, methodName);
}
}
}
}

public class InvokableCallback<T0, TReturn> : InvokableCallbackBase<TReturn> {
public class InvokableCallback<T0, TReturn> : InvokableCallbackBase<TReturn> {

public Func<T0, TReturn> func;
public Func<T0, TReturn> func;

public TReturn Invoke(T0 arg0) {
return func(arg0);
}
public TReturn Invoke(T0 arg0) {
return func(arg0);
}

public override TReturn Invoke(params object[] args) {
// Convert from special "unity-nulls" to true null
if (args[0] is UnityEngine.Object && (UnityEngine.Object) args[0] == null) args[0] = null;
return func((T0) args[0]);
}
public override TReturn Invoke(params object[] args) {
// Convert from special "unity-nulls" to true null
if (args[0] is UnityEngine.Object && (UnityEngine.Object) args[0] == null) args[0] = null;
return func((T0) args[0]);
}

/// <summary> Constructor </summary>
public InvokableCallback(object target, string methodName) {
if (target == null || string.IsNullOrEmpty(methodName)) {
func = x => default(TReturn);
} else {
func = (System.Func<T0, TReturn>) System.Delegate.CreateDelegate(typeof(System.Func<T0, TReturn>), target, methodName);
/// <summary> Constructor </summary>
public InvokableCallback(object target, string methodName) {
if (target == null || string.IsNullOrEmpty(methodName)) {
func = x => default(TReturn);
} else {
func = (System.Func<T0, TReturn>) System.Delegate.CreateDelegate(typeof(System.Func<T0, TReturn>), target, methodName);
}
}
}
}

public class InvokableCallback<T0, T1, TReturn> : InvokableCallbackBase<TReturn> {
public class InvokableCallback<T0, T1, TReturn> : InvokableCallbackBase<TReturn> {

public Func<T0, T1, TReturn> func;
public Func<T0, T1, TReturn> func;

public TReturn Invoke(T0 arg0, T1 arg1) {
return func(arg0, arg1);
}
public TReturn Invoke(T0 arg0, T1 arg1) {
return func(arg0, arg1);
}

public override TReturn Invoke(params object[] args) {
// Convert from special "unity-nulls" to true null
if (args[0] is UnityEngine.Object && (UnityEngine.Object) args[0] == null) args[0] = null;
if (args[1] is UnityEngine.Object && (UnityEngine.Object) args[1] == null) args[1] = null;
return func((T0) args[0], (T1) args[1]);
}
public override TReturn Invoke(params object[] args) {
// Convert from special "unity-nulls" to true null
if (args[0] is UnityEngine.Object && (UnityEngine.Object) args[0] == null) args[0] = null;
if (args[1] is UnityEngine.Object && (UnityEngine.Object) args[1] == null) args[1] = null;
return func((T0) args[0], (T1) args[1]);
}

/// <summary> Constructor </summary>
public InvokableCallback(object target, string methodName) {
if (target == null || string.IsNullOrEmpty(methodName)) {
func = (x, y) => default(TReturn);
} else {
func = (System.Func<T0, T1, TReturn>) System.Delegate.CreateDelegate(typeof(System.Func<T0, T1, TReturn>), target, methodName);
/// <summary> Constructor </summary>
public InvokableCallback(object target, string methodName) {
if (target == null || string.IsNullOrEmpty(methodName)) {
func = (x, y) => default(TReturn);
} else {
func = (System.Func<T0, T1, TReturn>) System.Delegate.CreateDelegate(typeof(System.Func<T0, T1, TReturn>), target, methodName);
}
}
}
}

public class InvokableCallback<T0, T1, T2, TReturn> : InvokableCallbackBase<TReturn> {
public class InvokableCallback<T0, T1, T2, TReturn> : InvokableCallbackBase<TReturn> {

public Func<T0, T1, T2, TReturn> func;
public Func<T0, T1, T2, TReturn> func;

public TReturn Invoke(T0 arg0, T1 arg1, T2 arg2) {
return func(arg0, arg1, arg2);
}
public TReturn Invoke(T0 arg0, T1 arg1, T2 arg2) {
return func(arg0, arg1, arg2);
}

public override TReturn Invoke(params object[] args) {
// Convert from special "unity-nulls" to true null
if (args[0] is UnityEngine.Object && (UnityEngine.Object) args[0] == null) args[0] = null;
if (args[1] is UnityEngine.Object && (UnityEngine.Object) args[1] == null) args[1] = null;
if (args[2] is UnityEngine.Object && (UnityEngine.Object) args[2] == null) args[2] = null;
return func((T0) args[0], (T1) args[1], (T2) args[2]);
}
public override TReturn Invoke(params object[] args) {
// Convert from special "unity-nulls" to true null
if (args[0] is UnityEngine.Object && (UnityEngine.Object) args[0] == null) args[0] = null;
if (args[1] is UnityEngine.Object && (UnityEngine.Object) args[1] == null) args[1] = null;
if (args[2] is UnityEngine.Object && (UnityEngine.Object) args[2] == null) args[2] = null;
return func((T0) args[0], (T1) args[1], (T2) args[2]);
}

/// <summary> Constructor </summary>
public InvokableCallback(object target, string methodName) {
if (target == null || string.IsNullOrEmpty(methodName)) {
func = (x, y, z) => default(TReturn);
} else {
func = (System.Func<T0, T1, T2, TReturn>) System.Delegate.CreateDelegate(typeof(System.Func<T0, T1, T2, TReturn>), target, methodName);
/// <summary> Constructor </summary>
public InvokableCallback(object target, string methodName) {
if (target == null || string.IsNullOrEmpty(methodName)) {
func = (x, y, z) => default(TReturn);
} else {
func = (System.Func<T0, T1, T2, TReturn>) System.Delegate.CreateDelegate(typeof(System.Func<T0, T1, T2, TReturn>), target, methodName);
}
}
}
}

public class InvokableCallback<T0, T1, T2, T3, TReturn> : InvokableCallbackBase<TReturn> {
public class InvokableCallback<T0, T1, T2, T3, TReturn> : InvokableCallbackBase<TReturn> {

public Func<T0, T1, T2, T3, TReturn> func;
public Func<T0, T1, T2, T3, TReturn> func;

public TReturn Invoke(T0 arg0, T1 arg1, T2 arg2, T3 arg3) {
return func(arg0, arg1, arg2, arg3);
}
public TReturn Invoke(T0 arg0, T1 arg1, T2 arg2, T3 arg3) {
return func(arg0, arg1, arg2, arg3);
}

public override TReturn Invoke(params object[] args) {
// Convert from special "unity-nulls" to true null
if (args[0] is UnityEngine.Object && (UnityEngine.Object) args[0] == null) args[0] = null;
if (args[1] is UnityEngine.Object && (UnityEngine.Object) args[1] == null) args[1] = null;
if (args[2] is UnityEngine.Object && (UnityEngine.Object) args[2] == null) args[2] = null;
if (args[3] is UnityEngine.Object && (UnityEngine.Object) args[3] == null) args[3] = null;
return func((T0) args[0], (T1) args[1], (T2) args[2], (T3) args[3]);
}
public override TReturn Invoke(params object[] args) {
// Convert from special "unity-nulls" to true null
if (args[0] is UnityEngine.Object && (UnityEngine.Object) args[0] == null) args[0] = null;
if (args[1] is UnityEngine.Object && (UnityEngine.Object) args[1] == null) args[1] = null;
if (args[2] is UnityEngine.Object && (UnityEngine.Object) args[2] == null) args[2] = null;
if (args[3] is UnityEngine.Object && (UnityEngine.Object) args[3] == null) args[3] = null;
return func((T0) args[0], (T1) args[1], (T2) args[2], (T3) args[3]);
}

/// <summary> Constructor </summary>
public InvokableCallback(object target, string methodName) {
if (target == null || string.IsNullOrEmpty(methodName)) {
func = (x, y, z, w) => default(TReturn);
} else {
func = (System.Func<T0, T1, T2, T3, TReturn>) System.Delegate.CreateDelegate(typeof(System.Func<T0, T1, T2, T3, TReturn>), target, methodName);
/// <summary> Constructor </summary>
public InvokableCallback(object target, string methodName) {
if (target == null || string.IsNullOrEmpty(methodName)) {
func = (x, y, z, w) => default(TReturn);
} else {
func = (System.Func<T0, T1, T2, T3, TReturn>) System.Delegate.CreateDelegate(typeof(System.Func<T0, T1, T2, T3, TReturn>), target, methodName);
}
}
}
}
6 changes: 4 additions & 2 deletions Runtime/InvokableCallbackBase.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
public abstract class InvokableCallbackBase<TReturn> {
public abstract TReturn Invoke(params object[] args);
namespace SerializableCallback {
public abstract class InvokableCallbackBase<TReturn> {
public abstract TReturn Invoke(params object[] args);
}
}
Loading