Here's an example of how you would pass a method along as a parameter.
using UnityEngine;
using System.Collections;
public class Example : MonoBehaviour
{
public delegate void TestDelegate(); // This defines what type of method you're going to call.
public TestDelegate m_methodToCall; // This is the variable holding the method you're going to call.
void Start()
{
// Fill Delegate.
m_methodToCall = AwesomeExampleMethod; // Notice we don't use (); here. (that can be confusing)
// Call method, pass along delegate.
SimpleMethod( m_methodToCall );
}
// This method expects a TestDelegate variable, allowing us to pass a custom void method.
private void SimpleMethod(TestDelegate method)
{
Debug.Log( "I'm about to call a method" );
method();
}
// A random method that is compatible with TestDelegate (asks no parameters, returns void)
private void AwesomeExampleMethod()
{
Debug.Log( "Yay!" );
}
}
I hope that helps, if it did, please accept my answer.
If you need any details, let me know!
Best of luck!
Cheers,
ThePersister
↧