Unity常用API方法和类

557 阅读6分钟

1、事件函数

    void Awake()
    {
        Debug.Log("Awake");
    }
    void OnEnable()
    {
        Debug.Log("Enable");
    }
    void Start () 
    {
        Debug.Log("Start");
    }
    void FixedUpdate()
    {
        Debug.Log("FixedUpdate");
    }
    
    void Update()
    {
        Debug.Log("Update");
    }
    void LateUpdate()
    {
        Debug.Log("LateUpdate");
    }
    void OnApplicationPause()
    {
        Debug.Log("OnApplicationPause");
    }
    void OnDisable()
    {
        Debug.Log("Disable");
    }
    void OnApplicatoinQuit()
    {
        Debug.Log("OnApplicationQuit");
    }
    void Reset()
    {
        Debug.Log("Reset");
    }
    void OnDestroy()
    {
        Debug.Log("OnDestroy");
    }

2、Time类

    public Transform cube;
    public int runCount = 10000;
    void Start()
    {
        //Debug.Log("Time.deltaTime:" + Time.deltaTime);//从最后一帧到当前帧的间隔(以秒为单位)
        //Debug.Log("Time.fixedDeltaTime:" + Time.fixedDeltaTime);//执行物理和其他固定帧率更新(如 MonoBehaviour 的 FixedUpdate)的时间间隔(以秒为单位)
        //Debug.Log("Time.fixedTime:" + Time.fixedTime);//自上次 FixedUpdate 开始以来的时间(只读)。这是游戏开始后的秒数。
        //Debug.Log("Time.frameCount:" + Time.frameCount);//自游戏开始以来的总帧数
        //Debug.Log("Time.realtimeSinceStartup:" + Time.realtimeSinceStartup);//游戏开始以来的实际时间
        //Debug.Log("Time.smoothDeltaTime:" + Time.smoothDeltaTime);//经过平滑处理的 Time.deltaTime
        //Debug.Log("Time.time:" + Time.time);//此帧开始的时间
        //Debug.Log("Time.timeScale:" + Time.timeScale);//时间流逝的尺度
        //Debug.Log("Time.timeSinceLevelLoad:" + Time.timeSinceLevelLoad);//自该帧开始以来的时间(只读)。这是自最后一个非加法场景完成加载以来的时间(以秒为单位)
        //Debug.Log("Time.unscaledTime:" + Time.unscaledTime);//此帧的与 timeScale 无关的时间(只读)。这是自游戏开始以来的秒数        float time00 = Time.realtimeSinceStartup;
        for (int i = 0; i < runCount; i++)
        {
            Method0();
        }
        float time1 = Time.realtimeSinceStartup;
        for (int i = 0; i < runCount; i++)
        {
            Method1();
        }
        float time2 = Time.realtimeSinceStartup;
        Debug.Log(time2 - time1);

        float time3 = Time.realtimeSinceStartup;
        for (int i = 0; i < runCount; i++)
        {
            Method2();
        }
        float time4 = Time.realtimeSinceStartup;
        Debug.Log(time4 - time3);
    }
    void Update () 
    {
        //Debug.Log("Time.deltaTime:" + Time.deltaTime);
        //Debug.Log("Time.fixedDeltaTime:" + Time.fixedDeltaTime);
        //Debug.Log("Time.fixedTime:" + Time.fixedTime);
        //Debug.Log("Time.frameCount:" + Time.frameCount);
        //Debug.Log("Time.realtimeSinceStartup:" + Time.realtimeSinceStartup);
        //Debug.Log("Time.smoothDeltaTime:" + Time.smoothDeltaTime);
        //Debug.Log("Time.time:" + Time.time);
        //Debug.Log("Time.timeScale:" + Time.timeScale);
        //Debug.Log("Time.timeSinceLevelLoad:" + Time.timeSinceLevelLoad);
        //Debug.Log("Time.unscaledTime:" + Time.unscaledTime);
        //cube.Translate(Vector3.forward *Time.deltaTime*3 );
        //Time.timeScale = 3f; 
    }

    void Method0()
    {

    }
    void Method1()
    {
        int i = 2;
        i += 2;
        i += 2;
    }
    void Method2()
    {
        int i = 2;
        i *= 2;
        i *= 2;
    }

3、GameObject

    public GameObject go;
    void Start()
    {
        //1,第一种创建方法 
        //GameObject go = new GameObject("Cube");
        //2,第二种
        //根据prefab 
        //根据另外一个游戏物体
        //GameObject.Instantiate(prefab);//可以根据prefab 或者 另外一个游戏物体克隆
        //3,第三种 创建原始的几何体
        //GameObject.CreatePrimitive(PrimitiveType.Plane);
        //GameObject go = GameObject.CreatePrimitive(PrimitiveType.Cube);
        //go.AddComponent<Rigidbody>();
        //// go.AddComponent<API01EventFunction>();

        //Debug.Log(go.activeInHierarchy);//定义 GameObject 在 Scene 中是否处于活动状态。
        //go.SetActive(false);//根据给定的值 true 或 /false/,激活/停用 GameObject。
        //Debug.Log(go.activeInHierarchy);
        //Debug.Log(go.tag);
        //Debug.Log(go.name);
        //Debug.Log(go.GetComponent<Transform>().name);

        //Light light = FindObjectOfType<Light>();
        //light.enabled = false;
        //Transform[] ts= FindObjectsOfType<Transform>();//不查找 未激活的游戏物体
        //foreach (Transform t in ts)
        //{
        //    Debug.Log(t);
        //}

        //GameObject go = GameObject.Find("Main Camera");
        //GameObject[] gos= GameObject.FindGameObjectsWithTag("MainCamera");
        GameObject go = GameObject.FindGameObjectWithTag("Finish");
        go.SetActive(false);
    }

4、Massage

    public GameObject target;
    void Start()
    {
        //target.BroadcastMessage("Attack", null, SendMessageOptions.DontRequireReceiver);//调用此游戏对象或其任何子项中的每个 MonoBehaviour 上名为 methodName 的方法。
        //target.SendMessage("Attack", null, SendMessageOptions.DontRequireReceiver);//调用此游戏对象中的每个 MonoBehaviour 上名为 methodName 的方法。
        target.SendMessageUpwards("Attack", null, SendMessageOptions.DontRequireReceiver);//调用此游戏对象中的每个 MonoBehaviour 上或此行为的每个父级上名为 methodName 的方法。
  }
    void Attack()
    {
        Debug.Log(this.gameObject + "正在进行攻击");  
    }

5、GetComponent

    public GameObject target;
    void Start()
    {
        Transform cube = target.GetComponent<Transform>();
        Transform t = target.GetComponent<Transform>();
        Debug.Log(cube);
        Debug.Log(t);
        Debug.Log("---------------------------------");
        Transform[] cubes = target.GetComponents<Transform>();
        Debug.Log(cubes.Length);
        Debug.Log("---------------------------------");
        cubes = target.GetComponentsInChildren<Transform>();
        foreach (Transform c in cubes)
        {
            Debug.Log(c);
        }
        Debug.Log("---------------------------------");
        cubes = target.GetComponentsInParent<Transform>();
        foreach (Transform c in cubes)
        {
            Debug.Log(c);
        }
    }

6、MonoBehaviour

    void Start()
    {
        Debug.Log(this.isActiveAndEnabled);//是否已激活并启用 Behaviour
        Debug.Log(this.enabled);
        enabled = false;
        Debug.Log(name);
        Debug.Log(tag);
        Debug.Log(gameObject);
        Debug.Log(transform);
    }

7、Invoke

    void Start()
    {
        //Invoke("Attack",3);//在 time 秒后调用 methodName 方法。
        InvokeRepeating("Attack", 4, 2);//在 time 秒后调用 methodName 方法,然后每 repeatRate 秒调用一次。
        CancelInvoke("Attack");//取消该 MonoBehaviour 上的所有 Invoke 调用。
    }
    void Update()
    {
        bool res = IsInvoking("Attack");//是否有任何待处理的 methodName 调用?
        print(res);
    }
    void Attack()
    {
        print("攻击目标");
    }

8、Coroutine

    public GameObject cube;
    void Start()
    {
        //print("hha");
        ////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 (; ; )
        {
            //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;
            }
        }
    }
    //Coroutines
    //1,返回值是IEnumerator
    //2,返回参数的时候使用yield return  null/0;
    //3,协程方法的调用StartCoroutine(method())
    IEnumerator ChangeColor()
    {
        print("hahaColor");
        yield return new WaitForSeconds(3);
        cube.GetComponent<MeshRenderer>().material.color = Color.blue;
        print("hahaColor");
        yield return null;
    }

9、OnMouseEventFunction

    void OnMouseDown()
    {
        print("Down"+gameObject);
    }
    void OnMouseUp()
    {
        print("up" + gameObject);
    }
    void OnMouseDrag()
    {
        print("Drag" + gameObject);
    }
    void OnMouseEnter()
    {
        print("Enter");
    }
    void OnMouseExit()
    {
        print("Exit");
    }
    void OnMouseOver()
    {
        print("Over");
    }
    void OnMouseUpAsButton()
    {
        print("Button" + gameObject);
    }

10、Mathf

    public Transform cube;
    public int a = 8;
    public int b = 20;
    public float t = 0;
    public float speed = 3;
  // Use this for initialization
    void Start()
    {
        //print(Mathf.Deg2Rad);//度到弧度换算常量
        //print(Mathf.Rad2Deg);//弧度到度换算常量
        //print(Mathf.Infinity);//正无穷大的表示形式
        //print(Mathf.NegativeInfinity);//负无穷大的表示形式
        //print(Mathf.PI);//π
        //print(Mathf.Epsilon);//微小浮点值

        //Debug.Log(Mathf.Floor(10.0F));//返回小于或等于 f 的最大整数
        //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 4 8 16 32
        //print(Mathf.ClosestPowerOfTwo(2));//4//返回最接近的 2 的幂值
        //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 //返回 f 的 p 次幂
        //print(Mathf.Sqrt(3));//1.6//返回 f 的平方根

        cube.position = new Vector3(0, 0, 0);
  }
    void Update()
    {
        //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));//在 a 与 b 之间按 t 进行线性插值

        //float x = cube.position.x;
        ////float newX = Mathf.Lerp(x, 10, Time.deltaTime);
        //float newX = Mathf.MoveTowards(x, 10, Time.deltaTime*speed);
        //cube.position = new Vector3(newX, 0, 0);

        //print(Mathf.MoveTowards(a, b, t));//将值 current 向 target 靠近

        //print(Mathf.PingPong(t, 20));
        cube.position = new Vector3(5+Mathf.PingPong(Time.time*speed, 5), 0, 0);//PingPong 返回一个值,该值将在值 0 与 length 之间递增和递减
    }
    private int hp = 100;
    void TakeDamage()
    {
        hp -= 9;

        //if (hp < 0)
        //    hp = 0;
        hp = Mathf.Clamp(hp,0, 100);
    }

11、Input

    public Transform cube;
    void Update()
    {
        //if (Input.GetKeyDown("left shift"))//在用户开始按下 name 标识的键的帧期间返回 true
        //{
        //    print("left shift");
        //}
        //if (Input.GetKeyDown(KeyCode.Space))
        //{
        //    print("KeyDOwn");
        //}
        //if (Input.GetKeyUp(KeyCode.Space))//在用户释放 name 标识的键的帧期间返回 true
        //{
        //    print("KeyUp");
        //}
        //if (Input.GetKey(KeyCode.Space))
        //{
        //    print("Key");
        //}

        //if (Input.GetMouseButton(0))//返回是否按下了给定的鼠标按钮
        //    Debug.Log("Pressed left click.");

        //if (Input.GetMouseButton(1))
        //    Debug.Log("Pressed right click.");

        //if (Input.GetMouseButton(2))
        //    Debug.Log("Pressed middle click.");

        //if (Input.GetMouseButtonDown(0))//在用户按下给定鼠标按钮的帧期间返回 true
        //    Debug.Log("Pressed left click.");

        //if (Input.GetMouseButtonDown(1))
        //    Debug.Log("Pressed right click.");

        //if (Input.GetMouseButtonDown(2))
        //    Debug.Log("Pressed middle click.");

        //if (Input.GetButtonDown("Fire1"))//在用户按下由 buttonName 标识的虚拟按钮的帧期间返回 true
        //{
        //    print("Fire1 Down");
        //}

        //if (Input.GetButtonDown("Horizontal"))
        //{
        //    print("Horizontal Down"); 
        //}
        //print( );

        //cube.Translate(Vector3.right * Time.deltaTime * Input.GetAxis("Horizontal")*10);
        //cube.Translate(Vector3.right * Time.deltaTime * Input.GetAxisRaw("Horizontal")*10);

        //if (Input.anyKeyDown)//在用户按任意键或鼠标按钮后的第一帧返回 true
        //{
        //    print("any key down");
        //}
        print(Input.mousePosition);
  }

12、Vector2

    void Start()
    {
        //print(Vector2.down);//(0,-1)
        //print(Vector2.up);//(0,1)
        //print(Vector2.left);//(-1,0)
        //print(Vector2.right);//(1,0)
        //print(Vector2.one);//(1,1)
        //print(Vector2.zero);//(0,0)


        //Vector2 a = new Vector2(2, 2);
        //Vector2 b = new Vector2(3, 4);
        //print(a.magnitude);//返回该向量的长度
        //print(a.sqrMagnitude);//返回该向量的平方长度
        //print(b.magnitude);
        //print(b.sqrMagnitude);

        //print(a.normalized);//返回 magnitude 为 1 时的该向量
        //print(b.normalized);


        //print(a.x + "," + a.y);
        //a.Normalize();
        //print(a[0] + "," + a[1]);

        //向量是结构体,是值类型,要整体赋值
        //transform.position = new Vector3(3, 3, 3);
        //Vector3 pos = transform.position;
        //pos.x = 10;
        //transform.position = pos;

        Vector2 a = new Vector2(2, 2);
        Vector2 b = new Vector2(3, 4);
        Vector2 c = new Vector2(3, 0);

        //print(Vector2.Angle(a, b));//返回 from 与 to 之间的无符号角度
        //print(Vector2.Angle(a, c));
        //print(Vector2.ClampMagnitude(c, 2));//返回 vector 的副本,其大小被限制为 /maxLength/
        //print(Vector2.Distance(b, c));//返回 a 与 b 之间的距离

        //print(Vector2.Lerp(a, b, 0.5f));//2.5 3//在向量 a 与 b 之间按 t 进行线性插值
        //print(Vector2.LerpUnclamped(a, b, 0.5f));//2.5 3//在向量 a 与 b 之间按 t 进行线性插值

        //print(Vector2.Lerp(a, b, 2f));//b 3,4
        //print(Vector2.LerpUnclamped(a, b, 2f));

        //print(Vector2.Max(a, b));
        //print(Vector2.Min(a, b));

        Vector2 res = b - a;//1,2
        print(res);
        print(res * 10);
        print(res / 5);
        print(a + b);
        print(a == b);

  }
    public Vector2 a = new Vector2(2, 2);
    public Vector2 target = new Vector2(10, 3);
  // Update is called once per frame
  void Update () {
        a = Vector2.MoveTowards(a, target, Time.deltaTime);//将点 current 移向 /target/
  }

13、Random

    public Transform cube;
    void Start()
    {
        //Random.InitState( (int)System.DateTime.Now.Ticks );
    }
    void Update()
    {
        //print(Random.Range(4, 10));
        //print(Random.Range(4, 5f));
        if (Input.GetKeyDown(KeyCode.Space))
        {
            print(UnityEngine.Random.Range(4, 100));
            print((int)System.DateTime.Now.Ticks);
        }
        //cube.position = Random.insideUnitCircle * 5;//返回半径为 1.0 的圆内或圆上的随机点
        cube.position = UnityEngine.Random.insideUnitSphere * 5;//返回半径为 1.0 的球体内或球体上的随机点
    }

14、Quaternion

    public Transform cube;
    public Transform player;
    public Transform enemy;
    void Start()
    {
        //cube.rotation = new Vector3(10, 0, 0);
        //cube.eulerAngles = new Vector3(10, 0, 0);
        //print(cube.eulerAngles);//返回或设置旋转的欧拉角表示
        //print(cube.rotation);

        //cube.eulerAngles = new Vector3(45, 45, 45);
        //cube.rotation = Quaternion.Euler(new Vector3(45, 45, 45));//返回一个旋转,它围绕 z 轴旋转 z 度、围绕 x 轴旋转 x 度、围绕 y 轴旋转 y 度(按该顺序应用)
        //print(cube.rotation.eulerAngles);
    }
    void Update()
    {
        if (Input.GetKey(KeyCode.Space))
        {
            Vector3 dir = enemy.position - player.position;
            dir.y = 0;
            Quaternion target = Quaternion.LookRotation(dir);//使用指定的 forward 和 upwards 方向创建旋转
            player.rotation = Quaternion.Lerp(player.rotation, target, Time.deltaTime);//在 a 和 b 之间插入 t,然后对结果进行标准化处理。参数 t 被限制在 [0, 1] 范围内
        }
    }

15、RigidbodyPosition

public Rigidbody playerRgd;
    public Transform enemy;
    public int force;
    void Update()
    {
        //playerRgd.position = playerRgd.transform.position + Vector3.forward * Time.deltaTime*10;//刚体的位置
        //playerRgd.MovePosition(playerRgd.transform.position + Vector3.forward * Time.deltaTime*10);//将运动 Rigidbody 向 position 移动

        //if (Input.GetKey(KeyCode.Space))
        //{
        //    Vector3 dir = enemy.position - playerRgd.position;
        //    dir.y = 0;
        //    Quaternion target = Quaternion.LookRotation(dir);
        //    playerRgd.MoveRotation(Quaternion.Lerp(playerRgd.rotation, target, Time.deltaTime));
        //}
        playerRgd.AddForce(Vector3.forward * force);//向 Rigidbody 添加力
    }

16、Camera

    private Camera mainCamera;
    void Start()
    {
        //mainCamera= GameObject.Find("MainCamera").GetComponent<Camera>();
        mainCamera = Camera.main;
    }
    void Update()
    {
        //Ray ray = mainCamera.ScreenPointToRay(Input.mousePosition);
        //RaycastHit hit;
        //bool isCollider = Physics.Raycast(ray, out hit);
        //if (isCollider)
        //{
        //    Debug.Log(hit.collider);
        //}
        Ray ray = mainCamera.ScreenPointToRay(new Vector3(200, 200, 0));//返回从摄像机通过屏幕点的光线
        Debug.DrawRay(ray.origin, ray.direction * 10, Color.yellow);
    }
    void RayHit()
    {
        if (Input.GetMouseButtonDown(0))
        {
            Ray ray;
            RaycastHit hit;
            GameObject go;//碰撞器碰撞到的物体
            ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            if (Physics.Raycast(ray, out hit))
            {
                go = hit.collider.gameObject;
                if (go.name.Equals("电源btn"))
                {

                }
            }
        }
    }

17、Application_xxxPath

    void Start()
    {
        print(Application.dataPath);//包含目标设备上的游戏数据文件夹路径
        print(Application.streamingAssetsPath);//StreamingAssets 文件夹的路径
        print(Application.persistentDataPath);//包含持久数据目录的路径
        print(Application.temporaryCachePath);//包含临时数据/缓存目录的路径 
    }

18、Application

    void Start()
    {
        print(Application.identifier);//在运行时返回应用程序标识。在 Apple 平台上为保存在 info.plist 文件中的“bundleIdentifier”,在 Android 平台上为 AndroidManifest.xml 中的“package”
        print(Application.companyName);//返回应用程序公司名称
        print(Application.productName);//返回应用程序产品名称
        print(Application.installerName);//返回安装应用程序的商店或包的名称
        print(Application.installMode);//返回应用程序安装模式
        print(Application.isEditor);//是否在 Unity Editor 内运行?
        print(Application.isFocused);//播放器当前是否具有焦点?
        print(Application.isMobilePlatform);//当前的运行时平台是否为已知的移动平台?
        print(Application.isPlaying);//在任何类型的已构建播放器中调用时,或者在播放模式的编辑器中调用时,返回 true(只读)
        print(Application.platform);//返回游戏运行平台
        print(Application.unityVersion);//用于播放内容的 Unity 运行时版本
        print(Application.version);//返回应用程序版本号
        print(Application.runInBackground);//  当应用程序在后台时,播放器是否应该运行?

        Application.Quit();//退出播放器应用程序
        Application.OpenURL("www.sikiedu.com");//遵循应用程序当前平台和环境的权限和限制,打开指定 URL。这采用不同方式进行处理(具体取决于 URL 的性质),并具有不同的安全限制(具体取决于运行时平台)

    }
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space))
        {
            //UnityEditor.EditorApplication.isPlaying = false;
            SceneManager.LoadScene(1);
        }
    }

19、SceneManager

    void Start()
    {
        print(SceneManager.sceneCount);//当前加载的场景总数
        print(SceneManager.sceneCountInBuildSettings);//Build Settings 中的场景数量
        print(SceneManager.GetActiveScene().name);//获取当前活动的场景
        print(SceneManager.GetSceneAt(0).name);//获取 SceneManager 的已加载场景列表中索引处的场景
        SceneManager.activeSceneChanged += OnActiveSceneChanged;//订阅此事件可在活动场景发生变化时收到通知
        SceneManager.sceneLoaded += OnSceneLoaded;//向此事件添加委托,以在加载场景时收到通知
    }
    void OnActiveSceneChanged(Scene a, Scene b)
    {
        print(a.name);
        print(b.name);
    }
    void OnSceneLoaded(Scene a, LoadSceneMode mode)
    {
        print(a.name + "" + mode);
    }
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space))
        {
            //SceneManager.LoadScene(1);//按照 Build Settings 中的名称或索引加载场景
            //SceneManager.LoadScene("02 - MenuScene");
            print(SceneManager.GetSceneByName("02 - MenuScene").buildIndex);//搜索已加载的场景,查找包含给定名称的场景
            SceneManager.LoadScene(1);
        }
    }

20、Audio Source UnityEngine

image.png

image.png

    public EazySoundDemoAudioControls[] AudioControls;//使用类创建数组
    public Slider globalVolSlider;
    public Slider globalMusicVolSlider;
    public Slider globalSoundVolSlider;
    
    EazySoundDemoAudioControls audioControl = AudioControls[0];//获取对象
    
    //定义类
    [System.Serializable]
    public struct EazySoundDemoAudioControls
    {
        public AudioClip audioclip;
        public Button playButton;
        public Button pauseButton;
        public Button stopButton;
        public Slider volumeSlider;
        public Text pausedStatusTxt;
    }
    
    public AudioSource audioSource;//音频组件
    public AudioClip audioclip;//音频片段 mp3
    void AudioTest()
    {
        audioclip = audioSource.clip;//要播放的默认 AudioClip。
        bool isMute = audioSource.mute;//使 AudioSource 静音/取消静音。静音时将音量设置为 0,取消静音则恢复原来的音量。
        float time = audioSource.time;//播放位置(以秒为单位)。
        float value = audioSource.volume;//音频源的音量(0.0 到 1.0)。
        audioSource.Play();//播放 clip。
        audioSource.Pause();//暂停 clip。
        audioSource.UnPause();//恢复播放该 AudioSource。
        audioSource.Stop();//停止 clip。
    }

21、Video Player UnityEngine.Video;

image.png

    public VideoPlayer videoPlayer;
    public VideoClip videoClip;
    void VideoTest()
    {
        videoClip = videoPlayer.clip;//VideoPlayer 正在播放的剪辑。
        videoPlayer.source = VideoSource.VideoClip;//VideoPlayer 用于播放的源。枚举类型
        string path = videoPlayer.url;//VideoPlayer 将从中读取内容的文件或 HTTP URL。
        double time = videoPlayer.time;//VideoPlayer.texture 中当前可用帧的准备时间。
        videoPlayer.Play();//开始播放
        videoPlayer.Pause();//暂停播放
        videoPlayer.Stop();//停止播放
    }