SIKI学习——UnityAPI常用方法和类详解0104

本文深入探讨Unity中协程的基本概念与执行原理,包括协程的开启、关闭及其实现颜色渐变动画的应用。此外,还详细解析了Mathf类中的各种数学函数,如插值、取整、求幂等,以及它们在游戏开发中的具体应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、什么是协程、它是如何执行的

如果执行的是一个普通方法,那么会等这个普通方法执行完,然后继续向下执行
如果调用的是一个协程方法,那么调用完协程方法后,不会等这个方法执行完,就继续向下执行了。

public class API08Coroutine : MonoBehaviour
{
    public GameObject cube;
 void Start ()
    {       
        print("ha");
        //ChangeColor();
        StartCoroutine(ChangeColor());
        //协程方法开启后,会继续运行下面的代码,不会等协程方法运行结束才继续执行
        print("hahaha");
 }
    //Coroutine
    //1.返回值IEnumerator
    //2.返回参数的时候使用yield return
    //3.协程方法的调用StartCoroutine(method());
    IEnumerator ChangeColor()
    {
        print("haColor");
        cube.GetComponent<MeshRenderer>().material.color = Color.blue;
        print("hahaColor");
        yield return null;
    }
}

二、使用Coroutine实现颜色动画渐变

public class API08Coroutine : MonoBehaviour
{
    public GameObject cube;
 void Start ()
    {       
        //print("ha");
        ////ChangeColor();
        //StartCoroutine(ChangeColor());
        ////协程方法开启后,会继续运行下面的代码,不会等协程方法运行结束才继续执行
        //print("hahaha");
 }
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space))
        {
            StartCoroutine(Fade());
        }
    }
    //实现渐变改变颜色
    IEnumerator Fade()
    {
        for (; ; )//这是个死循环
        {
            //1.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);
            if (Mathf.Abs(Color.red.g-newColor.g)<=0.01f)
            {
                break;
            }
        }
    }
    //Coroutine
    //1.返回值IEnumerator
    //2.返回参数的时候使用yield return
    //3.协程方法的调用StartCoroutine(method());
    IEnumerator ChangeColor()
    {
        print("haColor");
        yield return new WaitForSeconds(3);//暂停三秒在执行后面的代码
        cube.GetComponent<MeshRenderer>().material.color = Color.blue;
        print("hahaColor");
        yield return null;
    }
}

三、Coroutine协程的开启和关闭

public class API08Coroutine : MonoBehaviour
{
    public GameObject cube;
 void Start ()
    {       
        //print("ha");
        ////ChangeColor();
        //StartCoroutine(ChangeColor());
        ////协程方法开启后,会继续运行下面的代码,不会等协程方法运行结束才继续执行
        //print("hahaha");
 }
    private IEnumerator ie;
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space))
        {
            //第一种关闭方式
            //ie = Fade();
            //StartCoroutine(ie);
            //第二种关闭方式,开启跟关闭是对应着的
            StartCoroutine("Fade");
        }
        if (Input.GetKeyDown(KeyCode.S))
        {
            //StopCoroutine(ie);
            StopCoroutine("Fade");
        }
    }
    //实现渐变改变颜色
    IEnumerator Fade()
    {
        for (; ; )//这是个死循环
        {
            //1.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);
            if (Mathf.Abs(Color.red.g-newColor.g)<=0.01f)
            {
                break;
            }
        }
    }
    //Coroutine
    //1.返回值IEnumerator
    //2.返回参数的时候使用yield return
    //3.协程方法的调用StartCoroutine(method());
    IEnumerator ChangeColor()
    {
        print("haColor");
        yield return new WaitForSeconds(3);//暂停三秒在执行后面的代码
        cube.GetComponent<MeshRenderer>().material.color = Color.blue;
        print("hahaColor");
        yield return null;
    }
}

四、跟鼠标相关事件函数OnMouseXXX讲解

public class API09OnMouseEvent : MonoBehaviour
{
    void OnMouseDown()
    {
        print("Down"+gameObject);
    }
    void OnMouseUp()
    {
        print("UP" + gameObject);
    }
    void OnMouseDrag()
    {
        print("Drag" + gameObject);
    }
    void OnMouseEnter()
    {
        print("Enter" + gameObject);
    }
    void OnMouseExit()
    {
        print("Exit" + gameObject);
    }
    void OnMouseOver()
    {
        print("Over" + gameObject);
    }
    void OnMouseUpAsButton()
    {
        print("Button");
    }
}

五、Mthf里面的静态常量

public class API10Mthf : MonoBehaviour
{
 void Start ()
    {
        //度数转弧度
        print(Mathf.Deg2Rad);
        //弧度转度数
        print(Mathf.Rad2Deg);
        //无限大的数
        print(Mathf.Infinity);
        //无限小的数
        print(Mathf.NegativeInfinity);
        //π的值
        print(Mathf.PI);
        //大于0的小数
        print(Mathf.Epsilon);
    }
 void Update ()
    {
    }
}

六、Mathf中的Clamp限定方法

Abs去绝对值
Ceil向上取整(返回的是Float)
CeilToInt向上取整(返回的是Int类型的)
Clamp把一个值限定在一个范围之内
Clamp01把值限定在0~1之间

public class API10Mthf : MonoBehaviour
{
    public Transform cube;
 void Start ()
    {
        ////度数转弧度
        //print(Mathf.Deg2Rad);
        ////弧度转度数
        //print(Mathf.Rad2Deg);
        ////无限大的数
        //print(Mathf.Infinity);
        ////无限小的数
        //print(Mathf.NegativeInfinity);
        ////π的值
        //print(Mathf.PI);
        ////大于0的小数
        //print(Mathf.Epsilon);
 }
 void Update ()
    {
        //把Time.time的值限定在1到3之间,如果比1小,就返回1,比3大就返回3,如果在1到3之间就返回它本身的值
        //cube.position = new Vector3(Mathf.Clamp(Time.time,1.0f,3.0f),0,0);
        Debug.Log(Mathf.Clamp(Time.time, 1.0f, 3.0f));
 }
    //计算伤害时用
    private int hp = 100;
    void TakeDamage()
    {
        hp -= 9;
        //if (hp<0)
        //    hp = 0;
        //简单写法
        hp = Mathf.Clamp(hp,0,100);
    }
}

七、Mathf中的常用方法

ClosestPowerOfTwo取得离2的次方的值最近的数,2,4,8,16,32
DetlaAngle(计算出两个角度之间最短的距离)
Exp(e的多少次方)
Floor向下取整(float类型 10.2取10,-10.2取-11)
FloorToInt向下取整(Int类型)
Lerp
LerpAngle
Max(取得最大值)
Min(取得最小值)
Pow(f,p)(里面有两个值,取得f的p次方,float f,float p)
Sqrt(取得参数的平方根)

public class API10Mthf : MonoBehaviour
{
    public Transform cube;
 void Start ()
    {
        ////度数转弧度
        //print(Mathf.Deg2Rad);
        ////弧度转度数
        //print(Mathf.Rad2Deg);
        ////无限大的数
        //print(Mathf.Infinity);
        ////无限小的数
        //print(Mathf.NegativeInfinity);
        ////π的值
        //print(Mathf.PI);
        ////大于0的小数
        //print(Mathf.Epsilon);
        //向下取整
        //Debug.Log(Mathf.Floor(10.0f));
        //Debug.Log(Mathf.Floor(10.2f));
        //Debug.Log(Mathf.Floor(10.7f));
        //Debug.Log(Mathf.Floor(-10.0f));
        //Debug.Log(Mathf.Floor(-10.2f));
        //Debug.Log(Mathf.Floor(-10.7f));


        //取得离2的次方的值最近的数,2,4,8,16,32
        print(Mathf.ClosestPowerOfTwo(2));//4
        print(Mathf.ClosestPowerOfTwo(3));//8
        print(Mathf.ClosestPowerOfTwo(4));//8
        print(Mathf.ClosestPowerOfTwo(5));//8
        print(Mathf.ClosestPowerOfTwo(6));//8
        print(Mathf.ClosestPowerOfTwo(30));//8
        print(Mathf.Max(1,2));//2
        print(Mathf.Max(1,2,5,3,10));//10
        print(Mathf.Min(1, 2));//1
        print(Mathf.Min(1,2,5,3,10));//1
        print(Mathf.Pow(4,3));//64
        print(Mathf.Sqrt(3));//1.73
    }
 void Update ()
    {
        //把Time.time的值限定在1到3之间,如果比1小,就返回1,比3大就返回3,如果在1到3之间就返回它本身的值
        //cube.position = new Vector3(Mathf.Clamp(Time.time,1.0f,3.0f),0,0);
        //Debug.Log(Mathf.Clamp(Time.time, 1.0f, 3.0f));
 }
    //计算伤害时用
    private int hp = 100;
    void TakeDamage()
    {
        hp -= 9;
        //if (hp<0)
        //    hp = 0;
        //简单写法
        hp = Mathf.Clamp(hp,0,100);
    }
}

八、关于游戏开发中的插值运算

Lerp(a,b,t)t是0~1的比例,假设a=9,b=20
在这里插入图片描述

public class API10Mthf : MonoBehaviour
{
    public Transform cube;
    public int a = 8;
    public int b = 20;
    public float t = 0;//必须是float类型
    void Start ()
    {
        ////度数转弧度
        //print(Mathf.Deg2Rad);
        ////弧度转度数
        //print(Mathf.Rad2Deg);
        ////无限大的数
        //print(Mathf.Infinity);
        ////无限小的数
        //print(Mathf.NegativeInfinity);
        ////π的值
        //print(Mathf.PI);
        ////大于0的小数
        //print(Mathf.Epsilon);
        //向下取整
        //Debug.Log(Mathf.Floor(10.0f));
        //Debug.Log(Mathf.Floor(10.2f));
        //Debug.Log(Mathf.Floor(10.7f));
        //Debug.Log(Mathf.Floor(-10.0f));
        //Debug.Log(Mathf.Floor(-10.2f));
        //Debug.Log(Mathf.Floor(-10.7f));
        
        //取得离2的次方的值最近的数,2,4,8,16,32
        //print(Mathf.ClosestPowerOfTwo(2));//4
        //print(Mathf.ClosestPowerOfTwo(3));//8
        //print(Mathf.ClosestPowerOfTwo(4));//8
        //print(Mathf.ClosestPowerOfTwo(5));//8
        //print(Mathf.ClosestPowerOfTwo(6));//8
        //print(Mathf.ClosestPowerOfTwo(30));//8
        //print(Mathf.Max(1,2));//2
        //print(Mathf.Max(1,2,5,3,10));//10
        //print(Mathf.Min(1, 2));//1
        //print(Mathf.Min(1,2,5,3,10));//1
        //print(Mathf.Pow(4,3));//64
        //print(Mathf.Sqrt(3));//1.73


        cube.position = new Vector3(0,0,0);
    }
 void Update ()
    {
        //把Time.time的值限定在1到3之间,如果比1小,就返回1,比3大就返回3,如果在1到3之间就返回它本身的值
        //cube.position = new Vector3(Mathf.Clamp(Time.time,1.0f,3.0f),0,0);
        //Debug.Log(Mathf.Clamp(Time.time, 1.0f, 3.0f));


        //插值
        //print(Mathf.Lerp(a,b,t));
        float c = cube.position.x;
        //float newX= Mathf.Lerp(c,10,0.1f);
        //一般不是自己定义,一般是用时间
        //嫌慢的话可以乘以速度Time.deltaTime*Speed
        float newX = Mathf.Lerp(c,10,Time.deltaTime);
        cube.position = new Vector3(newX ,0,0);
 }
    //计算伤害时用
    private int hp = 100;
    void TakeDamage()
    {
        hp -= 9;
        //if (hp<0)
        //    hp = 0;
        //简单写法
        hp = Mathf.Clamp(hp,0,100);
    }
}
内容概要:本文详细解析了2014年全国大学生电子设计竞赛C题——智能小车设计的全过程。文章首先介绍了该竞赛的背景及其重要意义,指出其不仅是对学生电子设计能力的考验,还对学生的学术成长职业发展有深远影响。随后,文章深入剖析了C题的具体要求,包括小车的起跑、行驶、超车等复杂动作,强调了硬件(如控制模块、电源模块、车体、电机模块)软件(如信号检测与控制、两车通信、节能技术、程序设计)方面的关键技术实现方法。最后,文章分享了测试与优化的经验,并总结了团队合作、知识储备实践能力的重要性,展望了电子设计领域的发展趋势。 适合人群:电子信息专业学生、电子设计爱好者及希望深入了解智能小车设计的技术人员。 使用场景及目标:①了解全国大学生电子设计竞赛的背景重要性;②掌握智能小车设计的硬件选型软件编程技巧;③学习信号检测与控制、两车通信、节能技术等关键技术;④借鉴测试与优化的经验,提升实际动手能力解决问题的能力。 阅读建议:本文内容详实,涵盖了从理论到实践的各个方面。建议读者在阅读过程中结合实际操作,逐步理解掌握智能小车设计的各项技术原理,特别是对硬件电路设计软件编程部分,可以通过搭建实验平台进行实践,加深理解。同时,关注文中提到的测试与优化策略,有助于提高实际项目的成功率。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值