我尝试在使用线程的持续时间循环中实现淡入和淡出对象.....但在玩游戏后我得到了
NullReferenceException: Object reference not set to an instance of an object
此行的错误
resetevent.Set ();
请帮助我.....
我的代码:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System.Threading;
using System;
public class Transparent : MonoBehaviour {
private float duration = .7f;
DateTime time;
bool isStopped=false;
Thread thread;
Color textureColor;
AutoResetEvent resetevent;
// Update is called once per frame void
//----------------------------------------------
public void start_transparency()
{
Debug.Log ("game in start tranparency");
textureColor = this.GetComponent<SpriteRenderer> ().material.color;
thread = new Thread (run);
thread.Start ();
resetevent = new AutoResetEvent(false);
}
//------------------------------------------------
void blink() {
//textureColor.a = Mathf.PingPong(Time.time, duration) / duration;
//this.GetComponent<SpriteRenderer>().material.color = textureColor;
// this could also be a condition indicating "alive or dead"
resetevent.WaitOne();
DateTime now = DateTime.Now;
TimeSpan deltaTime = now - time;
time = now;
textureColor.a = Mathf.PingPong ((float)deltaTime.TotalSeconds, duration) / duration;
//end of if(this.transform.childCount =0)
}
//------------------------------------------------
void run()
{
while (!isStopped) {
time = DateTime.Now;
blink ();
}
}
//------------------------------------------------
private void Update()
{
resetevent.Set ();
this.GetComponent<SpriteRenderer> ().material.color = textureColor;
}
//---------------------------------------------------
private void OnDestroy()
{
thread.Abort();
isStopped = true;
}
//--------------------------------------------
private void OnApplicationQuit()
{
thread.Abort();
isStopped = true;
}
}