Files
plugin-library/Assets/Main/HotfixUpdateScript/Runtime/HotUpdate/LasterDoMain/IClickSystem.cs
2025-09-02 14:15:18 +08:00

302 lines
9.2 KiB
C#

using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Cysharp.Threading.Tasks;
using DG.Tweening;
using Stary.Evo;
using Stary.Evo.AudioCore;
using Stary.Evo.RKTools;
using UnityEngine;
using YooAsset;
namespace Main
{
public interface IClickSystem : ISystem
{
void AddClickIntervalEvent(Action callback);
void StartTime();
void EndClick();
void ResumeTouchClick(Transform tweenTarget);
Task BindClickEvent(List<ClickElementChildren> clickElementChildrens, Action<GameObject> callback,
bool isOneByOne = false);
void ClickBreatheTween(List<ClickElementChildren> tweenList, Transform tweenTarget);
void ClickBreatheTween(List<ClickElementChildren> tweenList, Transform tweenTarget, bool isOneByOne);
int ElementChildrenClickCount();
bool IsElementChildrenClick();
}
public class ClickSystem : AbstractSystem, IClickSystem
{
// public Sequence loopTargetTween;
private AudioClip _clickSound;
/// <summary>
/// 点击计时器
/// </summary>
public float time;
private CancellationTokenSource tokenSource;
private List<ClickElementChildren> clickElementChildrens;
private Action ClickIntervalEvent;
protected override void OnInit()
{
}
public void StartTime()
{
time = 0;
Update();
}
public async Task BindClickEvent(List<ClickElementChildren> clickElementChildrens, Action<GameObject> callback,
bool isOneByOne = false)
{
StartTime();
this.clickElementChildrens = clickElementChildrens;
//绑定点击事件
for (int i = 0; i < clickElementChildrens.Count; i++)
{
clickElementChildrens[i].transform.gameObject.ObjectAddTouchEvent(callback);
if (isOneByOne && i > 0)
{
clickElementChildrens[i].transform.gameObject.ObjectPauseTouchEvent();
}
}
//播放音效
var package = YooAssets.GetPackage("Main");
var sfxhandle = package.LoadAssetAsync<AudioClip>("Audios_au_effect_click");
await sfxhandle.Task;
_clickSound = sfxhandle.GetAssetObject<AudioClip>();
}
public void ClickBreatheTween(List<ClickElementChildren> tweenList, Transform tweenTarget)
{
Debug.Log("UnityEvo: 点击呼吸");
AudioCoreManager.PlaySFX(new AudioData()
{
clip = _clickSound
});
time = 0;
foreach (var tween in tweenList)
{
tween.transform.gameObject.ObjectResumeTouchEvent();
// if (tween.transform == tweenTarget)
// {
// tween.isClick = true;
// }
if (tween.transform == tweenTarget)
{
tween.isClick = true;
tween.transform.GetComponent<Animator>().CrossFade("dianji_idle", 0.01f);
this.SendEvent<ModeType, ClickElementChildren>(ModeType.PLayAudioOrVideo, tween);
tweenTarget.gameObject.ObjectPauseTouchEvent();
continue;
}
tween.transform.GetComponent<Animator>().CrossFade("dianji_nood", 0.01f);
}
}
public void ClickBreatheTween(List<ClickElementChildren> tweenList, Transform tweenTarget,bool isOneByOne)
{
Debug.Log("UnityEvo: 点击呼吸");
AudioCoreManager.PlaySFX(new AudioData()
{
clip = _clickSound
});
time = 0;
for (int i = 0; i < tweenList.Count; i++)
{
var tween = tweenList[i];
// if (tween.transform == tweenTarget)
// {
// tween.isClick = true;
// }
if (tween.transform == tweenTarget)
{
tween.isClick = true;
tween.transform.gameObject.ObjectPauseTouchEvent();
tween.transform.GetComponent<Animator>().CrossFade("dianji_disappear", 0.01f);
this.SendEvent<ModeType, ClickElementChildren>(ModeType.PLayAudioOrVideo, tween);
if (isOneByOne&& i+1 < tweenList.Count)
{
tweenList[i+1].transform.gameObject.ObjectResumeTouchEvent();
}
}
}
}
public void AddClickIntervalEvent(Action callback)
{
this.ClickIntervalEvent = callback;
}
public int ElementChildrenClickCount()
{
int count = 0;
//是否全部已经点击
for (int i = 0; i < clickElementChildrens.Count; i++)
{
if (clickElementChildrens[i].isClick == true)
{
count++;
}
}
return count;
}
public bool IsElementChildrenClick()
{
//是否全部已经点击
for (int i = 0; i < clickElementChildrens.Count; i++)
{
if (clickElementChildrens[i].isClick == false)
{
return false;
}
}
return true;
}
public void ResumeTouchClick( Transform tweenTarget)
{
// if (loopTargetTween != null)
// {
// loopTargetTween.Kill();
// loopTargetTween = null;
// }
time = 0;
if (tokenSource != null)
{
tokenSource?.Cancel();
tokenSource?.Dispose();
tokenSource = null;
}
foreach (var children in clickElementChildrens)
{
if (children.transform != tweenTarget )
{
var animator = children.transform.GetComponent<Animator>();
// 获取当前动画状态
var currentState = animator.GetCurrentAnimatorStateInfo(0);
if (currentState.IsName("dianji_disappear"))
{
children.transform.gameObject.ObjectResumeTouchEvent();
animator.CrossFade("dianji_appear", 0.01f);
}
}
}
}
public void EndClick()
{
// if (loopTargetTween != null)
// {
// loopTargetTween.Kill();
// loopTargetTween = null;
// }
time = 0;
if (tokenSource != null)
{
tokenSource?.Cancel();
tokenSource?.Dispose();
tokenSource = null;
}
foreach (var children in clickElementChildrens)
{
children.transform.gameObject.ObjectRemoveTouchEvent();
children.transform.GetComponent<Animator>().CrossFade("dianji_disappear", 0.01f);
}
}
private async void Update()
{
//TODO暂时禁用
if (tokenSource != null)
{
tokenSource?.Cancel();
}
tokenSource = new CancellationTokenSource();
try
{
while (time <= 15 && !tokenSource.IsCancellationRequested)
{
time += Time.deltaTime;
if (time >= 15)
{
Debug.Log("UnityEvo:执行未点击间隔事件");
time = 0;
ClickIntervalEvent?.Invoke();
}
await UniTask.Yield(tokenSource.Token);
}
}
catch (OperationCanceledException e)
{
Debug.Log("UnityEvo: 取消任务:" + e);
}
}
public override void Dispose()
{
if (tokenSource != null)
{
tokenSource?.Cancel();
tokenSource?.Dispose();
tokenSource = null;
}
// if (loopTargetTween != null)
// {
// loopTargetTween.Kill();
// loopTargetTween = null;
// }
}
}
public class ClickElementChildren
{
public bool isClick = false;
public string[] auid;
public string vid;
public Transform transform;
public Transform targetTransform;
public ClickElementChildren(Transform child)
{
this.transform = child;
}
public ClickElementChildren(Transform child, string[] auid, string vid)
{
this.auid = auid;
this.vid = vid;
this.transform = child;
}
public ClickElementChildren(Transform child, Transform targetTransform, string[] auid, string vid)
{
this.auid = auid;
this.vid = vid;
this.transform = child;
this.targetTransform = targetTransform;
}
}
}