[내일 배움 캠프 Unity 4기] W4D4 05.09 TIL

김용준·2024년 5월 9일
0

내일배움캠프

목록 보기
11/47

Goals

  • Learn and practice the concept of delegate and Anonymous function
  • Follow the program through the lecture

delegate in C#

The method, so called as function in C#, can be addressed like a variables via delegate. Even though the script could be written without the delegate, we should study the other expression of same features. One of the method to handle the function is delegate which can be facilitated in wide area; as a parameter of specific function, serialize the function in order, also for the grouping the method.
For the beginner, I write the following example to use delegate

delegate int MyDelegate(int a, int b); 
// first 'delegate' means the following method is considered as delegate
// second 'int' means the return type of method
// third 'MyDelegate' is the indicator
// forth 'int a, int b' is the parameters of the function

// sample functions
int Add(int a, int b) {Console.WriteLine("Add"); return a+b;}
int Sub(int a, int b) {Console.WriteLine("Sub"); return a-b;}

// the function requires delegates as a parameter
int Calc(int a, int b, MyDelegate del)
{
  int res = del(a,b);
  Console.WriteLine(res);
  return res;
}

static void Main(string[] args)
{
  // declare and initialize the delegate with 'Add' function
  var myDel = new MyDelegate(Add);
  
  // also other function could be attached with the indicator
  myDel += Sub
  
  // Now, the 'myDel' contains {Add,Sub} methods
  // Call the function which contains parameter of delegate
  Calc(5,3,myDel);
  
  /* The output in console could be
  Add
  Sub
  2
  Because the 'Calc' method gave the last method in elements
  */
}

Lamda expression(Anomynous func) in C#

For the developer who want to express the function in limited line(or who is lazy to name the method), the anomynous function(lambda expression) is regarded as one of choices. This method cannot be used independently. With the delegate or Func, the true features can be revealed.

// define delegate to contain anomynous function
delegate int MyDelegate(int a, int b);

static void Main(string[] args)
{
  // declare the delegate with initialization
  // the 2nd term, behind the `=` is the lambda expression
  // first (x,y) is the parameter of method
  // `=>` is the indicator of processing
  // (x+y) is the return method of delegate
  MyDelegate del = (x,y) => (x+y);
  
  // Use 'Function' in dotNet framework
  // Unlike the delegate, the 'Function' requires return type at the end of generics
  // the 'Func' is the type of method which can contains method
  // the generic '<int,int,int>' is the types of variable
  // the third int is the return type
  Func<int,int,int> func = (x,y) => (x+y);
  
  // print out commands
  Console.WriteLine(del(2,5));
  Console.WriteLine(func(2,5));
  
  /* Output in console could be
  7 // delegate with anomynous
  7 // function with anomynous
  */
}

Pallete on Unity


There's a feature to manage the tile map in unity. The tile pallete is the method I've learned on today. With the tile image on pallete, we can draw the map by picking the tiles. the tools shown in upper side of 'CollWall', 7 different images with its feature, is the method to control the tiles on 'Scene'. the tools on the upper right side of pallete has the features to control the pallete. By practicing the function one by one, I've learned the usage. The map decorated by pallete can be seen in the bottom

profile
꿈이큰개발자지망생

0개의 댓글