😎

そろそろ慣れたいUnityAction

2023/06/11に公開

毎回毎回、ググるのに疲れたので、ここに書いておきます

常用してないから、忘れるっていうのはわかっているので、どんどん使って行かないと

ググって出てきた見本をまとめてるだけなので、完全自分用

  • Delegate
  • Action
  • Func
  • UnityAction
  • Event
  • UnityEvent

を使ってログ出力するだけのスクリプトです。

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.UI;

public class DeAcEvTest : MonoBehaviour
{
    public delegate void Delegate1();
    public Delegate1 d1;

    public delegate void Delegate2(int i);
    public Delegate2 d2;

    public delegate int Delegate3(int i);
    public Delegate3 d3;
  
    // Actionは返り値が設定できない
    public Action<int> ac1;

    // Funcは返り値が設定できる<引数、返り値>
    public Func<int, int> fc1;

    public UnityAction<int> ua1;

    public event Action<int> ev1;

    public event Action<Collision> ev2;

    [SerializeField]
    UnityEvent ue1 = new UnityEvent();

    [SerializeField]
    UnityEvent<int> ue2 = new UnityEvent<int>();



    void Start()
    {
        // Delegate Start
        d1 = ShowDelegate1;
        d1 += ShowDelegate11;
        d1();

        d2 = ShowDelegate2;
        d2(100);

        d3 = ShowDelegate3;
        Debug.Log("Delegate 3:return value is " + d3(2));
        // Delegate End

        // Action Start
        ac1 = ShowAction1;
        ac1(10);
        // Action End

        // Func Start
        fc1 = ShowFunc1;
        Debug.Log("Func 1:return value is " + fc1(3));
        // Func End

        // UnityAction Start
        // 関数の型が同じなら、
        ua1 = ShowUnityAction1;
        ua1 += ShowAction1;
        ua1(20);
        // UnityAction End

        // Event Start
        ev1 = ShowEvent1;
        ev1(30);

        // 衝突したら実行される
        ev2 = ShowEvent2;
        // Event End

        // UnityEvent Start
        ue1.AddListener(ShowUnityEvent1);
        ue1.AddListener(ShowUnityEvent2);
        ue1?.Invoke();

        ue2.AddListener(ShowUnityEvent3);
        ue2.AddListener(ShowUnityEvent4);
        ue2?.Invoke(44);
        // UnityEvent End



    }

    // Delegate Start
    public void ShowDelegate1()
    {
        Debug.Log("Delegate 1");
    }

    public void ShowDelegate11()
    {
        Debug.Log("Delegate 11");
    }

    public void ShowDelegate2(int i)
    {
        Debug.Log("Delegate 2:parameter is " + i);
    }

    public int ShowDelegate3(int i)
    {
        Debug.Log("Delegate 3:parameter is " + i);

        return i * 2;
    }
    // Delegate End

    // Action Start
    public void ShowAction1(int i)
    {
        Debug.Log("Action 1:parameter is " + i);
    }
    // Action End

    // Func Start
    public int ShowFunc1(int i)
    {
        Debug.Log("Func 1:parameter is " + i);

        return i * 3;
    }
    // Func End

    // UnityAction Start
    public void ShowUnityAction1(int i)
    {
        Debug.Log("UnityAction 1:parameter is " + i);
    }
    // UnityAction End

    // Event Start
    public void ShowEvent1(int i)
    {
        Debug.Log("Event 1:parameter is " + i);
    }
    private void OnCollisionEnter(Collision collision)
    {
        ev2.Invoke(collision);
    }
    public void ShowEvent2(Collision collision)
    {
        Debug.Log(collision.gameObject.name);
    }
    // Event End

    // UnityEvent Start
    public void ShowUnityEvent1()
    {
        Debug.Log("UnityEvent 1");
    }
    public void ShowUnityEvent2()
    {
        Debug.Log("UnityEvent 2");
    }

    public void ShowUnityEvent3(int i)
    {
        Debug.Log("UnityEvent 3:parameter is " + i);
    }
    public void ShowUnityEvent4(int i)
    {
        Debug.Log("UnityEvent 4:parameter is " + i);
    }
    // UnityEvent End


}


出力結果

eventを特に使っていきたいのですが(特に、Collisionで動くevent)

あと、主に参考にした
https://taidanahibi.com/unity/delegate-event-unityaction-unityevent/
こちらにある

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events; //これ忘れずに!

//プレイヤークラス
public class Player : MonoBehaviour
{
    public delegate void OnDethDelegate(); //delegateの型を宣言
    public static event OnDethDelegate OnDethEvent; //eventの宣言 staticをつけて直接呼べるようにする

    
    public void Update()
    {
        //スペースキーを押したらDeth()を呼ぶ。

        if (Input.GetKey(KeyCode.Space))
        {
            Deth();
        }
    }

    //プレイヤーが死んだらGameOverのUIを発動させる。
    public void Deth()
    {
        //イベントを発動
        OnDethEvent?.Invoke();
        //こう書いてもいい
        //if (OnDethEvent != null)
        //{
        //    OnDethEvent.Invoke();
        //}s
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GameUI : MonoBehaviour
{
    //UIクラス

    //死んだらGameOverの表示をする
    public void DisplayGameOver()
    {
        gameObject.SetActive(true);
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PanelManager : MonoBehaviour
{
    //パネルを管理するクラス
    public GameUI gameUI;

    public void Start()
    {
        //イベント登録
        Player.OnDethEvent += OpenPanel;

        //↓ "="は使えないし、"event"を発動させることはできない
        //Player.OnDethEvent = OpenPanel;
        //Player.OnDethEvent();
    }
    //GameOverのパネルを表示させる関数をgameUIクラスから発動する
    public void OpenPanel()
    {
        gameUI.DisplayGameOver();
    }
}

これでプレイヤーがゲームオーバーを検知できる所を理解しなければ

Discussion