0.1Unity Event的几种用法

第一种用法新建2个class

//在PlayerContol  update-这边调用 if( Input.GetMouseButtonDown(1))
// trigger movement event
                player.movementByVelocityEvent.CallMovementByVelocityEvent(direction, moveSpeed);

MovementToPosition

using System;
using UnityEngine;

[RequireComponent(typeof(Rigidbody2D))]
[RequireComponent(typeof(MovementToPositionEvent))]
[DisallowMultipleComponent]
public class MovementToPosition : MonoBehaviour
{
    private Rigidbody2D rigidBody2D;
    private MovementToPositionEvent MovementToPositionEvent;
    private void Awake()
    {
        // Load components
        rigidBody2D = GetComponent<Rigidbody2D>();
        MovementToPositionEvent = GetComponent<MovementToPositionEvent>();
    }

    private void OnEnable()
    {
        MovementToPositionEvent.OnMovementToPosition += MovementToPositionEvent_OnMovementToPosition;
    }
    
    private void OnDisable()
    {
        MovementToPositionEvent.OnMovementToPosition -= MovementToPositionEvent_OnMovementToPosition;
    }

    private void MovementToPositionEvent_OnMovementToPosition(MovementToPositionEvent movementToPositionEvent, MovementToPositionArgs arg2)
    {
        MoveRigidbody(arg2.movePosition, arg2.currentPosition, arg2.moveSpeed);
    }

    private void MoveRigidbody(Vector3 movePosition, Vector3 currentPosition, float moveSpeed)
    {
        Vector2 unitVector = Vector3.Normalize(movePosition - currentPosition);
        rigidBody2D.MovePosition(rigidBody2D.position + (unitVector * moveSpeed * Time.fixedDeltaTime));
    }
}

MovementToPositionEvent

using System;
using UnityEngine;

[DisallowMultipleComponent]
public class MovementToPositionEvent : MonoBehaviour
{
    public event Action<MovementToPositionEvent, MovementToPositionArgs> OnMovementToPosition;

    public void CallMovementToPositionEvent(Vector3 movePosition,Vector3 currentPosition,float moveSpeed,Vector2 moveDirection,bool isRolling)
    {
        OnMovementToPosition?.Invoke(this, new MovementToPositionArgs(){ movePosition = movePosition,currentPosition = currentPosition,moveSpeed = moveSpeed,
            moveDirection = moveDirection,isRolling = isRolling
        } );
    }
}
public class MovementToPositionArgs : EventArgs
{
    public Vector3 movePosition;
    public Vector3 currentPosition;
    public float moveSpeed;
    public Vector2 moveDirection;
    public bool isRolling;
}

在这里插入图片描述

第二种用法UnityEvent,Action,EventHandler

using UnityEngine;


    public class EventForTestSubscriber : MonoBehaviour
    {
        private EventForTest eventForTest;
        private void Start()
        {
            eventForTest = GetComponent<EventForTest>();
            eventForTest.OnSpacePressed += EventForTest_OnSpacePressed;
            eventForTest.OnActionEvent += EventForTest_OnActionEvent;
            //eventForTest.onUnityEvent.AddListener(UnityEventMethod);
        }
        public void SpawnDamageText(float damageAmount)
        {
            Debug.Log("UnityEventMethod+    You take " + damageAmount + " damage");
        }

        private void EventForTest_OnActionEvent(int obj)
        {
            Debug.Log($"EventForTest_OnActionEvent: {obj}");
        }

        private void EventForTest_OnSpacePressed(object sender, EventForTest.OnSpacePressedEventArgs e)
        {
            Debug.Log($"EventForTest_OnSpacePressed: {e.spaceCountArgs}");
        }
    }


using System;
using UnityEngine;
using UnityEngine.Events;

public class EventForTest : MonoBehaviour
{
    public event EventHandler<OnSpacePressedEventArgs> OnSpacePressed;
    public event Action<int> OnActionEvent;
    public UnityEvent<float> OnUnityEvent;
    [SerializeField] private float damage = 5;
    private int spaceCount;
    private void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space))
        {
            spaceCount++;
            OnSpacePressed?.Invoke(this, new OnSpacePressedEventArgs { spaceCountArgs = spaceCount });
            OnActionEvent?.Invoke(spaceCount);
            OnUnityEvent?.Invoke(damage);

        }
    }
    public class OnSpacePressedEventArgs : EventArgs
    {
        public int spaceCountArgs;
    }
}

在这里插入图片描述

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值