当前位置: 首页>>代码示例>>C#>>正文


C# Script.CreateCoroutine方法代码示例

本文整理汇总了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);
		}
开发者ID:cyecp,项目名称:moonsharp,代码行数:32,代码来源:CoroutineTests.cs

示例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);
		}
开发者ID:cyecp,项目名称:moonsharp,代码行数:44,代码来源:CoroutineTests.cs

示例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);
		}
开发者ID:cyecp,项目名称:moonsharp,代码行数:32,代码来源:CoroutineTests.cs


注:本文中的System.Script.CreateCoroutine方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。