Unity教程之-关于Unity3d事件返回值迭代器的问题

 

最近做unity3d项目时发现,当代理事件返回值为迭代器时,只有最后一次事件绑定才会生效,但我为了做时序而使用了协同Coroutine(现在想想用状态机就ok了…),有多次绑定事件的情况,为了解决这种情况,想出了一种方案:


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

public class Events<T> : IEnumerable
{
private List<T> m_pActions = new List<T>();
public void AddEventListenser(T action)
{
m_pActions.Add(action);
}

public void RemoveEventListener(T action)
{
m_pActions.Remove(action);
}

public List<T> Actions
{
get
{
return m_pActions;
}
}

public IEnumerator GetEnumerator()
{
return m_pActions.GetEnumerator();
}
}

调用方法:


public delegate IEnumerator CoroutineLinksEventHandler();
public Events<CoroutineLinksEventHandler> OnRoundBegin = new Events<CoroutineLinksEventHandler>();

foreach (CoroutineLinksEventHandler action in OnRoundBegin)
{
yield return StartCoroutine(action());
}

好了,本篇unity3d教程到此结束,下篇我们再会!