using System.Linq; using System.Collections; using System.Threading.Tasks; using System.Collections.Generic; using DG.Tweening; using UnityEngine; public class Train : MonoBehaviour { public TrainType type; public bool useEffect; private LocomotionHandler locomotionHandler; private Coroutine c_effect; private List materials = new(); private float effectDuration = .4f; void OnEnable() { var uiMenu = FindObjectOfType(); uiMenu.currentTrain = this; if(materials.Count == 0) { materials = GetComponent().materials.ToList(); var materialsInChildren = gameObject.transform .GetChild(0) .GetComponent() .materials.ToList(); foreach(var mat in materialsInChildren) materials.Add(mat); } if(!useEffect) { SetEffect(true); if(locomotionHandler == null) locomotionHandler = FindObjectOfType(); var startPosition = locomotionHandler.pullUpPositionBefore; var speed = locomotionHandler.trainSpeed; transform.localPosition = new Vector3(transform.localPosition.x, transform.localPosition.y, startPosition); transform.DOLocalMoveZ(-16, speed) .SetEase(Ease.OutSine); } else SetEffect(true); } public async void Hide() { if(!useEffect) { var endPosition = locomotionHandler.pullUpPositionAfter; var speed = locomotionHandler.trainSpeed; transform.DOLocalMoveZ(endPosition, speed) .SetEase(Ease.InSine) .OnComplete(() => gameObject.SetActive(false)); await Task.Delay(3000); SetEffect(false); } else SetEffect(false); } void SetEffect(bool isShow) { if(c_effect != null) { StopCoroutine(c_effect); c_effect = null; } c_effect = StartCoroutine(SetEffectRoutine(isShow)); } IEnumerator SetEffectRoutine(bool isShow) { float startPosition = isShow ? -5 : 3; float currentValue = startPosition; float endPosition = 0; if(isShow) endPosition = 3; else endPosition = -5; float progress = 0; float duration = effectDuration; while(progress < 1) { currentValue = Mathf.Lerp(startPosition, endPosition, progress); for(int i = 0; i < materials.Count; i++) { materials[i] .SetFloat("_EffectPosition", currentValue); } progress += duration * Time.deltaTime; yield return null; } for(int i = 0; i < materials.Count; i++) { materials[i] .SetFloat("_EffectPosition", endPosition); } if(!isShow && useEffect) gameObject.SetActive(false); } }