一个屏幕上多个视频切换播放

方法一:1.将EventTriggerListener脚本拖到场景中

using UnityEngine;
using System.Collections;
using UnityEngine.EventSystems;
public class EventTriggerListener : UnityEngine.EventSystems.EventTrigger{
public delegate void VoidDelegate (GameObject go);
public VoidDelegate onClick;
public VoidDelegate onDown;
public VoidDelegate onEnter;
public VoidDelegate onExit;
public VoidDelegate onUp;
public VoidDelegate onSelect;
public VoidDelegate onUpdateSelect;
 
static public EventTriggerListener Get (GameObject go)
{
EventTriggerListener listener = go.GetComponent<EventTriggerListener>();
if (listener == null) listener = go.AddComponent<EventTriggerListener>();
return listener;
}
public override void OnPointerClick(PointerEventData eventData)
{
if(onClick != null) onClick(gameObject);
}
public override void OnPointerDown (PointerEventData eventData){
if(onDown != null) onDown(gameObject);
}
public override void OnPointerEnter (PointerEventData eventData){
if(onEnter != null) onEnter(gameObject);
}
public override void OnPointerExit (PointerEventData eventData){
if(onExit != null) onExit(gameObject);
}
public override void OnPointerUp (PointerEventData eventData){
if(onUp != null) onUp(gameObject);
}
public override void OnSelect (BaseEventData eventData){
if(onSelect != null) onSelect(gameObject);
}
public override void OnUpdateSelected (BaseEventData eventData){
if(onUpdateSelect != null) onUpdateSelect(gameObject);
}
}

2.ButonChange和musiccontroller脚本拖到空物体上并为其添加Audio Source

(1)ButonChange

 public GameObject[] btn;
    bool isplay1 = true;
    bool isplay2 = true;
    void Start()
    {
        foreach (GameObject item in btn)
        {
            EventTriggerListener.Get(item).onEnter += OnMouseEnter;
            EventTriggerListener.Get(item).onExit += OnMouseExit;
            EventTriggerListener.Get(item).onClick += OnClick;
        }
    }
    void OnMouseEnter(GameObject go)  //鼠标经过
    {
        switch (go.name)
        {


        }
    }
    void OnMouseExit(GameObject go)
    {
        switch (go.name)
        {
  
        }
    }
    void OnClick(GameObject go)  //鼠标按下
    {
        switch (go.name)
        {
            case "Button1":
                if (isplay1) {
                    musiccontroller._instance.StopMusic();
                    musiccontroller._instance.PlayMusic(0);
                    isplay1 = false;
                    isplay2 = true;
                }             
                break;
            case "Button2":
                if (isplay2) {
                    musiccontroller._instance.StopMusic();
                    musiccontroller._instance.PlayMusic(1);
                    isplay2 = false;
                    isplay1 = true;
                }            
                break;
        }
    }   

(2)musiccontroller

 public static musiccontroller _instance;
    public AudioClip[] _clips;//音频
    public MovieTexture[] move;//影片纹理
    public GameObject ray;//播放视频的屏幕 
    void Awake()
    {
        _instance = this;
    }
    //播放视频
    public void PlayMusic(int i)
    {
        ray.GetComponent<RawImage>().texture = move[i];
        move[i].Play();
        this.GetComponent<AudioSource>().clip = _clips[i];
        this.GetComponent<AudioSource>().Play();
    }
    //停止播放
    public void StopMusic() {
       foreach(MovieTexture shipin  in move){
           shipin.Stop();
       }
       this.GetComponent<AudioSource>().Stop();
    }

方法二:建一个空物体加上前面的musiccontroller脚本和JianTing脚本,并为其添加Audio Source

JianTing

 public GameObject[] btnss;
 bool isplay1 = true;
 bool isplay2 = true;


void Start () {
        foreach (GameObject obj in btnss) {
            GameObject item = GameObject.Find(obj.name);
            item.GetComponent<Button>().onClick.AddListener(
                delegate()
                {
                    this.UguibtnClick(item);
                }
                );
        }
}
    void UguibtnClick(GameObject a) {
        
            switch (a.name)
        {
            case "Button1":
                if (isplay1) {
                    musiccontroller._instance.StopMusic();
                    musiccontroller._instance.PlayMusic(0);
                    isplay1 = false;
                    isplay2 = true;
                }             
                break;
            case "Button2":
                if (isplay2) {
                    musiccontroller._instance.StopMusic();
                    musiccontroller._instance.PlayMusic(1);
                    isplay2 = false;
                    isplay1 = true;
                }            
                break;
        }
       
    }



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值