本文整理汇总了C#中System.Script.CreateCoroutine方法的典型用法代码示例。如果您正苦于以下问题:C# Script.CreateCoroutine方法的具体用法?C# Script.CreateCoroutine怎么用?C# Script.CreateCoroutine使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Script
的用法示例。
在下文中一共展示了Script.CreateCoroutine方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Coroutine_Direct_AsEnumerable
public void Coroutine_Direct_AsEnumerable()
{
string code = @"
return function()
local x = 0
while true do
x = x + 1
coroutine.yield(x)
if (x > 5) then
return 7
end
end
end
";
// Load the code and get the returned function
Script script = new Script();
DynValue function = script.DoString(code);
// Create the coroutine in C#
DynValue coroutine = script.CreateCoroutine(function);
// Loop the coroutine
string ret = "";
foreach (DynValue x in coroutine.Coroutine.AsTypedEnumerable())
{
ret = ret + x.ToString();
}
Assert.AreEqual("1234567", ret);
}
示例2: Coroutine_AutoYield
public void Coroutine_AutoYield()
{
string code = @"
function fib(n)
if (n == 0 or n == 1) then
return 1;
else
return fib(n - 1) + fib(n - 2);
end
end
";
// Load the code and get the returned function
Script script = new Script(CoreModules.None);
script.DoString(code);
// get the function
DynValue function = script.Globals.Get("fib");
// Create the coroutine in C#
DynValue coroutine = script.CreateCoroutine(function);
// Set the automatic yield counter every 10 instructions.
// 10 is a too small! Use a much bigger value in your code to avoid interrupting too often!
coroutine.Coroutine.AutoYieldCounter = 10;
int cycles = 0;
DynValue result = null;
// Cycle until we get that the coroutine has returned something useful and not an automatic yield..
for (result = coroutine.Coroutine.Resume(8);
result.Type == DataType.YieldRequest;
result = coroutine.Coroutine.Resume())
{
cycles += 1;
}
// Check the values of the operation
Assert.AreEqual(DataType.Number, result.Type);
Assert.AreEqual(34, result.Number);
// Check the autoyield actually triggered
Assert.IsTrue(cycles > 10);
}
示例3: Coroutine_Direct_Resume
public void Coroutine_Direct_Resume()
{
string code = @"
return function()
local x = 0
while true do
x = x + 1
coroutine.yield(x)
if (x > 5) then
return 7
end
end
end
";
// Load the code and get the returned function
Script script = new Script();
DynValue function = script.DoString(code);
// Create the coroutine in C#
DynValue coroutine = script.CreateCoroutine(function);
// Loop the coroutine
string ret = "";
while (coroutine.Coroutine.State != CoroutineState.Dead)
{
DynValue x = coroutine.Coroutine.Resume();
ret = ret + x.ToString();
}
Assert.AreEqual("1234567", ret);
}