Files
JJBB/Assets/Project/Script/SceneMovie/MovieEventRecv.cs

193 lines
6.3 KiB
C#
Raw Normal View History

2024-08-23 15:49:34 +08:00
using UnityEngine;
using UnityEngine.Playables;
using Games.Events;
using System.Collections;
using System.Collections.Generic;
public class MovieEventRecv : MonoBehaviour
{
[System.Serializable]
public class ParamGameObjInfo
{
public MovieEventSend.EventInfo _Event;
public GameObject newGameObj;
public Vector3 Pos = Vector3.zero;
public Vector3 localEulerAngles = Vector3.zero;
public Vector3 Scale = Vector3.one;
}
public bool HideOnAwake = false;
public ParamGameObjInfo[] ParamGameObjs;
private void Awake()
{
Games.Events.EventDispatcher.Instance.Add(Games.Events.EventId.MovieEvent, OnRecive);
gameObject.SetActive(!HideOnAwake);
}
void OnDestroy()
{
Games.Events.EventDispatcher.Instance.Remove(Games.Events.EventId.MovieEvent, OnRecive);
}
private void OnRecive(object param)
{
EventSenderInfo SenderInfo = param as EventSenderInfo;
if (SenderInfo == null || SenderInfo._SendID == -1)
return;
for(int i=0;i<SenderInfo.Events.Count;i++)
{
for(int j = 0;j< ParamGameObjs.Length; j++)
{
if(ParamGameObjs[j]._Event.EveneID == SenderInfo.Events[i].EveneID && ParamGameObjs[j]._Event.EventType == SenderInfo.Events[i].EventType)
{
OnRecive1(SenderInfo._SendID, SenderInfo._State, SenderInfo.Events[i].EventType, ParamGameObjs[j]);
}
}
}
}
private void OnRecive1(int SenderID, bool state, MovieEventSend.EventType _Type, ParamGameObjInfo ParamGameObj)
{
switch (_Type)
{
case MovieEventSend.EventType.ChangeMesh:
{
ChangeMesh(state, SenderID, ParamGameObj);
}
break;
case MovieEventSend.EventType.AddMesh:
{
AddMesh(state, SenderID,ParamGameObj);
}
break;
case MovieEventSend.EventType.StartTimeLine:
{
StartTimeLine(state, SenderID,ParamGameObj);
}
break;
case MovieEventSend.EventType.Dissolve:
{
Dissolve(state, SenderID, ParamGameObj);
}
break;
case MovieEventSend.EventType.ShowGameObj:
{
ShowGameObj(state, SenderID, ParamGameObj);
}
break;
case MovieEventSend.EventType.HideGameObj:
{
ShowGameObj(!state, SenderID, ParamGameObj);
}
break;
case MovieEventSend.EventType.DestroyObj:
{
DestroyGameObj();
}
break;
}
}
Dictionary<int, GameObject> AddMeshObjs = new Dictionary<int, GameObject>();
void AddMesh(bool state,int SenderID, ParamGameObjInfo ParamGameObj)
{
if (state == false && AddMeshObjs.ContainsKey(SenderID))
{
GameObject DelObj = AddMeshObjs[SenderID];
if (DelObj != null)
{
DelObj.SetActive(false);
GameObject.Destroy(DelObj);
}
AddMeshObjs.Remove(SenderID);
return;
}
if (ParamGameObj == null || state == false)
return;
if (AddMeshObjs.ContainsKey(SenderID))
{
AddMeshObjs[SenderID].SetActive(true);
return;
}
GameObject AddMeshObj = GameObject.Instantiate(ParamGameObj.newGameObj) as GameObject;
if (AddMeshObj == null)
return;
AddMeshObj.transform.localPosition = ParamGameObj.Pos;
AddMeshObj.transform.localEulerAngles = ParamGameObj.localEulerAngles;
AddMeshObj.transform.localScale = (ParamGameObj.Scale == Vector3.zero ? Vector3.one : ParamGameObj.Scale);
AddMeshObjs.Add(SenderID, AddMeshObj);
}
Dictionary<int, GameObject> NewMeshObjs = new Dictionary<int, GameObject>();
void ChangeMesh(bool state,int SenderID, ParamGameObjInfo ParamGameObj)
{
if(state==false && NewMeshObjs.ContainsKey(SenderID))
{
GameObject DelObj = NewMeshObjs[SenderID];
if (DelObj != null)
{
DelObj.SetActive(false);
GameObject.Destroy(DelObj);
}
NewMeshObjs.Remove(SenderID);
gameObject.SetActive(true);
return;
}
if (ParamGameObj == null || state==false)
return;
if (NewMeshObjs.ContainsKey(SenderID))
return;
GameObject NewMeshObj = GameObject.Instantiate(ParamGameObj.newGameObj) as GameObject;
if (NewMeshObj == null)
return;
NewMeshObj.transform.SetParent(transform.parent);
NewMeshObj.transform.localPosition = transform.localPosition;
NewMeshObj.transform.localEulerAngles = transform.localEulerAngles;
NewMeshObj.transform.localScale = transform.localScale;
gameObject.SetActive(false);
NewMeshObjs.Add(SenderID, NewMeshObj);
}
void StartTimeLine(bool state, int SenderID, ParamGameObjInfo ParamGameObj)
{
if (ParamGameObj == null || ParamGameObj.newGameObj == null)
return;
PlayableDirector play = ParamGameObj.newGameObj.GetComponent<PlayableDirector>();
if (play == null)
return;
if (state)
play.Play();
else
play.Stop();
}
void Dissolve(bool state, int SenderID, ParamGameObjInfo ParamGameObj)
{
if (ParamGameObj == null || ParamGameObj.newGameObj == null || state == false)
return;
Dissolve dissolve = ParamGameObj.newGameObj.EnsureComponent<Dissolve>();
if (dissolve != null)
dissolve.StartDissolove();
}
void DestroyGameObj()
{
gameObject.SetActive(false);
GameObject.Destroy(gameObject);
}
void ShowGameObj(bool state, int SenderID, ParamGameObjInfo ParamGameObj)
{
if (ParamGameObj == null || ParamGameObj.newGameObj == null)
{
gameObject.SetActive(state);
return;
}
ParamGameObj.newGameObj.SetActive(state);
}
}