当前位置: 首页> 文旅> 文化 > ui页面设计规范_站长之家seo查找_网络营销有哪些模式_百度搜索推广创意方案

ui页面设计规范_站长之家seo查找_网络营销有哪些模式_百度搜索推广创意方案

时间:2025/7/12 19:29:32来源:https://blog.csdn.net/qq_42047805/article/details/143215470 浏览次数:0次
ui页面设计规范_站长之家seo查找_网络营销有哪些模式_百度搜索推广创意方案

目录

前言

一、拼UI

二、上代码


前言

 效果如图:(因为是GIF格式,录不上音频)


一、拼UI

1.新建空物体添加AudioSource,给AudioSource添加音频文件,取消勾选PlayOnAwake,勾选上Loop

2.创建Slider,用于控制音频进度

3.创建Button,用于控制播放和暂停

4.创建两个Text,分别为当前进度时间和总音频时间

5.准备好播放和暂停的2个UI素材

二、上代码

代码如下:

using UnityEngine;
using UnityEngine.UI;public class SZJCAudioProgressBar : MonoBehaviour
{public AudioSource audioSource;        // 音频源public Slider progressBar;              // 进度条public Text currentTimeText;            // 当前时间文本public Text totalTimeText;              // 总时间文本public Button playPauseButton;          // 播放/暂停按钮public Sprite playSprite;//播放图片public Sprite PauseSprite;//暂停图片private bool isPlaying = false;         // 播放状态标记void Start(){// 将 Slider 的值设置为 0progressBar.value = 0;// 设置总时间文本totalTimeText.text = FormatTime(audioSource.clip.length);// 添加事件监听器progressBar.onValueChanged.AddListener(OnProgressBarValueChanged);// 添加按钮点击事件playPauseButton.onClick.AddListener(TogglePlayPause);}void Update(){// 更新进度条的值和当前时间文本if (audioSource.isPlaying){progressBar.value = audioSource.time / audioSource.clip.length;currentTimeText.text = FormatTime(audioSource.time);}else if (audioSource.time >= audioSource.clip.length) // 检查是否播放完毕{// 将播放状态设置为 falseisPlaying = false;playPauseButton.GetComponent<Image>().sprite = playSprite; // 更新按钮文本progressBar.value = 1; // 将进度条设置为满currentTimeText.text = FormatTime(audioSource.clip.length); // 更新当前时间文本为总时间}}// 切换播放和暂停状态private void TogglePlayPause(){if (isPlaying){audioSource.Pause();playPauseButton.GetComponent<Image>().sprite = playSprite; // 更新按钮文本}else{audioSource.Play();playPauseButton.GetComponent<Image>().sprite = PauseSprite; // 更新按钮文本}isPlaying = !isPlaying; // 切换播放状态}// 当 Slider 的值改变时调用此方法private void OnProgressBarValueChanged(float value){if (value < 1){// 根据进度条的值设置音频播放的时间audioSource.time = value * audioSource.clip.length;}}// 格式化时间为 "分:秒" 格式private string FormatTime(float time){int minutes = Mathf.FloorToInt(time / 60);int seconds = Mathf.FloorToInt(time % 60);return string.Format("{0:00}:{1:00}", minutes, seconds);}
}

        直接把代码找个地方放上去,然后把刚才创建的内容依次拖拽上就好了。

        另外,笔者给playSprite和pauseSprite拖拽的是播放和暂停的两个图片素材,随便找个俩图片代替就行,不然会报空哈~

关键字:ui页面设计规范_站长之家seo查找_网络营销有哪些模式_百度搜索推广创意方案

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: