纳金网

标题: 朋友请我帮忙写了一个Unity播放完视屏后执行回调的工具脚本 [打印本页]

作者: may    时间: 2019-10-29 01:33
标题: 朋友请我帮忙写了一个Unity播放完视屏后执行回调的工具脚本

朋友请我帮忙写一个Unity播放完视屏后执行回调的工具脚本,这个脚本可以像UnityUGUI一样在编辑器里注册事件,像Button一样拖拽注册时间。
因为里边使用了Unity的UnityEvent,话不多说,上代码!
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.SceneManagement;
  5. using UnityEngine.Video;
  6. using UnityEngine.Events;

  7. public class test : MonoBehaviour
  8. {

  9.     VideoPlayer video;
  10.     /// <summary>
  11.     /// 已经播放了
  12.     /// </summary>
  13.     private bool IsPlayed;
  14.     /// <summary>
  15.     /// 视频播放完毕后的回调事件
  16.     /// </summary>
  17.     [Header("在这里添加你的回调,像button一样")]
  18.     public UnityEvent PlayOverEvent;

  19.     private void Start()
  20.     {
  21.         video = GetComponent<VideoPlayer>();
  22.         PlayOverEvent = new UnityEvent();
  23.         PlayOverEvent.AddListener(() =>print("播放完毕回调执行!!!"));
  24.     }

  25.     private void Update()
  26.     {
  27.         if (video!=null)
  28.         {
  29.             if (video.isPlaying)
  30.             {
  31.                 IsPlayed = true;
  32.             }
  33.             if (IsPlayed)
  34.             {
  35.                 if (!video.isPlaying)
  36.                 {
  37.                     PlayOverEvent.Invoke();
  38.                     IsPlayed = false;
  39.                 }
  40.             }
  41.         }
  42.     }
  43. }
复制代码





欢迎光临 纳金网 (http://go.narkii.com/club/) Powered by Discuz! X2.5