- Home /
How to assign a function to UnityEvent without the use of lambdas?
I have a piece of C# code which I am to convert into C++/CLI
for the purpose of connecting C#
with C++
. Here's the code:-
using UnityEngine;
using UnityEngine.Events;
public class A
{
public void Func1(int index)
{
}
public void Func2()
{
}
}
public class B : MonoBehaviour
{
UnityEvent event;
private void Start()
{
A aClass = new A();
event.AddListener(() => A.Func1(index)); // This event needs to be without lambdas
event.AddListener(A.Func2);
}
}
Since I am going to convert this code to C++/CLI
, I won't be able to use lambdas as they are not allowed. I also need to pass the specific index that is to be determined by Start()
, so I cannot access the index from other functions too.
EDIT: I can't use local functions too but since they are not allowed in C++/CLI
.
Answer by Monsoonexe · May 29, 2021 at 06:43 PM
Consider a local function:
using UnityEngine;
using UnityEngine.Events;
public class A
{
public void Func1(int index)
{
}
public void Func2()
{
}
}
public class B : MonoBehaviour
{
UnityEvent event;
int index;
private void Start()
{
A aClass = new A();
void LocalFunction()
{ //very similar in behaviour to a lambda, but is not anonymous.
aClass.Func1(index)
}
event.AddListener(LocalFunction);
}
}
@Monsoonexe I tried using local functions. They work fine in C#
but they too are not allowed in C++/CLI
.
Your answer
Follow this Question
Related Questions
Shared memory between c# and c++ 1 Answer
Importing C++/CLI DLL causes errors 0 Answers
Unity crushes while running dll functions(log+code) 0 Answers
Problem importing a mixed-mode (C++/CLI) library 2 Answers
Using Native C++ dll in Unity 1 Answer