Unity学习笔记——基础常用API(1)

25 篇文章 4 订阅
7 篇文章 0 订阅

Time

1、Time.deltaTime大概是1/60秒一次,也就是一秒六十次;

2、Time.FixedDeltaTime,修正的时间;

3、Time.RealtimeSiceStartup,游戏开始运行使用的时间(秒);(可以用时间间隔来计算某些函数运行的时间)

4、FrameTime,帧数

5、timeScle,加速比例

创建物体的三种方法

        //第一种创建方法,括号内可以写上创建的物体名称 
        GameObject go = new GameObject("Cube");
        //根据prefab创建
        //根据另外一个游戏物体
        GameObject.Instantiate(prefab);//创建prefab 可以根据一个prefab或者另外一个游戏物体创建

        //创建系统内的原始简单物体
        GameObject.CreatePrimitive(PrimitiveType.Cube);
        GameObject.CreatePrimitive(PrimitiveType.Capsule);

通过代码给物体添加组件(脚本)

        //组件可以是系统组件也可以是自定义组件
        GameObject go = GameObject.CreatePrimitive(PrimitiveType.Capsule);
        go.AddComponent<Rigidbody>();
        go.AddComponent<API01EventFunction>();

搜索物体

        //全局查找物体 比较耗费性能
        GameObject go = GameObject.Find("Main Camera");
        //查找子物体中所有的物体 返回数组
        GameObject[] gos = GameObject.FindGameObjectsWithTag("Main Camera");
        //查找子物体中的物体,返回active的一个
        GameObject go = GameObject.FindGameObjectWithTag("Main Camera");

游戏物体间消息发送和接受

        
        public GameObject target;

        //发送消息 参数,消息名称、参数、是否必须有接收者
        target.BroadcastMessage("Attack", null, SendMessageOptions.DontRequireReceiver);//消息发送范围为所有当前物体及所有子孙物体
        target.SendMessage("Attack", null, SendMessageOptions.DontRequireReceiver);//消息发送范围为所有当前物体 不包括子孙物体
        target.SendMessageUpwards("Attack", null, SendMessageOptions.DontRequireReceiver);//消息发送范围为所有当前物体及所有父和父以上物体


    //接收消息方法
        void Attack()
    {
        Debug.Log(this.gameObject + "攻击目标");
    }

获取组建的方法

//获取组件
gameObject.GetComponent<componentName>();

//获取多个组件
gameObject.GetComponents<componentName>();//返回数组

//获取自己及子孙物体中的组件
gameObject.GetComponentsInChildren<componentName>();//返回数组

//获取自己及父和父以上物体的组件
gameObject.GetComponentsInParent<componentName>();//返回数组

Invoke的使用

    void Start()
    {
        //3秒后调用attack方法
        Invoke("Attack", 3);

        //4秒后开始调用方法,之后每隔两秒调用一次
        InvokeRepeating("Attack", 4, 2);
        //取消Invoke调用方法
        CancelInvoke("Attack");
    }

    // Update is called once per frame
    void Update()
    {
        bool res = IsInvoking("Attack");
        print(res);
    }

    void Attack()
    {
        //C#中常用的输出方法是Debug.Log  print是monobehaviour的方法
        print("攻击目标");
    }

Coroutine携程的使用

    public GameObject Cube;
    // Start is called before the first frame update
    void Start()
    {
        //携程方法的调用
        //携程方法开启后不会阻塞主线程的执行
        StartCoroutine(ChangeColor());
       
    }
    //携程方法
    //1、携程方法的返回值为IEnumerator
    //2、返回参数的时候使用yield return
    IEnumerator ChangeColor()
    {
        Cube.GetComponent<MeshRenderer>().material.color = Color.blue;

        yield return null;
    }

例:使用携程实现颜色渐变,使用Lerp插值

     void Update()
    {
        //按下空格执行携程方法
        if (Input.GetKeyDown(KeyCode.Space))
        {
            StartCoroutine(Fade());
        }
    }

    IEnumerator Fade()
    {
        for(; ; )//死循环
        {
            //拿到cube的颜色
            Color color = cube.GetComponent<MeshRenderer>().material.color;
            //得到一个新的渐变颜色 (Lerp可以详细学习一下)
            Color newColor = Color.Lerp(color, Color.red, 0.02f);
            //把新颜色赋值给cube
            cube.GetComponent<MeshRenderer>().material.color = newColor;
            //延迟0.02秒执行
            yield return new WaitForSeconds(0.02f);
            //颜色差值距红色0.01结束死循环
            if (Mathf.Abs(Color.red.g - newColor.g) <= 0.01f)
            {
                break;
            }
        }
    }

关闭携程

    private IEnumerator ie;

    void Update()
    {
        //按下空格执行携程方法
        if (Input.GetKeyDown(KeyCode.Space))
        {
            //使用IEnumerator参数开启携程 需要使用IEnumerator关闭
            //ie = Fade();
            //StartCoroutine(Fade());
            //使用方法名开启携程,需要使用方法名关闭
            StartCoroutine("Fade");

        }
        if (Input.GetKeyDown(KeyCode.S))
        {
            //StopCoroutine(ie);
            StopCoroutine("Fade");

        }
    }

Mathf的常用方法

        print(Mathf.Deg2Rad);//弧度转化为角度
        print(Mathf.Rad2Deg);//角度转化弧度
        print(Mathf.Infinity);//正无穷大的一个数
        print(Mathf.NegativeInfinity);//负无穷大的一个数
        print(Mathf.PI);//圆周率
        print(Mathf.Epsilon);//无限接近0的一个正数

        print(Mathf.ClosestPowerOfTwo(2));//取得参数最接近的2的平方数
        print(Mathf.Max(1,2,4,7,3));//取最大值
        print(Mathf.Min(3,5,1,7,86,4));//取最小值
        print(Mathf.Pow(4,5));//取平方 即4的5次方
        print(Mathf.Sqrt(3));//3的平方根

Mathf的Lerp、MoveTowarsd和PingPong方法

    public Transform cube;
    void Start()
    {
        //cube位置初始化为0,0,0
        cube.position = new Vector3(0, 0, 0);
    }
    void Update()
    {
        //获取cube当前的x值
        float x = cube.position.x;

        //通过插值算出新的x值   (最小值,最大值,取值比例)
        //非线性运动 移动的速度会越来越慢 插值运算越来越小 越来越无限接近10
        //float newX = Mathf.Lerp(x, 10, 0.1f);

        //通过MoveTowards运算  (最小值,最大值,每次增加的数值)
        //线性运动 移动速度一样
        float newX = Mathf.MoveTowards(x, 10, 0.1f);

        //把新的x赋值给cube  
        //两种方法都是取中间值,并且不会超过两个值值得范围
        cube.position = new Vector3(newX, 0, 0);

        //Mathf.PingPong方法即数值线性往返运算,
        //(数值,最大值)线性往复运动  参数一在0和参数2之间往复运算 
        cube.position = new Vector3(Mathf.PingPong(Time.Time,5),0.0);
    }

 

 

 

 

 

 

 

 

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值