174 lines
4.1 KiB
C#
174 lines
4.1 KiB
C#
using UnityEngine;
|
||
using DG.Tweening;
|
||
using System.IO;
|
||
using NPOI.SS.UserModel;
|
||
using NPOI.XSSF.UserModel;
|
||
using UnityEngine.Events;
|
||
using Org.BouncyCastle.Ocsp;
|
||
using System.Collections.Generic;
|
||
using System.Collections;
|
||
|
||
public class RotationController : MonoBehaviour
|
||
{
|
||
[Header("控制对象")]
|
||
public Transform upDownObject; // 上下转动的物体(转 Y)
|
||
public Transform leftRightObject; // 左右转动的物体(转 X)
|
||
|
||
public GameObject animationObj;
|
||
public Transform updownFallow;
|
||
public Transform leftRightFallow;
|
||
|
||
[Header("Excel 设置")]
|
||
|
||
public string exlPath;
|
||
public string sheetName = "Data";
|
||
|
||
[Header("动画设置")]
|
||
public float upDownDuration = 0.8f;
|
||
public float leftRightDuration = 0.8f;
|
||
|
||
float upDownValue;
|
||
float leftRightValue;
|
||
// 初始旋转
|
||
private Vector3 upDownStartRot;
|
||
Sequence seq;
|
||
private Vector3 leftRightStartRot;
|
||
|
||
public UnityEvent OnEnableEv;
|
||
void Awake()
|
||
{
|
||
// 保存初始旋转
|
||
upDownStartRot = upDownObject.localEulerAngles;
|
||
leftRightStartRot = leftRightObject.localEulerAngles;
|
||
|
||
LoadExcel();
|
||
}
|
||
|
||
void OnEnable()
|
||
{
|
||
OnEnableEv?.Invoke();
|
||
StartCoroutine(ReplayAnimation());
|
||
|
||
|
||
// 每次激活都强制重置
|
||
ResetState();
|
||
|
||
Debug.Log("重置");
|
||
// 重新播放动画
|
||
PlayAnimation();
|
||
}
|
||
|
||
IEnumerator ReplayAnimation()
|
||
{
|
||
|
||
if (GetComponent<Animation>() != null)
|
||
{
|
||
|
||
yield return new WaitForSeconds(.1f);
|
||
GetComponent<Animation>().Stop();
|
||
|
||
}
|
||
|
||
}
|
||
|
||
|
||
|
||
void OnDisable()
|
||
{
|
||
// 防止 SetActive(false) 时未完成的动画继续执行
|
||
seq?.Kill();
|
||
}
|
||
|
||
|
||
void ResetState()
|
||
{
|
||
// 清除旧动画
|
||
seq?.Kill();
|
||
|
||
// 恢复初始角度
|
||
upDownObject.localEulerAngles = upDownStartRot;
|
||
leftRightObject.localEulerAngles = leftRightStartRot;
|
||
}
|
||
|
||
void LoadExcel()
|
||
{
|
||
//string path = Application.streamingAssetsPath+"/"+ exlPath
|
||
string path = Path.Combine(Application.streamingAssetsPath, exlPath);
|
||
|
||
if (!File.Exists(path))
|
||
{
|
||
Debug.LogError("Excel 文件不存在:" + path);
|
||
return;
|
||
}
|
||
|
||
using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read))
|
||
{
|
||
IWorkbook workbook = new XSSFWorkbook(fs);
|
||
ISheet sheet = workbook.GetSheet(sheetName);
|
||
|
||
IRow row = sheet.GetRow(1);
|
||
|
||
upDownValue = (float)row.GetCell(0).NumericCellValue;
|
||
leftRightValue = (float)row.GetCell(1).NumericCellValue;
|
||
}
|
||
}
|
||
|
||
public bool is181=false;
|
||
public UnityEvent onAnimCompleteAction;
|
||
|
||
void PlayAnimation()
|
||
{
|
||
if (upDownObject == null || leftRightObject == null)
|
||
{
|
||
Debug.LogError("请在 Inspector 指定 upDownObject 和 leftRightObject!");
|
||
return;
|
||
}
|
||
|
||
// 目标旋转(相对加值)
|
||
Vector3 upDownTarget = upDownObject.localEulerAngles;
|
||
if (is181)
|
||
upDownTarget.x += upDownValue;
|
||
else
|
||
upDownTarget.y += upDownValue;
|
||
|
||
Vector3 leftRightTarget = leftRightObject.localEulerAngles;
|
||
leftRightTarget.z += leftRightValue;
|
||
|
||
// 创建顺序动画
|
||
seq = DOTween.Sequence();
|
||
|
||
seq.Append(upDownObject.DOLocalRotate(upDownTarget, upDownDuration))
|
||
.Append(leftRightObject.DOLocalRotate(leftRightTarget, leftRightDuration))
|
||
|
||
// -----------------------
|
||
// 动画播放完毕触发事件
|
||
// -----------------------
|
||
.OnComplete(() =>
|
||
{
|
||
Debug.Log("动画已全部完成");
|
||
//给动画物体同步角度用于播放动画
|
||
|
||
//animationObj.SetActive(true);
|
||
//gameObject.SetActive(false);
|
||
|
||
|
||
// 代码绑定事件
|
||
onAnimCompleteAction?.Invoke();
|
||
});
|
||
}
|
||
|
||
public void PauseTween() {
|
||
|
||
seq.Pause();
|
||
}
|
||
|
||
public void UpRotation()
|
||
{
|
||
updownFallow.transform.localRotation = upDownObject.localRotation;
|
||
leftRightFallow.transform.localRotation = leftRightObject.localRotation;
|
||
}
|
||
|
||
|
||
|
||
}
|