Unity代码基础之“MonoBehaviour里面的常用变量”和“协程”(3)

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

public class scene3 : MonoBehaviour
{
    public GameObject cubecolor;
    public cube1 cube;
    // Start is called before the first frame update
    void Start()
    {
        //五、MonoBehaviour里面的常用变量
        Debug.Log(this.isActiveAndEnabled);//gameObject是否处于激活状态
        Debug.Log(this.enabled);//是控制一个物体是否在屏幕上渲染或显示,而物体实际还是存在,只是相当于隐身,而物体本身的碰撞体还依然存在的
       // enabled = false;
        Debug.Log(name);//名称

        Debug.Log(tag);//标签
        Debug.Log(gameObject);//游戏物体
        Debug.Log(transform);//组件
        print("haha");

        Debug.Log(cube.isActiveAndEnabled);
        Debug.Log(cube.enabled);
        //cube.enabled = false;
        Debug.Log(cube.name);

        Debug.Log(cube.tag);
        Debug.Log(cube.gameObject);
        Debug.Log(cube.transform);

        //8.MonoBehaviour中Invoke的使用
        //Invoke("Attack", 3);//int秒后调用名字为"方法名"的方法。
        InvokeRepeating("Attacka", 3, 6);//int1秒后调用调用名字为"方法名"的方法,之后每隔int2秒后再次调用这个方法。
        //bool res = IsInvoking("Attack");//有返回值返回bool型。正在调用就返回true 
        CancelInvoke("Attacka");//取消方法名的调用,不指定参数会把所有的invoke取消


        //六、什么是协程(也叫协成方法Coroutines)、它是如何执行的
        //1.什么是协成
        //普通方法:
        //执行顺序是从上往下的,遇到方法会先执行方法里的,再继续往下执行
        //协成方法:
        //但是如果你是执行的协成方法,遇到方法不会先执行方法里的,它会继续往下执行(并且自身可以暂停)
        print("普通调用1");
        //Changcolor();//普通方法
        //StartCoroutine(Changecolorb());//调用协成
        print("普通调用2");
        //协成方法(看CPU的强大,如果强,还是跟普通方法一样的调用顺序,如果不强,则会按它的规则顺序执行)
        //Coroutines
        //(1).返回值是IEnumerator
        //(2).返回参数的时候要使用到 yield return null可以是其他值
        //(3).调用StartCoroutine(method());
        // (4).关闭协成的方法StopCoroutine("Fade");

        //2.使用Coroutine实现颜色动画渐变
        //Fade();

        //3.Coroutine协程的开启和关闭
        //两种方式

        

    }
    IEnumerator Changecolorb() {
        print("协成1");
        yield return new WaitForSeconds(3);//上一句执行后在这句停3s再执行下一句
        cubecolor.GetComponent<MeshRenderer>().material.color = Color.blue;
        print("协成2");
        yield return null;//不加会报错,因为这个方法要返回这个类型,所以要做一个返回,null可以是其他值
    }

    void Changcolor() {
        //修改一个游戏物体的颜色
        print("普通调用3");
      
        cubecolor.GetComponent<MeshRenderer>().material.color = Color.blue;
        
        print("普通调用4");
    }

    private IEnumerator ie;
    // Update is called once per frame
    void Update()
    {
        bool res = IsInvoking("Attack");//有返回值返回bool型。正在调用就返回true 
        //Debug.Log(res);

        if(Input.GetKeyDown(KeyCode.Space)){//按下空格键的时候启用
            ie = Fade();
            // StartCoroutine(ie);//方式1
            StartCoroutine("Fade");//方式2
        } 

        if (Input.GetKeyDown(KeyCode.S)) {

            //StopCoroutine(Fade());//这种关闭是不行的
            //StopCoroutine(ie);//方式1 这样是可以的
            StopCoroutine("Fade");//方式2

        }
    }

    IEnumerator Fade() {
        for (;;){
            //Color color = cubecolor.GetComponent<MeshRenderer>().material.color =new Color(i,i,i,i);
           Color color = cubecolor.GetComponent<MeshRenderer>().material.color;//获取当前颜色
           Color newcolor = Color.Lerp(color, Color.red, 0.02f);//当前颜色-新的颜色-用时
            cubecolor.GetComponent<MeshRenderer>().material.color = newcolor;//赋值
           yield return new WaitForSeconds(0.02f);//暂停一个时间来看变化0.2S一个变化
            if (Mathf.Abs(Color.red.g - newcolor.g) <= 0.01f)
            {//判断颜色什么时候结束,Mathf.Abs绝对值,如果2个颜色的g值,相差的值小于等于0.01就让它结束
                break;
            }

        }
        
    }
    void Attack()
    {
        Debug.Log("3秒后公攻击目标");
    }
    void Attacka()
    {
        Debug.Log("再次攻击");
    }
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值