示例#1
0
 public FloatOperationDelegate fod; // A delegate field
 void Awake()
 {
     // Assign the method FloatAdd() to fod
     fod = FloatAdd;
     // Add the method FloatMultiply(), now BOTH are called by fod
     fod += FloatMultiply;
     // Check to see whether fod is null before calling
     if (fod != null)
     {
         // Call fod(3,4); it calls FloatAdd(3,4) & then FloatMultiply(3,4)
         float result = fod(3, 4);
         // Prints: The sum of 3 & 4 is 7.
         // then Prints: The product of 3 & 4 is 12.
         print(result);
         // Prints: 12
         // Thie result is 12 because the last target method to be called
         // is the one that returns a value via the delegate.
     }
 }
示例#2
0
 void Awake()
 {
     // Assign the method FloatAdd() to fod
     fod = FloatAdd;
     // Add the method FloatMultiply(), now BOTH are called by fod
     fod += FloatMultiply;
     // Check to see whether fod is null before calling
     if (fod != null)
     {
         // Call fod(3,4); it calls FloatAdd(3,4) & then FloatMultiply(3,4)
         float result = fod(3, 4);
         // Prints: The sum of 3 & 4 is 7.
         // then Prints: The product of 3 & 4 is 12.
         print(result);
         // Prints: 12
         // Thie result is 12 because the last target method to be called
         // is the one that returns a value via the delegate.
     }
 }