我希望能夠從我的委託中獲取所有方法的返回值。這是我用c#編寫的代碼。Unity3D:使用委託返回值
using UnityEngine;
using System.Collections;
public static class DelagetsAndEvents {
public delegate int UnitEventHandler(string _unit);
public static event UnitEventHandler unitSpawn;
public static int UnitSpawn(string _unit)
{
if(unitSpawn != null)
{
unitSpawn(_unit);
}
// here I want to return 1 + 2 from Planet/UnitSpawn and SolarSystem/UnitSpawn
// is it possible to run a foreach on every method in the delegate and add their returns?
return (method 1's return value) + (method 2's return value) (Or both seperately, that would be even better)
}
}
public class Planet {
public Planet()
{
DelagetsAndEvents.unitSpawn += UnitSpawn;
}
int UnitSpawn(string _unit)
{
Debug.Log("yo");
return 1;
}
}
public class SolarSystem{
public SolarSystem()
{
DelagetsAndEvents.unitSpawn += UnitSpawn;
}
int UnitSpawn(string _unit)
{
Debug.Log("bla");
return 2;
}
}
正如您所看到的,委託具有返回類型int。然後我放入我的委託的方法也有返回類型的int。其中一人返回1,另一人返回2.是否有辦法讓這些結果到我執行委託人的地點?這將在這裏:
using UnityEngine;
using System.Collections;
public class TestDelagets : MonoBehaviour {
void Start() {
SolarSystem s = new SolarSystem();
Planet p = new Planet();
string g = "";
int i = DelagetsAndEvents.UnitSpawn(g);
Debug.Log(i);
}
}
的LINQ不工作的統一,但其中的一件事情,統一開發者建議迴避。據報在iOS上失敗。不確定是否仍有問題。 – Everts
@fafase:被編輯爲顯示非LINQ版本。 –