1.yield return new WaitForSeconds(3);
让携程方法暂停三秒再向下执行
1.yield return new WaitForSeconds(3);
让携程方法暂停三秒再向下执行
for 死循环
for(;;)
{
循环结构体
if(判断结构体){
break;
}
}
使用Coroutine实现颜色动画渐变
实现黑白渐变动画
public class CoroutineCS : MonoBehaviour {
public GameObject cube;
void Update () {
if (Input.GetKeyDown(KeyCode.Space))
{
StartCoroutine(Fade());//实现黑到白的渐变
}
}
IEnumerator Fade()
{
for (float i = 0; i <= 1; i += 0.1f)//i代表颜色
{
cube.GetComponent<MeshRenderer>().material.color = new Color(i, i, i, i);//四个i分别代表R,G,B和透明度
yield return new WaitForSeconds(0.1f);//暂停0.1秒
}
}
}
void Update () {
if (Input.GetKeyDown(KeyCode.Space))
{
StartCoroutine(Fade());//实现白到红的渐变
}
}
IEnumerator Fade()
{
for (; ; )//死循环
{
Color color = cube.GetComponent<MeshRenderer>().material.color;//获取cube的color属性
Color newcolor = Color.Lerp(color, Color.red, 0.02f);//设置一个新颜色:红色,并设置0.02秒变换一次
cube.GetComponent<MeshRenderer>().material.color = newcolor;//把新颜色付给他
yield return new WaitForSeconds(0.02f);//暂停0.02秒
if (Mathf.Abs(Color.red.g - newcolor.g) <= 0.01f)//当新颜色的g与标准红色相差小于0.01,结束循环
{
break;
}
}
}
使用协程实现颜色动画渐变
协程暂停
yield return new WaitForSeconds()
yield return new WaitForSenconds(float xx秒)
协程。实现一个颜色渐变效果。
yield return new WaitforSeconds(3f);
for(float i = 0;i<=1;i+=0.1f)
{
cube.getComponent<MeshRender>().material.color = new Color(i,i,i,i);
yield return new WaitForSecond(0.1f);
参数正常是1.
}
Color newColor = Color.Lerp(当前color,目标color,0.02f)
yield return new WaitForSeconds(0.02f);
Mathf.Abs()绝对值。
yield return new WaitForSeconds(3);
等待3秒后开始调用协程方法;
yield return null;
协程返回值;
i<=1;i+=0.1f
void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
StartCoroutine(Fade());
}
}
IEnumerator Fade()
{
for (float i = 0; i <= 1; i += 0.1f)
{
cube.GetComponent<MeshRenderer>().material.color = new Color(i, i, i,i);
yield return new WaitForSeconds(0.1f);
}
}
void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
StartCoroutine(Fade());
}
}
IEnumerator Fade()
{
while (true)
{
// cube.GetComponent<MeshRenderer>().material.color = new Color(i, i, i,i);
Color color = cube.GetComponent<MeshRenderer>().material.color;
Color newColor = Color.Lerp(color,Color.red,0.02f);
cube.GetComponent<MeshRenderer>().material.color = newColor;
yield return new WaitForSeconds(0.02f);
print(1);
if (Mathf.Abs(Color.red.g-newColor.g)<=0.01f)
{
break;
}
}
}
1.创建游戏物体f的三种方法
new GameObject();括号里可以添加想要创建的物体类型例如
new GameObject("Cube");创建出一个正方体
GameObject.Instantiate();
实例化Prefabs或实例化游戏物体
GameObject.CreatePrimitive();
创建原始图形如:.GameObject.CreatePrimitive(PrimitiveType.Cube);
2.通过代码给游戏物体添加组件
private GameObject object;
object.AddComponent<Rigidbody>();
通过代码给游戏物体j添加脚本
object.AddComponent<脚本名>();
3.GameObject游戏物体的常用类
GameObject.activeInHierarchy 物体是否处于激活状态(包括自身及子物体)
4.UnityEngine下Object的静态方法
Object.Destroy:
Destroy(gameobject,time);
Object.DontDestroyOnLoad:
DontDestroyOnLoad(transform.gameObject);(在进行场景之间转换时不会被销毁,会被带到下一个场景中)
Object.FindObjectOfType:
Object a =FindObjectOfType(typeof(组件名))
Rigidbody a=FindObjectOfType<Rigidbody>();
只返回第一个被检测到含有此组件的物体。不查找未激活的游戏物体
Object.FindObjectsOfType:
Object a =FindObjectsOfType(typeof(组件名))
Rigidbody a=FindObjectsOfType<Rigidbody>();
将检测到的所有的含有此组件的物体都存在数组中。不查找未激活的游戏物体
5.查找方法
Find(比较耗费性能)
FindGameObjectsWithTag返回所有是次标签的物体返回数组
FindWithTag只返回第一个检测到的物体。
6.游戏物体间消息的发送和接收
GameObject.BroadcastMessage();包括其自身及其子物体
GameObject.SendMessage(); 不包括子物体
GameObject.SendMessageUpwards();包括其自身及其父物体
7.得到组件的各种方法函数
GetComponent;只会得到在游戏物体身上的第一个检测到的组件
GetComponent;会得到物体上所有的组件
GetComponentInChildren;会得到其自身及其子物体身上第一个检测到的相应的组件
GetComponentInParent;会得到其自身及其父物体身上第一个检测到的相应的组件
GetComponentsInChildren;会得到其自身及其子物体身上所有检测到的相应的组件
GetComponentsInParent;会得到其自身及其父物体身上所有检测到的相应的组件
8.Public Functions
Invoke;
Public void Invoke(string methodName,float time );可延时调用方法
CancelInvoke;取消所有调用的方法(仅适用于当前脚本,不影响其他脚本中相同方法的调用)
InvokeRepeating;重复调用方法
IsInvoking;判断方法是否正在被调用
协程:
1.返回值是IEnumerator
2.返回参数时使用yield return null/0。
3协程方法的调用:使用StartCoroutine(需要调用的协程方法的名字);
4暂停yield return new WaitForSeconds(需要暂停的时间);
例如:
IEnumerator Method(){
内容;
yied return null;
}