c# - 将引用(以及Unity的WWW.progress)传递给协同程序/IEnumerator

标签 c# unity3d compiler-errors coroutine ienumerator

我已经使用Unity 5在C#中制作了一个协程,我想传递一个对其的引用。它似乎没有用,所以我制作了一个测试脚本来尝试隔离以下错误。error CS1623: Iterators cannot have ref or out parameters
这是测试脚本:

using UnityEngine;
using System.Collections;

public class TEMP : MonoBehaviour {

    // Use this for initialization
    void Start () {
        float var = 3.141f;
        StartCoroutine ( test (ref var) );
    }

    IEnumerator test (ref float value) {
        for (int i = 1; i <= 10; i++) {
            Debug.Log (value);
            yield return null;
        }
    }
}

另外,在我的实际脚本上,我正在使用Unity的WWW类下载某些内容,当我通过www.progress作为引用时,也会出现此错误:A property or indexer 'UnityEngine.WWW.progress' may not be passed as 'ref' or 'out' parameter

最佳答案

从函数参数中删除引用。

// Use this for initialization
void Start () {
    float var = 3.141f;
    StartCoroutine ( test (var) );
}

IEnumerator test (float value) {
    for (int i = 1; i <= 10; i++) {
        Debug.Log (value);
        yield return null;
    }
}

根据您的要求,您可以使用out参数执行以下操作。我不确定您打算如何使用这些方法,但我认为您对ref参数及其工作方式有错误的认识。
class Program
{
    static void Main(string[] args)
    {
        float var = 3.14f;
        StartCoroutine(test(var), out var);

    }

    static IEnumerator test(float value)
    {
        for (int i = 1; i <= 10; i++)
        {
            Console.WriteLine(value);
            yield return value + 1;
        }
    }

    static void StartCoroutine(IEnumerator test, out float update)
    {
        update = 0;
        while (test.MoveNext())
        {
            update++;
            Console.WriteLine("Executing..." + update);
        }
    }
}

但是,您永远无法将ref或out参数传递给Interator。

关于c# - 将引用(以及Unity的WWW.progress)传递给协同程序/IEnumerator,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33435111/

相关文章:

swift - ARKit——适应光照条件

c# - Unity C# : Issue with rotating, 现在面对错误的方向

java - 无法对非静态方法进行静态引用

c# - Inno Setup GetStringFileInfo 无法读取 AssemblyTitle

c# - Excel 生成 c# 中需要空白列名

c# - Devexpress ASPxPopupControl 不弹出

c# - 从其他脚本Unity访问脚本

c# - 为什么我的验证消息是通用的? ASP.NET MVC

java - 如何解决JBoss Remote错误?

java - 找不到符号thread.sleep。