目录
1.1 想知道:Unity中协程(IEnumerator)的使用方法介绍
2.1 Unity中协程(IEnumerator)的使用方法介绍
2.3 Unity 协程之三种分析解决StopCorotine终止不了
3.1 物体隐藏时候,协程关闭了,所以物体隐藏时候需要注意协程的开关
3.2 如操作3:协程start前需要判断其是否开启了,否则协程会不断地叠加(即:开启协程前,将之前已经开启的给关闭)
3.4 使用此方法可以开启、关闭线程(比输入字符串函数比较好,因为可以搜到那些地方调用了此协程)
四.操作:3:协程start前需要判断其是否开启了,否则协程会不断地叠加
1.错误的演示一遍:当我按下多次R按键,会发现Cube运行的越来越快
1.1 运行结果:当我不断按下R按键,cube会移动的越来越快,
1.正确的演示:弄一个bool变量控制协程是否已经开启了(因为查阅API,发现协程没有提供是否正在运行的变量,所以只好自己创建一个变量)
四.操作:4:StopCoroutine(ie_StopRecorder)不如 StopCoroutine("IE_StopRecorder")
1.StopCoroutine(ie_StopRecorder)发现没有执行协程
1.StopCoroutine("IE_StopRecorder")发现可以执行协程
Unity中协程(IEnumerator)的使用方法介绍_悲欢离合的博客-CSDN博客
- 总结:good:适合多看
Unity3D协程介绍 以及 使用_huang9012的专栏-CSDN博客_unity 协程
- 总结:good:适合多看
https://jingyan.baidu.com/article/7c6fb428c8478480642c9029.html
亲测:StartCoroutine(Func()),StopCoroutine(Func())的方法无法关闭协程- 亲测:StartCoroutine(“FuncName”),StopCoroutine("FuncName") 能够关闭协程
亲测:IEnumerator wrapFunc,包装一下协程函数,StartCoroutine(wrapFunc),StopCoroutine(wrapFunc) 能够关闭协程(项目有此发现无法关闭,所有不推荐使用)- 亲测:使用此方法可以开启、关闭线程(比输入字符串函数比较好,因为可以搜到那些地方调用了此协程)
private Coroutine coroutine_setReConnect; if (coroutine_setReConnect != null) { StopCoroutine(coroutine_setReConnect); coroutine_setReConnect = null; } coroutine_setReConnect = StartCoroutine(IE_SetReConnect());
- 亲测:使用此方法可以开启、关闭线程(比输入字符串函数比较好,因为可以搜到那些地方调用了此协程)
private Coroutine coroutine_setReConnect; if (coroutine_setReConnect != null) { StopCoroutine(coroutine_setReConnect); coroutine_setReConnect = null; } coroutine_setReConnect = StartCoroutine(IE_SetReConnect());
using System.Collections;
using UnityEngine;
public class IEnumeratorTest: MonoBehaviour
{
/// <summary>定义一个全局的对象用来接收协程的返回值</summary>
IEnumerator ie;
void Start()
{
ie = Test(5, "hello");
StartCoroutine(ie);
//使用方法名,并且带有一个参数(最多只能传一个参数)
StartCoroutine("Test1", 0);
}
void Update()
{
if(Input.GetKeyDown(KeyCode.Space))
{
StopCoroutine(ie);
StopCoroutine("Test1");
}
}
IEnumerator Test(int a,string str)
{
WaitForSeconds sec = new WaitForSeconds(1f);
while(true)
{
Debug.Log(a);
Debug.Log(str);
yield return new sec;
}
}
IEnumerator Test1(int a)
{
WaitForSeconds sec = new WaitForSeconds(1f);
while(true)
{
Debug.Log(a);
yield return new sec;
}
}
}
StartCoroutine("StopRecorder");
IEnumerator StopRecorder()
{
yield return new WaitForSeconds(5f);
//TODO:添加需要执行的动作
}

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// 功能:测试协程停止会不会对之前的协程造成影响
/// </summary>
public class MyCoroutineTest : MonoBehaviour
{
public GameObject obj_cube;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyUp(KeyCode.Space))
{
StopCoroutine("Coroutine");
//print("StopCoroutine");
}
else if (Input.GetKeyUp(KeyCode.R))
{
StartCoroutine("Coroutine");
//print("StartCoroutine");
}
}
//测试发现:不断的按下R,开启协程,导致cube移动越来越快
IEnumerator Coroutine()
{
while (true)
{
yield return new WaitForSeconds(0.8f);
Vector3 v3 = obj_cube.transform.position;
obj_cube.transform.position = new Vector3(v3.x + 1, v3.y, v3.z);
}
}
}

- 协程不断的StartCoroutine会导致物体越来越快,所以一定要StartCoroutine前StopCoroutine

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// 功能:测试协程停止会不会对之前的协程造成影响
/// </summary>
public class MyCoroutineTest : MonoBehaviour
{
/// <summary>GameObject:测试移动的cube</summary>
public GameObject obj_cube;
/// <summary>bool:协程是否开始运行</summary>
private bool bIsStartCoroutine;
// Start is called before the first frame update
void Start()
{
bIsStartCoroutine = false;
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyUp(KeyCode.Space))
{
if (bIsStartCoroutine)
{
bIsStartCoroutine = false;
StopCoroutine("Coroutine");
print("StopCoroutine");
}
}
else if (Input.GetKeyUp(KeyCode.R))
{
if (!bIsStartCoroutine)
{
bIsStartCoroutine = true;
StartCoroutine("Coroutine");
print("StartCoroutine");
}
}
}
测试发现:不断的按下R,开启协程,导致cube移动越来越快
//IEnumerator Coroutine()
//{
// while (true)
// {
// yield return new WaitForSeconds(0.8f);
// Vector3 v3 = obj_cube.transform.position;
// obj_cube.transform.position = new Vector3(v3.x + 1, v3.y, v3.z);
// }
//}
//测试发现:
IEnumerator Coroutine()
{
while (true)
{
yield return new WaitForSeconds(0.8f);
Vector3 v3 = obj_cube.transform.position;
obj_cube.transform.position = new Vector3(v3.x + 1, v3.y, v3.z);
}
}
}






我正在学习如何使用Nokogiri,根据这段代码我遇到了一些问题:require'rubygems'require'mechanize'post_agent=WWW::Mechanize.newpost_page=post_agent.get('http://www.vbulletin.org/forum/showthread.php?t=230708')puts"\nabsolutepathwithtbodygivesnil"putspost_page.parser.xpath('/html/body/div/div/div/div/div/table/tbody/tr/td/div
总的来说,我对ruby还比较陌生,我正在为我正在创建的对象编写一些rspec测试用例。许多测试用例都非常基础,我只是想确保正确填充和返回值。我想知道是否有办法使用循环结构来执行此操作。不必为我要测试的每个方法都设置一个assertEquals。例如:describeitem,"TestingtheItem"doit"willhaveanullvaluetostart"doitem=Item.new#HereIcoulddotheitem.name.shouldbe_nil#thenIcoulddoitem.category.shouldbe_nilendend但我想要一些方法来使用
类classAprivatedeffooputs:fooendpublicdefbarputs:barendprivatedefzimputs:zimendprotecteddefdibputs:dibendendA的实例a=A.new测试a.foorescueputs:faila.barrescueputs:faila.zimrescueputs:faila.dibrescueputs:faila.gazrescueputs:fail测试输出failbarfailfailfail.发送测试[:foo,:bar,:zim,:dib,:gaz].each{|m|a.send(m)resc
我正在尝试设置一个puppet节点,但rubygems似乎不正常。如果我通过它自己的二进制文件(/usr/lib/ruby/gems/1.8/gems/facter-1.5.8/bin/facter)在cli上运行facter,它工作正常,但如果我通过由rubygems(/usr/bin/facter)安装的二进制文件,它抛出:/usr/lib/ruby/1.8/facter/uptime.rb:11:undefinedmethod`get_uptime'forFacter::Util::Uptime:Module(NoMethodError)from/usr/lib/ruby
当我使用Bundler时,是否需要在我的Gemfile中将其列为依赖项?毕竟,我的代码中有些地方需要它。例如,当我进行Bundler设置时:require"bundler/setup" 最佳答案 没有。您可以尝试,但首先您必须用鞋带将自己抬离地面。 关于ruby-我需要将Bundler本身添加到Gemfile中吗?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/4758609/
我想了解Ruby方法methods()是如何工作的。我尝试使用“ruby方法”在Google上搜索,但这不是我需要的。我也看过ruby-doc.org,但我没有找到这种方法。你能详细解释一下它是如何工作的或者给我一个链接吗?更新我用methods()方法做了实验,得到了这样的结果:'labrat'代码classFirstdeffirst_instance_mymethodenddefself.first_class_mymethodendendclassSecond使用类#returnsavailablemethodslistforclassandancestorsputsSeco
我在我的项目中添加了一个系统来重置用户密码并通过电子邮件将密码发送给他,以防他忘记密码。昨天它运行良好(当我实现它时)。当我今天尝试启动服务器时,出现以下错误。=>BootingWEBrick=>Rails3.2.1applicationstartingindevelopmentonhttp://0.0.0.0:3000=>Callwith-dtodetach=>Ctrl-CtoshutdownserverExiting/Users/vinayshenoy/.rvm/gems/ruby-1.9.3-p0/gems/actionmailer-3.2.1/lib/action_mailer
设置:狂欢ruby1.9.2高线(1.6.13)描述:我已经相当习惯在其他一些项目中使用highline,但已经有几个月没有使用它了。现在,在Ruby1.9.2上全新安装时,它似乎不允许在同一行回答提示。所以以前我会看到类似的东西:require"highline/import"ask"Whatisyourfavoritecolor?"并得到:Whatisyourfavoritecolor?|现在我看到类似的东西:Whatisyourfavoritecolor?|竖线(|)符号是我的终端光标。知道为什么会发生这种变化吗? 最佳答案
我已经从我的命令行中获得了一切,所以我可以运行rubymyfile并且它可以正常工作。但是当我尝试从sublime中运行它时,我得到了undefinedmethod`require_relative'formain:Object有人知道我的sublime设置中缺少什么吗?我正在使用OSX并安装了rvm。 最佳答案 或者,您可以只使用“require”,它应该可以正常工作。我认为“require_relative”仅适用于ruby1.9+ 关于ruby-主要:Objectwhenrun
我注意到像bundler这样的项目在每个specfile中执行requirespec_helper我还注意到rspec使用选项--require,它允许您在引导rspec时要求一个文件。您还可以将其添加到.rspec文件中,因此只要您运行不带参数的rspec就会添加它。使用上述方法有什么缺点可以解释为什么像bundler这样的项目选择在每个规范文件中都需要spec_helper吗? 最佳答案 我不在Bundler上工作,所以我不能直接谈论他们的做法。并非所有项目都checkin.rspec文件。原因是这个文件,通常按照当前的惯例,只