一、需求
需要定时在早上8:50,中午12:00,下午13:10定时播放音乐
二、实现步骤
- 依次在unity创建背景图、主文字提示、时间文字提示、音量控制器及音量文字提示、退出按钮、播放按钮,暂停按钮
- 在Canvas下创建一个Script脚本:获取工程中的Text、Button、Slider、AudioClip组件,及音乐文件“AudioClip"。将音乐文件附加到AudioSource的AudioClip上、并将上面的组件关联到代码上;
public Text tipstext;public Text SoundText;public Text dateText;//退出、播放、暂停按钮public Button exitButton;public Button playButton;public Button stopButton;public Slider volSilder;//音乐资源private AudioSource bgm;
- 依次实现文本显示、按钮事件监听、音乐播放、时间逻辑判断,整个需求就完成了
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using Unity.Burst.Intrinsics;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.UI;
//using UnityEngine.UIElements;public class MusicControl : MonoBehaviour
{ //文字提示public Text tipstext;public Text SoundText;public Text dateText;//退出、播放、暂停按钮public Button exitButton;public Button playButton;public Button stopButton;public Slider volSilder;//音乐资源private AudioSource bgm;//时间DateTime nowTime = DateTime.Now;string NowTimeStr = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");// Start is called before the first frame updatevoid Start(){//初始化按钮InitButtonClick();//获取音乐组主键,并设置音量大小bgm = transform.GetComponent<AudioSource>();bgm.volume = 0.5f;//初始化文字提示ShowText(tipstext,"等待音乐播放!");ShowText(SoundText,"当前音量50%,滑动设置音量大小");ShowText(dateText,NowTimeStr);//不设置全屏Screen.fullScreen =false;}// Update is called once per framevoid Update(){//在指定时间执行OnTimePlay();}// 播放音乐 isplaying:是否进行音乐播放private void PlayMusic(bool isplaying){//Debug.Log("调用播放音乐:"+isplaying);if(bgm.enabled ==false){Debug.Log("找不到音乐文件");return;}if(isplaying==true &&bgm.isPlaying==false){bgm.Play();//展示播放音乐提示ShowText(tipstext,"正在播放音乐:"+bgm.clip.name);Debug.Log("正在执行播放音乐");//音乐播放时,每5秒检查一次音乐状态InvokeRepeating("CheckMusicPlaying",0,5.0f);}else if(isplaying == true && bgm.isPlaying ==true){//正在播放音乐时,传 播放参数,提示不能重复播放ShowText(tipstext,"正在播放音乐中,不要重复播放!");}else if(isplaying == false && bgm.isPlaying==true){//正在播放,传 暂停播放参数,停止播放bgm.Stop();ShowText(tipstext,bgm.clip.name +"音乐已停止!");}else if(isplaying ==false && bgm.isPlaying ==false){//未播放,传暂停播放参数,提示不能重复暂停ShowText(tipstext,"音乐已停止,不要重复点击停止播放按钮!");}}//更新Text组件显示的内容private void ShowText(Text gmtxet,string str){gmtxet.text=str;}//初始化按钮点击监听事件void InitButtonClick(){if(exitButton !=null){exitButton.onClick.AddListener(OnExitButtonClick);}else{Debug.Log("退出按钮未获取到!");}if(playButton != null){playButton.onClick.AddListener(OnPlayButtonClick);}else{Debug.Log("播放按钮未获取到!");}if(stopButton != null){stopButton.onClick.AddListener(OnStopButtonClick);}else{Debug.Log("停止按钮未获取到!");}if(volSilder != null){volSilder.onValueChanged.AddListener(OnVolSliderChange);}else{Debug.Log("音量按钮未获取到!");}}//执行退出按钮点击事件void OnExitButtonClick(){Debug.Log("退出");#if UNITY_EDITOR//在编辑器也能退出UnityEditor.EditorApplication.isPlaying = false;#elseApplication.Quit();#endif}//执行点击播放按钮事件void OnPlayButtonClick(){ PlayMusic(true); }//执行点击暂停按钮事件void OnStopButtonClick(){//Debug.Log("停止");PlayMusic(false);}//检查音量播放,音乐播放初始化时调用void CheckMusicPlaying(){if(bgm !=null &&bgm.isPlaying){Debug.Log("音乐正在播放");}else{Debug.Log("音乐已经播放完成");ShowText(tipstext,"音乐播放完成!");//取消检查音乐播放监听事件CancelInvoke("CheckMusicPlaying");}}//已被引用,监听时间 触发播放void OnTimePlay(){//获取当前时间nowTime = DateTime.Now;NowTimeStr =nowTime.ToString("yyyy-MM-dd HH:mm:ss");//展示当前时间ShowText(dateText,NowTimeStr);//Debug.Log("时:"+nowTime.Hour+",分:"+nowTime.Minute);if(nowTime.Hour==8 && nowTime.Minute ==50 && nowTime.Second==0 ){//避免重复播放if(bgm.isPlaying==false){PlayMusic(true); }}else if(nowTime.Hour==12 && nowTime.Minute ==0 && nowTime.Second==0){if(bgm.isPlaying==false){PlayMusic(true); }}else if(nowTime.Hour==13 && nowTime.Minute ==10 && nowTime.Second==0 ){if(bgm.isPlaying==false){Debug.Log("下午:"+nowTime.Hour+"时,"+nowTime.Minute+"分,"+nowTime.Second+"秒");PlayMusic(true); }}}//音量滑动键改变public void OnVolSliderChange(float value){//设置音乐音量大小bgm.volume= value;//float.ToString P表示创建一个百分比字符串,P2保留两位小数ShowText(SoundText,"音量:"+value.ToString("P0"));}}