gpt4 book ai didi

c# - Unity3D_2019 : Adding EventTriggers at runtime

转载 作者:行者123 更新时间:2023-12-02 00:04:15 25 4
gpt4 key购买 nike

在 Unity3D 2017 版中,您可以通过执行以下操作一次添加多个 EventTriggers:

for (int i = 0; i < 20; i++)
{
EventTrigger.Entry pDown = new EventTrigger.Entry();
pDown.eventID = EventTriggerType.PointerDown;
pDown.callback.AddListener((eventdata) => { InventorySlotCopy(); });
slots[i].GetComponent<EventTrigger>().triggers.Add(pDown);

EventTrigger.Entry pUp = new EventTrigger.Entry();
pUp.eventID = EventTriggerType.PointerUp;
pUp.callback.AddListener((eventdata) => { InventorySlotInsert(); });
slots[i].GetComponent<EventTrigger>().triggers.Add(pUp);
}

slots 只是 GameObjectsarray,每个都有一个 Image 和一个 EventTrigger 附加到它。

但是,在 Unity3D 版本 2019 中使用与上面相同的代码会导致添加那些 EventTriggers 但不会将 functions 分配给 Listener .

enter image description here

Unity2019 是怎么做到的?

最佳答案

首先:

您将看不到检查器中添加的监听器!

在 Inspector 中,您只能看到永久 监听器,而不是在运行时添加的监听器!这些基本上是 UnityEvent<BaseEventData> 所以看Manual: UnityEvents

When a UnityEvent is added to a MonoBehaviour it appears in the Inspector and persistent callbacks can be added.

添加永久 监听器的唯一方法是通过检查器或自定义 EditorScript!但这无论如何都不是您想在这里做的。


我会稍微修改您的脚本以使其更安全。我只是在方法被调用时记录一些消息,以表明它们被调用了。

public class Example : MonoBehaviour
{
// Rather make these directly of type EventTrigger
// So you don't have to use GetComponent later and can be sure that these array only receives
// objects that actually have that Component attached!
public EventTrigger[] slots;

private void Start()
{
// Instead of a hardcoded index either iterate using slots.Length
// or simply directly foreach
foreach (var slot in slots)
{
var pDown = new EventTrigger.Entry
{
eventID = EventTriggerType.PointerDown
};

pDown.callback.AddListener(eventData => { InventorySlotCopy(); });
slot.triggers.Add(pDown);

var pUp = new EventTrigger.Entry
{
eventID = EventTriggerType.PointerUp
};

pUp.callback.AddListener(eventData => { InventorySlotInsert(); });
slot.triggers.Add(pUp);
}
}

private void InventorySlotCopy()
{
Debug.Log(nameof(InventorySlotCopy));
}

private void InventorySlotInsert()
{
Debug.Log(nameof(InventorySlotInsert));
}
}

其余的取决于您的设置。我们不知道你的对象是什么 slots是,但基本上有两(三)种选择:

选项 1 - 用户界面

如果这些槽对象是UI Canvas 中的元素例如ImageText .

这似乎是您的具体情况。

  1. 确保有一个 EventSystem出现在现场。

    通常在创建第一个 Canvas 时创建一个.

    如果不是现在通过 Hierachy View 创建它→ 右键单击UIEventSystem

  2. 确保您有选项 RayCast Target在您广告位的 UI 组件上启用

结果:

enter image description here


选项 2 - 3D 对象(选项 3 - 带 2D 碰撞体的 3D 对象)

只是为其他用户添加这个。

如果这些对象不是 UI 元素而是 3D 对象,请确保

  1. 您同样需要 EventSystem和以前一样

  2. 插槽有 Collider附加到相同的组件 GameObject EvenTrigger组件附加到或其任何子项(它可以嵌套)。

  3. 你的 Camera需要组件 PhysicsRaycaster随附的。在这里确保 eventMask包括放置插槽对象的图层。 (对于使用 2D 碰撞器的选项 3,请改用 Physics2DRaycaster )

结果:

enter image description here


如您所见,这些方法在 Inspector 中不可见(由于前面提到的原因),但添加的监听器方法仍会被调用。

关于c# - Unity3D_2019 : Adding EventTriggers at runtime,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61138585/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com