如何逐步声明公共事件,然后根据相同的整数执行?

huangapple go评论90阅读模式
英文:

How to incrementally declare public events and then execute based on the same int?

问题

我需要能够按指定顺序逐渐触发 i 个方法的能力,这些方法必须是公共 Unity 事件,以便可以用来执行不同的操作。目前的实现是一个不够动态且结构混乱的方式,需要改进。

public class IncrementEventHandler : MonoBehaviour
{
    int eventIncrement = 0;
    public int numberOfTriggers; // 这里是你要在检查器中设置的触发器数量

    public List<UnityEvent> triggerEventsList = new List<UnityEvent>();

    public void Start()
    {
        // 初始化 triggerEventsList,声明 i 个 UnityEvent
        for (int i = 0; i < numberOfTriggers; i++)
        {
            triggerEventsList.Add(new UnityEvent());
        }
    }

    public void triggerEvents()
    {
        eventIncrement++;

        if (eventIncrement <= numberOfTriggers)
        {
            triggerEventsList[eventIncrement - 1].Invoke();
        }
    }
}

这个版本将允许你在检查器中设置触发器数量并自动生成对应数量的 Unity 事件。然后,在 triggerEvents 方法中,它会按顺序触发这些事件,直到达到指定的触发器数量。这个实现更加动态和可扩展。

英文:

What problem I'm trying to solve:
I need the ability to incrementally fire off i number of methods, one method per trigger in the specified order.

These methods have to be public unity events so that they can be used to do different things.

The "I don't know what I'm doing" version of this is below - like it works, mostly, but it's horrible. Besides the horrible structure, it's not dynamic.

public class IncrementEventHandler : MonoBehaviour
    {
        int eventIncrement = 0;

        public UnityEvent onTriggered1;
        public UnityEvent onTriggered2;
        public UnityEvent onTriggered3;
        public UnityEvent onTriggered4;
        public UnityEvent onTriggered5;
        public UnityEvent onTriggered6;
        public UnityEvent onTriggered7;
        public UnityEvent onTriggered8;
        public UnityEvent onTriggered9;
        public UnityEvent onTriggered10;

        public void triggerEvents()
        {
            eventIncrement++;
        
            if(eventIncrement == 1)
            {
                onTriggered1.Invoke();
            }
            if(eventIncrement == 2)
            {
                onTriggered2.Invoke();
            }
            if(eventIncrement == 3)
            {
                onTriggered3.Invoke();
            }
            if(eventIncrement == 4)
            {
                onTriggered4.Invoke();
            }
            if(eventIncrement == 5)
            {
                onTriggered5.Invoke();
            }
            if(eventIncrement == 6)
            {
                onTriggered6.Invoke();
            }
            if(eventIncrement == 7)
            {
                onTriggered7.Invoke();
            }
            if(eventIncrement == 8)
            {
                onTriggered8.Invoke();
            }
            if(eventIncrement == 9)
            {
                onTriggered9.Invoke();
            }
            if(eventIncrement == 10)
            {
                onTriggered10.Invoke();
            }
            
        }
    }

Attached image https://i.stack.imgur.com/RbttT.png is what it looks like in the inspector. And that's what I want to create but actually make it DYNAMIC. Have a public int i that I populate in the inspector, that then declares the i number of public UnityEvents named "onTriggered"+"i" that automatically appear in the inspector that then can be used to do whatever is needed. Then whatever else in the game calls triggerEvents() and that can be called up to i times to do the whole list of events.

If these were not methods then I'd have a basic grasp of how to serialize all of this for the inspector, but methods are static so no serialization. I tried to learn about delegates to fix the static issue, but still have absolutely no understanding of how to apply them to my specific use case. I have a basic grasp that they are used to subscribe classes to methods they don't inherently have but that doesn't seem to solve anything for me in terms of making a dynamic list of methods in the inspector. So I don't know if that's the correct thread to follow.

The other hurdle is I don't know how to dynamically name these methods for declaration.

Basically I need to declare the methods in a for loop and then execute them incrementally.

Appreciate any help.

答案1

得分: 0

你尝试过创建一个Unity事件列表吗?

使用 System.Collections;
使用 System.Collections.Generic;
使用 UnityEngine;
使用 UnityEngine.Events;

public class IncrementEventHandler : MonoBehaviour
{
    public List<UnityEvent> unityEvents = new List<UnityEvent>();
    public int eventIncrement = 0;

    public void triggerEvents()
    {
        eventIncrement++;

        for (int i = eventIncrement; i < unityEvents.Count; i++)
        {
            unityEvents[i]?.Invoke();
        }
    }
}
英文:

Have you tried making a list of unityEvents ?

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

public class IncrementEventHandler : MonoBehaviour
{
    public List&lt;UnityEvent&gt; unityEvents = new List&lt;UnityEvent&gt;();
    public int eventIncrement = 0;


    
    public void triggerEvents()
    {

        eventIncrement++;

        for (int i = eventIncrement ; i &lt; unityEvents.Count; i++)
        {

            unityEvents[i]?.Invoke();

        }



    }

huangapple
  • 本文由 发表于 2023年7月13日 13:59:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/76676328.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定