Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Unity3D:使用委托返回值_C#_Unity3d_Delegates_Return Value - Fatal编程技术网

C# Unity3D:使用委托返回值

C# Unity3D:使用委托返回值,c#,unity3d,delegates,return-value,C#,Unity3d,Delegates,Return Value,我希望能够从委托中的所有方法获取返回值。这是我用c#编写的代码 如您所见,委托的返回类型为int。然后,我放入委托中的方法的返回类型也为int。其中一个返回1,另一个返回2。是否有办法将这些结果发送到我执行委托的位置?这将是: using UnityEngine; using System.Collections; public class TestDelagets : MonoBehaviour { void Start () { SolarSystem s = n

我希望能够从委托中的所有方法获取返回值。这是我用c#编写的代码

如您所见,委托的返回类型为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);
    }
}
在“常规的.NET框架”中,您可以使用
Delegate.GetInvocationList
。例如,要将其与LINQ结合起来:

// Note: do all of this after checking that unitSpawn is non-null...

var results = unitSpawn.GetInvocationList()
                       .Cast<UnitEventHandler>()
                       .Select(d => d(_unit))
                       .ToList();
//注意:在检查unitSpawn是否为非null后执行所有这些操作。。。
var results=unitSpawn.GetInvocationList()
.Cast()
.选择(d=>d(_单位))
.ToList();
我不知道这是否会对团结起作用,但我希望它会

如果LINQ部件不工作,您可以使用:

var invocations = unitSpawn.GetInvocationList();
var results = new int[invocations.Length];
for (int i = 0; i < invocations.Length; i++)
{
    results[i] = ((UnitEventHandler)invocations[i]).Invoke(_unit);
}
var invocations=unitSpawn.GetInvocationList();
var results=newint[invocations.Length];
for(int i=0;i
在“常规的.NET框架”中,您可以使用
委托.GetInvocationList
。例如,要将其与LINQ结合起来:

// Note: do all of this after checking that unitSpawn is non-null...

var results = unitSpawn.GetInvocationList()
                       .Cast<UnitEventHandler>()
                       .Select(d => d(_unit))
                       .ToList();
//注意:在检查unitSpawn是否为非null后执行所有这些操作。。。
var results=unitSpawn.GetInvocationList()
.Cast()
.选择(d=>d(_单位))
.ToList();
我不知道这是否会对团结起作用,但我希望它会

如果LINQ部件不工作,您可以使用:

var invocations = unitSpawn.GetInvocationList();
var results = new int[invocations.Length];
for (int i = 0; i < invocations.Length; i++)
{
    results[i] = ((UnitEventHandler)invocations[i]).Invoke(_unit);
}
var invocations=unitSpawn.GetInvocationList();
var results=newint[invocations.Length];
for(int i=0;i
当您提到需要获取附加值或两个单独的值时,我会选择不同的方法

您可以使用Linq,但Unity建议避免使用它。很可能是由于C++和C语言和GC之间的序列化过程。p> 您可以将方法存储在一系列操作中。然后,您可以获取全部金额,也可以使用基本的foreach循环逐个获取

public class DelegateContainer : IDisposable{
    private IList<Func<string, int>> container = null;
    public DelegateContainer(){
        this.container = new List<Func<string,int>>();
    }
    public void Dispose(){
         this.container.Clear();
         this.container = null;
    }
    public bool AddMethod(Func<string, int> func){
       if(func != null && this.container.Contains(func) == false){
           this.container.Add(func);
           return true;
       }
       return false;
    }
    public bool RemoveMethod(Func<string, int>func){
       if(func != null && this.container.Contains(func) == true){
           this.container.Remove(func);
           return true;
       }
       return false;
    }
    public int GetFullValue(){
       int total = 0;
       foreach(var meth in this.container){
           if(meth != null) { total += meth(""); }
       }
       return total;
    }
    public IEnumerable<int> GetAllValues(){
        IList <int> list = new List<int>();
        foreach(var meth in this.container){
           if(meth != null) { list.Add(meth("");); }
       }
       return list as IEnumerable<int>;
    }
} 
公共类DelegateContainer:IDisposable{
私有IList容器=null;
公共委派容器(){
this.container=新列表();
}
公共空间处置(){
this.container.Clear();
this.container=null;
}
公共bool AddMethod(Func Func){
if(func!=null&&this.container.Contains(func)==false){
this.container.Add(func);
返回true;
}
返回false;
}
公共布尔删除方法(func){
if(func!=null&&this.container.Contains(func)==true){
此.container.Remove(func);
返回true;
}
返回false;
}
public int GetFullValue(){
int-total=0;
foreach(此.container中的var meth){
如果(meth!=null){total+=meth(“”;}
}
返回总数;
}
公共IEnumerable GetAllValues(){
IList list=新列表();
foreach(此.container中的var meth){
如果(meth!=null){list.Add(meth(“”;);}
}
返回列表为IEnumerable;
}
} 

当您提到需要获取附加值或两个单独的值时,我会选择不同的方法

您可以使用Linq,但Unity建议避免使用它。很可能是由于C++和C语言和GC之间的序列化过程。p> 您可以将方法存储在一系列操作中。然后,您可以获取全部金额,也可以使用基本的foreach循环逐个获取

public class DelegateContainer : IDisposable{
    private IList<Func<string, int>> container = null;
    public DelegateContainer(){
        this.container = new List<Func<string,int>>();
    }
    public void Dispose(){
         this.container.Clear();
         this.container = null;
    }
    public bool AddMethod(Func<string, int> func){
       if(func != null && this.container.Contains(func) == false){
           this.container.Add(func);
           return true;
       }
       return false;
    }
    public bool RemoveMethod(Func<string, int>func){
       if(func != null && this.container.Contains(func) == true){
           this.container.Remove(func);
           return true;
       }
       return false;
    }
    public int GetFullValue(){
       int total = 0;
       foreach(var meth in this.container){
           if(meth != null) { total += meth(""); }
       }
       return total;
    }
    public IEnumerable<int> GetAllValues(){
        IList <int> list = new List<int>();
        foreach(var meth in this.container){
           if(meth != null) { list.Add(meth("");); }
       }
       return list as IEnumerable<int>;
    }
} 
公共类DelegateContainer:IDisposable{
私有IList容器=null;
公共委派容器(){
this.container=新列表();
}
公共空间处置(){
this.container.Clear();
this.container=null;
}
公共bool AddMethod(Func Func){
if(func!=null&&this.container.Contains(func)==false){
this.container.Add(func);
返回true;
}
返回false;
}
公共布尔删除方法(func){
if(func!=null&&this.container.Contains(func)==true){
此.container.Remove(func);
返回true;
}
返回false;
}
public int GetFullValue(){
int-total=0;
foreach(此.container中的var meth){
如果(meth!=null){total+=meth(“”;}
}
返回总数;
}
公共IEnumerable GetAllValues(){
IList list=新列表();
foreach(此.container中的var meth){
如果(meth!=null){list.Add(meth(“”;);}
}
返回列表为IEnumerable;
}
} 

谢谢大家!这很有帮助。我用以下代码解决了这个问题:

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);
        }



        System.Delegate[] funcs = unitSpawn.GetInvocationList();
        int[] TIntArray = new int[funcs.Length];

        for (int i = 0; i < funcs.Length; ++i)
        {
            TIntArray[i] = (int) funcs[i].DynamicInvoke(_unit);


        }

        return TIntArray;
    }
}

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;
    }
}

谢谢大家!这很有帮助。我用以下代码解决了这个问题:

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);
        }



        System.Delegate[] funcs = unitSpawn.GetInvocationList();
        int[] TIntArray = new int[funcs.Length];

        for (int i = 0; i < funcs.Length; ++i)
        {
            TIntArray[i] = (int) funcs[i].DynamicInvoke(_unit);


        }

        return TIntArray;
    }
}

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;
    }
}

Linq确实在Unity中工作,但Unity开发者建议避免使用它。据报道,它在iOS上失败。不确定是否仍然有问题。@Everts可能这是过去几年发生的变化,但Linq目前在Unity中绝对没有问题。它可能已经改进了,但有一段时间,iOS上的新版本也会带来麻烦。这是由于AOT与JIT编译。但是他们可能已经用一些构建魔法修复了它。Linq确实在Unity中起作用,但Unity开发者建议避免这种情况。据报道,它在iOS上失败。不确定是否仍然有问题。@Everts可能这是过去几年发生的变化,但Linq目前在Unity中绝对没有问题。它可能已经改进了,但有一段时间,iOS上的新版本也会带来麻烦。这是由于AOT与JIT编译。但他们可能已经用一些建造魔法修复了它。