Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using System; // musimy użyć to
- public class TestingEvents : MonoBehaviour
- {
- public event EventHandler OnSpacePressed; //nazwa eventu "OnCośtam" np. "OnEnemyKilled"
- // testujrmy z delefatem przekazując zmienną tyu int
- public event TestEventDelegate OnIntEvent;
- public delegate void TestEventDelegate(int i);
- private int spaceCount;
- // testujemy z delegatem void (bez paramteru)
- public event Action OnActionEvent;
- // można również podać parametry, np:
- public event Action<bool, int> OnActionEventWithParameters;
- void Start()
- {
- }
- private void Update()
- {
- if(Input.GetKeyDown(KeyCode.Space)){
- //Space pressed! //jeżeli wciśnięto spację, uruchamiamy event OnSpacePressed
- OnSpacePressed?.Invoke(this, EventArgs.Empty); // "?.Invoke" zastępuje sprawdzenie czy ktoś tego słucha
- // "this, EventArgs.Empty" używamy gdy nie wysyłamy argumentów
- // w tym przykładzie event jest wywoływany w Update pod spacją,
- // ale można go dać w dowolnym innym miejscu
- // turaj testujemy co innego - delegata używamy aby przesłać obiekt lub wartość
- spaceCount++;
- OnIntEvent?.Invoke(spaceCount);
- // tutaj testujrmy delegat action (void)
- OnActionEvent?.Invoke();
- // action z parametrami
- OnActionEventWithParameters?.Invoke(false, 666);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement