c# – 将参数传递给UnityEvent

好的,我有点困惑.我通过this教程学习了UnityEvent和Messaging System.但我有一个问题,我无法理解.如何将参数传递给Invoking函数?

例如(我在教程中有EventManager):

void OnEnable() {
        EventManager.StartListening ("OnPlayerTeleport", TeleportPlayer);
    }

    void OnDisable() {
        EventManager.StopListening ("OnPlayerTeleport", TeleportPlayer);
    }

    void TeleportPlayer () {
        float yPos = transform.position.y;
        yPos += 20.0f;
        transform.position = new Vector3 (transform.position.x, yPos, transform.position.z);
    }

我有触发器:

void Update () {
    if (Input.GetButtonDown ("Teleport")) {
        EventManager.TriggerEvent ("OnPlayerTeleport");
    }
}

但是,如果我想将’height’值传递给’TeleportPlayer’函数怎么办:

void TeleportPlayer (float h) {
    float yPos = transform.position.y;
    yPos += h;
    transform.position = new Vector3 (transform.position.x, yPos, transform.position.z);
}

我怎样才能做到这一点?

解决方法:

使用C#delegate / Action而不是Unity的UnityEvent.它比Unity的活动更快.我几天前移植了it.您只需稍微修改一下即可获得所需内容.

1.通过将所有Action声明更改为Action< float>,使Action动作浮动这意味着它将允许具有float参数的函数.

2.现在,使TriggerEvent函数采用float参数.通过改变

public static void TriggerEvent(string eventName)

public static void TriggerEvent(string eventName, float h)

这是新的EventManager脚本.

using UnityEngine;
using System.Collections.Generic;
using System;

public class EventManager : MonoBehaviour
{

    private Dictionary<string, Action<float>> eventDictionary;

    private static EventManager eventManager;

    public static EventManager instance
    {
        get
        {
            if (!eventManager)
            {
                eventManager = FindObjectOfType(typeof(EventManager)) as EventManager;

                if (!eventManager)
                {
                    Debug.LogError("There needs to be one active EventManger script on a GameObject in your scene.");
                }
                else
                {
                    eventManager.Init();
                }
            }

            return eventManager;
        }
    }

    void Init()
    {
        if (eventDictionary == null)
        {
            eventDictionary = new Dictionary<string, Action<float>>();
        }
    }

    public static void StartListening(string eventName, Action<float> listener)
    {

        Action<float> thisEvent;
        if (instance.eventDictionary.TryGetValue(eventName, out thisEvent))
        {
            thisEvent += listener;
        }
        else
        {
            thisEvent += listener;
            instance.eventDictionary.Add(eventName, thisEvent);
        }
    }

    public static void StopListening(string eventName, Action<float> listener)
    {
        if (eventManager == null) return;
        Action<float> thisEvent;
        if (instance.eventDictionary.TryGetValue(eventName, out thisEvent))
        {
            thisEvent -= listener;
        }
    }

    public static void TriggerEvent(string eventName, float h)
    {
        Action<float> thisEvent = null;
        if (instance.eventDictionary.TryGetValue(eventName, out thisEvent))
        {
            thisEvent.Invoke(h);
        }
    }
}

测试:

public class Test : MonoBehaviour
{

    void OnEnable()
    {
        EventManager.StartListening("OnPlayerTeleport", TeleportPlayer);
    }

    void OnDisable()
    {
        EventManager.StopListening("OnPlayerTeleport", TeleportPlayer);
    }

    void Update()
    {
        if (Input.GetButtonDown("Teleport"))
        {
            EventManager.TriggerEvent("OnPlayerTeleport", 5);
        }
    }

    void TeleportPlayer(float h)
    {
        float yPos = transform.position.y;
        yPos += h;
        transform.position = new Vector3(transform.position.x, yPos, transform.position.z);
    }
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐