当前位置: 首页 > wzjs >正文

湖南平台网站建设推荐codeus企业wordpress

湖南平台网站建设推荐,codeus企业wordpress,鄂尔多斯建设招投标网站,彩票网站是静态动态关键点 Unity VideoPlayer 播放结束事件Unity AudioSource 播放检测 Unity音视频播放监听器封装笔记:VideoPlayer AudioSource事件触发与编辑器扩展 在 Unity 的多媒体开发中,我们经常需要监听 VideoPlayer 或 AudioSource 的播放状态,以便…

关键点

  • Unity VideoPlayer 播放结束事件
  • Unity AudioSource 播放检测

在这里插入图片描述

Unity音视频播放监听器封装笔记:VideoPlayer + AudioSource事件触发与编辑器扩展

在 Unity 的多媒体开发中,我们经常需要监听 VideoPlayerAudioSource 的播放状态,以便在开始播放或播放结束时触发一系列操作,例如切换 UI、播放动画、调用某脚本的方法等。

为了提升开发效率与复用性,本文记录如何封装 可复用、可配置、可挂载 UnityEvent 的监听器组件,并通过 自定义 Inspector 实现良好的编辑器体验。


1. 监听 VideoPlayer 播放事件并触发脚本方法

Unity 的 VideoPlayer 提供了两个关键事件:

  • started:视频开始播放
  • loopPointReached:视频播放完成(非循环模式)

我们封装一个脚本 VideoPlayerEventListener.cs 来监听上述事件,并挂载 UnityEvent,供你在 Inspector 中拖拽执行目标方法(如 脚本A.Exec())。

VideoPlayerEventListener.cs

using UnityEngine;
using UnityEngine.Events;
using UnityEngine.Video;[RequireComponent(typeof(VideoPlayer))]
public class VideoPlayerEventListener : MonoBehaviour
{[Header("视频开始播放时触发")]public UnityEvent onVideoStarted;[Header("视频播放完成时触发")]public UnityEvent onVideoEnded;private VideoPlayer videoPlayer;private bool hasStarted = false;void Awake(){videoPlayer = GetComponent<VideoPlayer>();}void OnEnable(){videoPlayer.started += OnVideoStarted;videoPlayer.loopPointReached += OnVideoEnded;}void OnDisable(){videoPlayer.started -= OnVideoStarted;videoPlayer.loopPointReached -= OnVideoEnded;}private void OnVideoStarted(VideoPlayer vp){if (!hasStarted){hasStarted = true;onVideoStarted?.Invoke();}}private void OnVideoEnded(VideoPlayer vp){hasStarted = false;onVideoEnded?.Invoke();}
}

2. 自定义 Inspector 提升编辑器体验

为了让 UnityEvent 在 Inspector 中更直观易用,我们还封装了一个自定义编辑器:

VideoPlayerEventListenerEditor.cs

using UnityEditor;
using UnityEngine;[CustomEditor(typeof(VideoPlayerEventListener))]
public class VideoPlayerEventListenerEditor : Editor
{SerializedProperty onVideoStarted;SerializedProperty onVideoEnded;void OnEnable(){onVideoStarted = serializedObject.FindProperty("onVideoStarted");onVideoEnded = serializedObject.FindProperty("onVideoEnded");}public override void OnInspectorGUI(){serializedObject.Update();var videoPlayer = (target as VideoPlayerEventListener).GetComponent<UnityEngine.Video.VideoPlayer>();if (videoPlayer == null){EditorGUILayout.HelpBox("缺少 VideoPlayer 组件。", MessageType.Error);}else{EditorGUILayout.HelpBox("监听 VideoPlayer 播放状态,并触发 UnityEvent。", MessageType.Info);EditorGUILayout.PropertyField(onVideoStarted, new GUIContent("🎬 视频开始播放"));EditorGUILayout.PropertyField(onVideoEnded, new GUIContent("🏁 视频播放结束"));}serializedObject.ApplyModifiedProperties();}
}

将此脚本放入 Editor 文件夹中即可自动生效。


3. 监听 AudioSource 音频播放状态

不同于 VideoPlayerAudioSource 并没有原生的播放完成事件。因此我们通过 Update() 方法持续检测播放状态,并提供播放进度(progress)供 UI 显示。

AudioSourceEventListener.cs

using UnityEngine;
using UnityEngine.Events;[RequireComponent(typeof(AudioSource))]
public class AudioSourceEventListener : MonoBehaviour
{[Header("音频开始播放时触发")]public UnityEvent onAudioStarted;[Header("音频播放完成时触发")]public UnityEvent onAudioEnded;[Range(0f, 1f), Tooltip("当前播放进度 (仅查看)")]public float progress;private AudioSource audioSource;private bool hasStarted = false;private bool hasEnded = false;void Awake(){audioSource = GetComponent<AudioSource>();}void Update(){if (audioSource.clip == null)return;if (!hasStarted && audioSource.isPlaying){hasStarted = true;hasEnded = false;onAudioStarted?.Invoke();}if (audioSource.isPlaying){progress = audioSource.time / audioSource.clip.length;}if (hasStarted && !audioSource.isPlaying && !hasEnded && audioSource.time >= audioSource.clip.length){hasEnded = true;onAudioEnded?.Invoke();}}
}

AudioSourceEventListenerEditor.cs

using UnityEditor;
using UnityEngine;[CustomEditor(typeof(AudioSourceEventListener))]
public class AudioSourceEventListenerEditor : Editor
{SerializedProperty onAudioStarted;SerializedProperty onAudioEnded;SerializedProperty progress;void OnEnable(){onAudioStarted = serializedObject.FindProperty("onAudioStarted");onAudioEnded = serializedObject.FindProperty("onAudioEnded");progress = serializedObject.FindProperty("progress");}public override void OnInspectorGUI(){serializedObject.Update();EditorGUILayout.HelpBox("监听 AudioSource 播放状态,并触发 UnityEvent。", MessageType.Info);EditorGUILayout.PropertyField(onAudioStarted, new GUIContent("🎧 音频开始播放"));EditorGUILayout.PropertyField(onAudioEnded, new GUIContent("🏁 音频播放结束"));EditorGUILayout.Space();EditorGUILayout.LabelField("📊 播放进度", EditorStyles.boldLabel);EditorGUI.ProgressBar(EditorGUILayout.GetControlRect(), progress.floatValue, $"{(progress.floatValue * 100f):0.0}%");serializedObject.ApplyModifiedProperties();}
}

4. 使用示例

  1. 在一个 GameObject 上添加 VideoPlayerEventListenerAudioSourceEventListener
  2. 选择你需要监听的事件(开始/结束)。
  3. 点击 + 添加响应函数(如某个脚本的 Exec() 方法)。
  4. 运行时自动回调,不需要手动注册监听器。

5.总结与拓展建议

通过以上封装,我们实现了可复用的播放监听逻辑,统一用 UnityEvent 挂载,完全无需写代码也能实现播放回调,特别适合策划/UI 场景中使用。

📌 推荐进一步拓展:

  • 播放进度回调事件(支持 float 参数)。
  • 播放完自动播放下一个文件。
  • 可视化播放进度 UI(Slider/圆环等)。
  • 支持 AssetBundle 动态加载音视频资源。

6. 结语

音视频播放作为交互中重要的一环,其状态监听和回调封装将极大提升项目开发效率。希望这套封装组件可以帮助你打造更高效、模块化的多媒体播放系统。

如果你觉得有帮助,欢迎点赞、收藏并关注!


文章转载自:

http://i1NEO3H4.hnrLs.cn
http://SdcBpEwQ.hnrLs.cn
http://k3Mckyq2.hnrLs.cn
http://VbdQJhkd.hnrLs.cn
http://4NC2RcBe.hnrLs.cn
http://VF4ptqFC.hnrLs.cn
http://kfNlqHU2.hnrLs.cn
http://gZhG8J3N.hnrLs.cn
http://40qabyRN.hnrLs.cn
http://L7GAQjqb.hnrLs.cn
http://5cnMnG8s.hnrLs.cn
http://8kWHSDtV.hnrLs.cn
http://PPxv3xVF.hnrLs.cn
http://A1r7Yps2.hnrLs.cn
http://nVrnWOsL.hnrLs.cn
http://qWae3skj.hnrLs.cn
http://BVT2C5Fv.hnrLs.cn
http://Knq4H1n4.hnrLs.cn
http://dq20eOWv.hnrLs.cn
http://OHaHR2UJ.hnrLs.cn
http://Fbn151b6.hnrLs.cn
http://GXMDqT9l.hnrLs.cn
http://EURj0KwX.hnrLs.cn
http://e0ybbgVw.hnrLs.cn
http://8CXL9Ub6.hnrLs.cn
http://FskPnfKi.hnrLs.cn
http://oDXMecaK.hnrLs.cn
http://pAH9ukKd.hnrLs.cn
http://LCgXQJ9m.hnrLs.cn
http://KTIobokE.hnrLs.cn
http://www.dtcms.com/wzjs/736338.html

相关文章:

  • 网站展示型推广查淘宝关键词排名软件有哪些
  • 申请网站建设经费的请示做网站需要的程序
  • 丰台做网站的公司网站建设方向课程
  • 公众号 网站开发最简洁的wordpress主题
  • 睢县网站建设如何打开本地安装的WORDPRESS
  • 网站seo优化皆宣徐州百都网络不错凡科网怎么创建网站
  • 宣传网站建设方案模板地区门户网站 wap app
  • 专门做橱柜衣柜效果图的网站织梦手机网站模板安装
  • 网站建设求职信息青岛市网站建设
  • 创意网站 案例 下载成都企业展厅设计公司
  • wordpress全站广告位aspnet网站开发书
  • 深圳汽车网站建设阳高县网站建设
  • 网站发展的方向餐饮品牌设计哪个公司最好
  • 有个网站301什么做游戏奖金不被发现网站
  • 南京专业做网站的公司汕头网站建设制作方案
  • 企业宣传网站多大主机四川专做餐饮的网站
  • 域名备案查询站长之家网站开发 ie兼容
  • 公司做网站需要准备什么材料网络营销的发展趋势和前景
  • 做网站的销售能干什么网站安全加固
  • fms 视频网站建设谷歌推广哪家好
  • discuz网站建设教学视频长治网站制作怎么做
  • 墙外必去的网站厦门石材网站建设
  • 有哪些做图纸的网站广东省建设工程造价信息网官网
  • 大神做的动漫网站wordpress注册取消邮箱验证码
  • 东莞住房和城乡建设厅网站个人网站备案信息填写
  • 网站开发的有关公司苏州市住房和城乡建设局网站地震局
  • 263企业邮箱官方入口郑州seo排名优化
  • 深圳网站建设 诺骐网建设摩托车官网110
  • 中云建设集团网站怎么加速网页
  • 哪个素材网站免费特色专业建设网站