Advertisement
MarcinKubat

EVENTS - subscriber

Jun 5th, 2020
255
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.06 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using System; // musimy użyć to
  5.  
  6. public class TestingEventSubscriber : MonoBehaviour
  7. {
  8.     // Start is called before the first frame update
  9.     void Start()
  10.     {
  11.         // STRUKTURA PODPIĘCIA FUNKCJI POD EVENT
  12.         //
  13.        
  14.         // potrzebujemy referencji do naszego innego skryptu
  15.         TestingEvents testingEvents = GetComponent<TestingEvents>();
  16.         // z tą referencją mamy dostęp do naszego eventu:
  17.         testingEvents .OnSpacePressed += TestingEvents_OnSpacePressed; // tutaj mówimy, że funkcja TestingEvents_OnSpacePressed
  18.                                                                        // wykonuje się na event .OnSpacePressed
  19.                                                                        // do powiedzenia tego musimy mieć obiekt klasy w której jest event
  20.                                                                        //
  21.                                                                        // nie wiem, dlaczego "GetComponent<TestingEvents>();"
  22.                                                                        // sprawdzić, czy to musi być obiekt w którym jest event?
  23.  
  24.         // testujemy inny event
  25.         testingEvents .OnIntEvent += TestingEvents_WithDelegate;
  26.  
  27.  
  28.         // testujemy delegat Action bez parametrów
  29.         testingEvents .OnActionEvent += TestingEvents_Action;
  30.  
  31.         // testujemy delegat Action a parametrami
  32.         testingEvents .OnActionEventWithParameters += TestingEvents_ActionParameters;
  33.        
  34.     }
  35.  
  36.     private void TestingEvents_OnSpacePressed(object sender, EventArgs e)
  37.     {
  38.         Debug.Log("SPACE - Testing Event Subscriber");
  39.     }
  40.  
  41.         private void TestingEvents_WithDelegate(int i)
  42.     {
  43.         Debug.Log("SPACE - DELEGATE " + i);
  44.     }
  45.  
  46.         private void TestingEvents_Action()
  47.     {
  48.         Debug.Log("ACTION :3");
  49.     }
  50.  
  51.     private void TestingEvents_ActionParameters(bool _bool, int _int)
  52.     {
  53.         Debug.Log(_bool + " " + _int);
  54.     }
  55.  
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement