I'm new to Unity and I don't know very much about it. However, I've played around with Java and Objective-C in the past; two object-oriented C-based programming languages that are very much like the C# Unity uses.
Objective-C has blocks that are variables that allow you to store a function inside of them and call them later. When used in practice, you can use blocks as function parameters and class properties, saving a programmer "if
" statements and instead simply calling the block.
An example of that syntax would be (taken from the linked tutorial site):
// Declare a block variable
double (^distanceFromRateAndTime)(double rate, double time);
// Create and assign the block
distanceFromRateAndTime = ^double(double rate, double time) {
return rate * time;
};
// Call the block
double dx = distanceFromRateAndTime(35, 1.5);
NSLog(@"A car driving 35 mph will travel %.2f miles in 1.5 hours.", dx);
Java has the Runnable
class, which is the basis of its multithreading structure. (Thread
inherits from Runnable
.) I learned about that class from a previous question I asked. There are two ways of making a Runnable
: The Java 8 way of using lambdas...
Runnable r = () -> { System.out.println("This is just an example!"); }
The Java 8 using lambdas as a function parameter...
void someFunction(Runnable r) {
r.run();
}
// In a method, not sure about the semicolons
someFunction( () -> { System.out.println("This will work, too!"); });
And finally, pre-Java 8...
Runnable r = Runnable(){
@Override
public void run() {
System.out.println("Yet another example");
}
}
My question is, how would I go about achieving this in Unity's variant of C#?