FM/Assets/ABigFile/XLZ/Scripts/Book_M.cs

203 lines
5.1 KiB
C#

using System;
using System.Collections;
using System.Collections.Generic;
using Runtime;
using UnityEngine;
using UnityEngine.Playables;
using UnityEngine.Serialization;
using ZGame;
public class Book_M : MonoBehaviour
{
[SerializeField] private PlayableDirector playable;
[SerializeField] List<float> times = new List<float>();
[SerializeField] int number = 0;
public List<Renderer> renders = new List<Renderer>();
public Renderer[] insideRenders;
public Renderer box5;
private void Awake()
{
EventManager.Instance.Subscribe(ProductIsShowDesignCoverEventArgs.EventId, ProductIsShowDesignCoverEvent);
}
private void OnDestroy()
{
EventManager.Instance.Unsubscribe(ProductIsShowDesignCoverEventArgs.EventId, ProductIsShowDesignCoverEvent);
}
private void ProductIsShowDesignCoverEvent(object sender, GameEventArgs e)
{
var args = e as ProductIsShowDesignCoverEventArgs;
box5.enabled = !args.isShow;
}
// Update is number - 1 once per frame
void Update()
{
#if UNITY_EDITOR
if (Input.GetKeyDown(KeyCode.A))
{
// Next();
}
if (Input.GetKeyDown(KeyCode.D))
{
// Last();
}
#endif
}
public void SetIndex(int ind)
{
StopAllCoroutines();
if (number == 4 && (ind == 2 || ind == 1))
{
this.number = ind;
StartCoroutine(PlayNext(1));
return;
}
if (this.number < ind) // 1 3 4
{
this.number = ind;
if (ind == 4)
StartCoroutine(PlayCloseBook());
else if (ind == 3)
StartCoroutine(PlayNext(2));
else if (ind == 1)
StartCoroutine(PlayNext(1));
}
else if (this.number > ind) // 0 2 3
{
this.number = ind;
if (ind == 3)
StartCoroutine(PlayOpenBook());
else if (ind == 2 || ind == 1)
StartCoroutine(PlayLast(1));
else if (ind == 0)
StartCoroutine(PlayLast(0));
}
}
// public void Next()
// {
// StopAllCoroutines();
// StartCoroutine(PlayNext());
// }
//
// public void Last()
// {
// StopAllCoroutines();
// StartCoroutine(PlayLast());
// }
//
// public void CloseBook()
// {
// StopAllCoroutines();
// StartCoroutine(PlayCloseBook());
// }
public void ChangeTexture(Texture tex)
{
foreach (var rend in renders)
{
rend.material.mainTexture = tex;
}
//mainModel.material.mainTexture = tex;
}
public void ChangeLineTexture(Texture tex)
{
foreach (var rend in insideRenders)
{
rend.material.mainTexture = tex;
}
//mainModel.material.mainTexture = tex;
}
void PlayStart()
{
// Debug.Log($"start {number}");
if (number == 3)
EventManager.Instance.FireNow(this, new ProductIsShowDesignCoverEventArgs(false));
}
void PlayFinish()
{
// Debug.Log($"finish {number}");
if (number == 2 || number == 1)
EventManager.Instance.FireNow(this, new ProductIsShowDesignCoverEventArgs(true));
EventManager.Instance.FireNow(this, new BookPlayAnimatorFinishEventArgs(number));
}
float currentTime = 0;
IEnumerator PlayNext(int index)
{
// if(index==2)
// box5.enabled=false;
PlayStart();
playable.timeUpdateMode = DirectorUpdateMode.Manual;
while (currentTime < times[index])
{
playable.time += Time.deltaTime;
playable.DeferredEvaluate();
currentTime = (float)playable.time;
yield return null;
}
PlayFinish();
}
IEnumerator PlayLast(int index)
{
PlayStart();
playable.timeUpdateMode = DirectorUpdateMode.Manual;
while (currentTime > times[index])
{
playable.time -= Time.deltaTime;
playable.DeferredEvaluate();
currentTime = (float)playable.time;
yield return null;
}
// EventManager.Instance.FireNow(this,new OpenDecalUIArgs());
PlayFinish();
}
IEnumerator PlayCloseBook()
{
PlayStart();
playable.timeUpdateMode = DirectorUpdateMode.Manual;
while (currentTime > times[0])
{
playable.time -= Time.deltaTime;
playable.DeferredEvaluate();
currentTime = (float)playable.time;
yield return null;
}
// box5.enabled=true;
PlayFinish();
}
IEnumerator PlayOpenBook()
{
// box5.enabled=false;
PlayStart();
playable.timeUpdateMode = DirectorUpdateMode.Manual;
while (currentTime < times[2])
{
playable.time += Time.deltaTime;
playable.DeferredEvaluate();
currentTime = (float)playable.time;
yield return null;
}
PlayFinish();
}
}