Last active
October 8, 2022 21:05
-
-
Save lukacat10/b1447e553e6b9d639fee5de00abdbfef to your computer and use it in GitHub Desktop.
Auto create variadic method overloads for extended unity events
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
count = 4 | |
thing_to_replace = "<T0>" | |
input = """public static bool HasPersistentListenerMethod<T0>(this UnityEvent<T0> uEvent, UnityAction<T0> action) | |
{ | |
var methodName = action.Method.Name; | |
for (int i = 0; i < uEvent.GetPersistentEventCount(); i++) | |
{ | |
if (uEvent.GetPersistentMethodName(i) == methodName) | |
{ | |
return true; | |
} | |
} | |
return false; | |
} | |
public static void SetEventListener<T0>(this UnityEvent<T0> uEvent, UnityAction<T0> action, int index = -1, Lifecycle lifecycle = Lifecycle.START) | |
{ | |
if (index == -1 && uEvent.HasPersistentListenerMethod(action)) | |
{ | |
return; | |
} | |
if (!Application.isEditor) | |
{ | |
if(Application.isPlaying && lifecycle != Lifecycle.AWAKE) | |
uEvent.AddListener(action); | |
return; | |
} | |
if (index == -1 && !uEvent.HasPersistentListenerMethod(action)) | |
index = uEvent.GetPersistentEventCount(); | |
var methodInfo = action.Method;// MethodInfoHelper.GetMethodInfo(() => action.Invoke((T0) GetDefault(typeof(T0)))); | |
var methodName = methodInfo.Name; | |
var has = uEvent.HasPersistentListener(index); | |
//var listenerDelegate = (UnityAction<T0>)methodInfo.CreateDelegate(methodInfo.DeclaringType); | |
if (has && methodName != uEvent.GetPersistentMethodName(index)) | |
{ | |
UnityEventTools.RegisterPersistentListener(uEvent, index, action);//uiManager.updateStylePoints); | |
} | |
else if (!has) | |
{ | |
UnityEventTools.AddPersistentListener(uEvent, action); // TODO: WARNING! some sources say it could be problematic on production builds! | |
} | |
}""" | |
def apply(argNumber: int): | |
if argNumber == 0: | |
return "" | |
chain = "<T0" | |
if argNumber > 1: | |
for i in range(2, argNumber+1): | |
chain += ", T" + str(i - 1) | |
return chain + ">" | |
def main(): | |
for i in range(count+1): | |
print(input.replace(thing_to_replace, apply(i))) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment