public AudioClip audiosSuccess; 将音频文件放入
AudioSource audiosSource = this.GetComponent<AudioSource>(); 引用音频文件
audiosSource.PlayOneShot(audiosSuccess); 完整播放一次放入的音频audiosSuccess
以此类推可以改变 材质Texture 材质Material
public AudioClip[] song; 用数组传入多个音频文件
int num = Random.Range(0, song.Length); //定义随机数范围 产生随机数 随机播放
AudioClip clip = this.song[num]; //将随机的歌放入中间量
AudioSource audioSource = GetComponent<AudioSource>(); //创建一个新的音频
audioSource.clip = clip; //将音频(中间量)放入
audioSource.Play(); //播放音频
Debug.Log("当前播放第" + num + ",名字为:" + clip.name);
int num = -1; 顺序播放音乐
num++;
if (num >= song.Length) num = 0;
AudioClip clip = this.song[num];
AudioSource audioSource = GetComponent<AudioSource>();
audioSource.clip = clip;
audioSource.Play();
Debug.Log("当前播放第" + num + ",名字为:" + clip.name);
num++; 顺序更换颜色
if(num>= Color.Length) num = 0;
Material col = this.Color[num];
MeshRenderer material = GetComponent<MeshRenderer>();
material.material = col;
Debug.Log("当前颜色第" + num + "个,名字为:" + col.name);
Invoke(func,delay) 只调用一次
InvokeRepeating(func,delay,interval) 循环调用 相当于定时器每隔 interval时间后调用函数
IsInvoking(func) 是否在调度中
Cancellnvoke(func) 取消调用、从调度队列中移除
this.Invoke("DoSomething", 3); (调用的函数名,第一次触发时间)相当于延时3s
this.InvokeRepeating("DoSomething", 3,2);(调用的函数名,第一次触发时间,反复时间间隔)
Thread.CurrentThread.ManagedThreadId 获取线程号
unity中函数都为单线程
当调用两个中断InvokeRepeating时 unity会同时进行两个中断
像执行一次函数可以先判断是否在调用
if (IsInvoking("Expand")) //判断是否在调用
{
CancelInvoke("Expand"); //关闭调用Expand
}
else
{
InvokeRepeating("Expand", 2, 2);
}
CancelInvoke() 把脚本中的Invoke都关闭
InvokeRepeating("Upspeed", 0.1f, 0.1f); 用定时器实现对转速的匀加速匀减速
public float maxrotateSpeed = 0f;
bool m_speedUp = false; 判断是加速还是减速
if (Input.GetMouseButtonDown(0))
{
m_speedUp= !m_speedUp;
}
private void Upspeed()
{
if (m_speedUp)
{
if (rotateSpeed < maxrotateSpeed)
rotateSpeed += 10;
}
else
{
if(rotateSpeed > 0 )
rotateSpeed -= 10;
}