1, 新建工程做相关设置。

Unity间隔或延迟固定的时间让物体移动一下,绘制线条_新建工程

2  run.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class run : MonoBehaviour {

	[Header("Time Variables")]
	[Tooltip("设置间隔时间的两个变量")]
	public float proTime = 0.0f; 
	public float NextTime = 0.0f; 

	[Header("Speed")]
	[Tooltip("控制物体移动的快慢")]
	[Range(1, 100)]
	public float scaleSpeed = 5.0f;


	// Use this for initialization
	void Start () {
		
	}
	
	// Update is called once per frame
	void Update () {

		proTime = Time.fixedTime;
		if (proTime - NextTime == 3) 
		//if (proTime - NextTime > 3) 
		{
			print ("FixedTime Here" + (proTime - NextTime));
			transform.Translate (Vector3.up*scaleSpeed);
			//transform.Translate (Vector3.up*scaleSpeed*Time.deltaTime);
			NextTime = proTime;
		}
	}
}

2 第二种思路是使用 InvokeRepeating()函数

InvokeRepeating("clearAnn", 1, 3); 放在Start里面,延迟1秒运行,之后没有间隔3秒钟调用一次 clearAnn函数

这样程序运行后,函数 clearAnn函数就可以和Update()函数配合实现多线程。

如果要延迟一段时间可以 clearAnn函数中设置

		System.Threading.Thread.Sleep (3);

3 第三种思路是使用多线来实现

 private Thread clearThread;

Void Start()
{

  clearThread =  new Thread (clearAnnotations);


}

 void Update ()
{

//do something
}

void clearAnnotations()
{
  //do another something
}

如果在clearAnnotations中实现延时则如下,当然也可在里面根据需要控制bool变量的值,从而和update()里面的功能相配合,实现多线程。

	void clearAnnotations()
	{
		Thread.Sleep (3);
		this.GetComponent<LineRenderer> ().positionCount = 0;
		isNUll = true;
		i = 0;
	}

4  我想到的每四种方案是使用协程

但发现不行,也许是我程序写的不对。我要实现的功能是在Update里面绘制线条,在绘制完后大概3秒钟左右把绘制的清除,以便有利于我继续绘制,并继续3秒钟左右清除,一直循环。

但协程的效果是根本就绘不出线条,全部清除了。

我仔细一想,这应该和协同的原理有关,它就是一个主线程,然后把cpu分成小的单元,一会给yield之前的部分,一会给yield之后的部分。但也好像是不对呀。

不知道 为什么?

 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Threading;

public class Annotations : MonoBehaviour {

	//to find HTC vive controller(Right or Left)
	//public SteamVR_TrackedObject trackedObj;
	private Thread clearThread;

	public float proTime = 0.0f;
	public float nextTime = 0.0f;

	private LineRenderer line; 


	private int i = 0;  

	Vector3 RunStart = Vector3.zero;
	Vector3 RunNext = Vector3.zero;

	bool isNUll = true;


	// Use this for initialization
	void Start ()
	{
		
		clearThread = new Thread (clearAnnotations);
		line = this.GetComponent<LineRenderer>();//获得该物体上的LineRender组件 
		//line.SetColors(Color.blue, Color.red);//设置颜色  
		//line.SetWidth(0.2f, 0.1f);//设置宽度 
	    //InvokeRepeating("clearAnn", 1, 3);

	}
	// Update is called once per frame
	void Update ()
	{


		var point = this.transform.position;
		//print ("point"+point);

		//To have lines using LineRenderer
		if(isNUll)
		{
			RunStart = point;
			isNUll = false;
		}

		RunNext = point;
		//print ("RunNext"+RunNext);

		if (RunStart != RunNext) {
			i++;
			line.SetVertexCount(i);//设置顶点数 
			line.SetPosition(i-1, point);

		}

		RunStart = RunNext;


		// clear annotations
//		proTime = Time.fixedTime;
//		if (proTime - nextTime == 5) {
//
//			this.GetComponent<LineRenderer> ().positionCount = 0;
//			isNUll = true;
//			i = 0;
//			nextTime = proTime;
//		}

	//	StartCoroutine (clearA());

		if (Input.GetKeyDown(KeyCode.A) || Input.GetKeyDown(KeyCode.Q)) {

			this.GetComponent<LineRenderer> ().positionCount = 0;
			isNUll = true;
			i = 0;
		}


	}

	void clearAnnotations()
	{
		Thread.Sleep (3);
		this.GetComponent<LineRenderer> ().positionCount = 0;
		isNUll = true;
		i = 0;
	}



	IEnumerator clearA()
	{
		print ("222222");
		Thread.Sleep (3);
		yield return null;
		this.GetComponent<LineRenderer> ().positionCount = 0;
		isNUll = true;
		i = 0;
	}


}