Unity3d 统一自定义触发事件

Unity3d 统一自定义触发事件,unity3d,events,Unity3d,Events,我是新来的Unity 我在场景中有多个对象(都共享同一脚本)和播放器对象(自己的脚本) 我想实现一个定制的监听器,当单个对象执行某些操作时,它会通知玩家(假设某个int值为10) 有人能给我小费吗 谢谢我建议使用。让事件触发一个调用玩家脚本中函数的函数 (来自链接) 事件管理器 using UnityEngine; using System.Collections; public class EventManager : MonoBehaviour { public delegate

我是新来的
Unity

我在场景中有多个对象(都共享同一脚本)和播放器对象(自己的脚本)

我想实现一个定制的监听器,当单个对象执行某些操作时,它会通知玩家(假设某个int值为10)

有人能给我小费吗

谢谢

我建议使用。让事件触发一个调用玩家脚本中函数的函数

(来自链接)

事件管理器

using UnityEngine;
using System.Collections;

public class EventManager : MonoBehaviour 
{
    public delegate void ClickAction();
    public static event ClickAction OnClicked;

    
    void OnGUI()
    {
        if(GUI.Button(new Rect(Screen.width / 2 - 50, 5, 100, 30), "Click"))
        {
            if(OnClicked != null)
                OnClicked();
        }
    }
}
using UnityEngine;
using System.Collections;

public class TeleportScript : MonoBehaviour 
{
    void OnEnable()
    {
        EventManager.OnClicked += Teleport;
    }
    
    
    void OnDisable()
    {
        EventManager.OnClicked -= Teleport;
    }
    
    
    void Teleport()
    {
        Vector3 pos = transform.position;
        pos.y = Random.Range(1.0f, 3.0f);
        transform.position = pos;
    }
}
TeleportScript(使用EventManager中的事件)

我建议使用。让事件触发一个调用玩家脚本中函数的函数

(来自链接)

事件管理器

using UnityEngine;
using System.Collections;

public class EventManager : MonoBehaviour 
{
    public delegate void ClickAction();
    public static event ClickAction OnClicked;

    
    void OnGUI()
    {
        if(GUI.Button(new Rect(Screen.width / 2 - 50, 5, 100, 30), "Click"))
        {
            if(OnClicked != null)
                OnClicked();
        }
    }
}
using UnityEngine;
using System.Collections;

public class TeleportScript : MonoBehaviour 
{
    void OnEnable()
    {
        EventManager.OnClicked += Teleport;
    }
    
    
    void OnDisable()
    {
        EventManager.OnClicked -= Teleport;
    }
    
    
    void Teleport()
    {
        Vector3 pos = transform.position;
        pos.y = Random.Range(1.0f, 3.0f);
        transform.position = pos;
    }
}
TeleportScript(使用EventManager中的事件)


您确实希望使用事件,以便任何实体都可以向任何其他实体广播信息:

public class BroadcastingEntity : MonoBehaviour
{
     public static event Action<BroadcastingEntity> RaiseLowHealth,
     protected void OnLowHealth()
     {
          if(RaiseLowHealth != null) { RaiseLowHealth(this); }
     }
     public static event Action<BroadcastingEntity> RaiseDeath,
     protected void OnDeath()
     {
          if(RaiseDeath!= null) { RaiseDeath(this); }
     }
}
事件是静态的,但参数是对触发事件的实体的引用。所以,您可以打印名称或在其上获取其他组件。 你可以做:

    private void EntityLowHealth(BroadcastingEntity entity)
    {
         print(entity.GetType());
    }

这将给出实体的实际子类型,因此您可以执行所有类型的操作

您确实希望使用事件,以便任何实体都可以向任何其他实体广播信息:

public class BroadcastingEntity : MonoBehaviour
{
     public static event Action<BroadcastingEntity> RaiseLowHealth,
     protected void OnLowHealth()
     {
          if(RaiseLowHealth != null) { RaiseLowHealth(this); }
     }
     public static event Action<BroadcastingEntity> RaiseDeath,
     protected void OnDeath()
     {
          if(RaiseDeath!= null) { RaiseDeath(this); }
     }
}
事件是静态的,但参数是对触发事件的实体的引用。所以,您可以打印名称或在其上获取其他组件。 你可以做:

    private void EntityLowHealth(BroadcastingEntity entity)
    {
         print(entity.GetType());
    }

这将给出实体的实际子类型,因此您可以执行所有类型的操作

谢谢您的回答,我需要将发送此事件的gameobject作为参数传递。在
代理
远程传送
中传递参数是一种很好的做法吗?是的,应该可以。谢谢回答,我需要作为发送此事件的
游戏对象
的参数传递。在
委托
远程传送
中传递参数是一种良好的做法吗?是的,应该可以。