using System.Collections; using System.Collections.Generic; using UnityEngine; using Sirenix.OdinInspector; using System; using UnityEngine.UI; using System.IO; using System.Linq; using QFramework; using System.Reflection; public class MoveItemManager : MonoBehaviour { public static MoveItemManager instance; [TabGroup("配置相关")] [LabelText("设备名称")] [ValidateInput("IsValid")] public string deviceName; private bool IsValid(string value) { return !string.IsNullOrEmpty(value); } [TabGroup("配置相关")] [TableList] public List moveItemInfos; /// /// 使用音频控制结束 /// bool useAudioCtrl; public ModelItem m_CurrentSelectModelItem; private void Awake() { instance = this; UIKit.OpenPanel(); } public void Update() { Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); RaycastHit hit; if (Physics.Raycast(ray, out hit)) { ModelItem modelItem = GetParentPartMarkWithModelItem(hit.collider.transform); if (m_CurrentSelectModelItem != modelItem) { if (m_CurrentSelectModelItem != null) { m_CurrentSelectModelItem.CloseHighlighter(); } m_CurrentSelectModelItem = modelItem; m_CurrentSelectModelItem.OpenHighlighter(); } // 处理射线击中的对象 if (modelItem != null) { UIKit.GetPanel().SetSubTitleText(modelItem.name); if(Input.GetMouseButtonDown(0)&& Time.timeScale == 0) { CameraSurround.instance.SetCameraPosition(modelItem.transform, false, false); } } } else { if (m_CurrentSelectModelItem != null) { m_CurrentSelectModelItem.CloseHighlighter(); m_CurrentSelectModelItem = null; UIKit.GetPanel().SetSubTitleText(""); } } } /// /// 查找携带PartMark的节点 /// /// /// private ModelItem GetParentPartMarkWithModelItem(Transform traget) { ModelItem tmpMark = traget.GetComponent(); if (tmpMark == null && traget.parent != null) { tmpMark = GetParentPartMarkWithModelItem(traget.parent); } return tmpMark; } /// /// 正向播放 /// public void PlayPlayMoveItem() { StopAllCoroutines(); SetInitState(); PlayMoveItemByIndex(); } /// /// 反向播放 /// public void PlayMoveItemByReverse() { StopAllCoroutines(); SetFinalState(); PlayMoveItemByReverseIndex(moveItemInfos.Count - 1); } /// /// 正向播放 /// /// private void PlayMoveItemByIndex(int index = 0) { StopAllCoroutines(); Debug.Log("播放" + moveItemInfos[index].moveItem.name); UIKit.GetPanel().SetSubTitleText(moveItemInfos[index].moveItem.name); StartCoroutine(AudioHelper.LoadSpecifiedAudioFile("Config/Audios/内部结构拆解/" + deviceName, moveItemInfos[index].moveItem.name, len => { if (len > moveItemInfos[index].moveItem.m_Duration) { Debug.Log("音频长"); useAudioCtrl = true; } else { Debug.Log("动画长"); useAudioCtrl = false; } }, () => { if (useAudioCtrl) { Debug.Log("音频回调"); OnPlayMoveItemByIndexCallBack(index); } })); StartCoroutine(MovePathByItem(moveItemInfos[index], false, () => { if (!useAudioCtrl) { Debug.Log("动画回调"); OnPlayMoveItemByIndexCallBack(index); } })); } /// /// 正向播放完成一条回调 /// /// private void OnPlayMoveItemByIndexCallBack(int index) { moveItemInfos[index].moveItem.gameObject.SetActive(false); moveItemInfos[index].modelItem.gameObject.SetActive(true); moveItemInfos[index].finish = true; if (index + 1 > moveItemInfos.Count - 1) { Debug.Log("正播播放序列完成"); //UIKit.ClosePanel(); //if (m_Reverse) //{ // PlayMoveItemByReverseIndex(moveItemInfos.Count - 1); //} } else { PlayMoveItemByIndex(index + 1); } } /// /// 反向播放 /// /// private void PlayMoveItemByReverseIndex(int index) { StopAllCoroutines(); Debug.Log("反向播放" + moveItemInfos[index].moveItem.name); UIKit.GetPanel().SetSubTitleText(moveItemInfos[index].moveItem.name); StartCoroutine(AudioHelper.LoadSpecifiedAudioFile("Config/Audios/内部结构拆解/" + deviceName, moveItemInfos[index].moveItem.name, len => { if (len > moveItemInfos[index].moveItem.m_Duration) { Debug.Log("音频长"); useAudioCtrl = true; } else { Debug.Log("动画长"); useAudioCtrl = false; } }, () => { if (useAudioCtrl) { Debug.Log("音频回调"); OnPlayMoveItemByReverseIndexCallBack(index); } })); StartCoroutine(MovePathByItem(moveItemInfos[index], true, () => { if (!useAudioCtrl) { Debug.Log("动画回调"); OnPlayMoveItemByReverseIndexCallBack(index); } })); } /// /// 反向播放完成一条回调 /// /// private void OnPlayMoveItemByReverseIndexCallBack(int index) { moveItemInfos[index].finish = false; if (index - 1 < 0) { Debug.Log("反向播放序列完成"); //UIKit.ClosePanel(); } else { PlayMoveItemByReverseIndex(index - 1); } } IEnumerator MovePathByItem(MoveItemInfo moveItemInfo, bool isReverse, Action finishedCallBack = null) { float timer = 0; float lerp = 0; while (timer < moveItemInfo.moveItem.m_Duration) { MoveItem moveItem = moveItemInfo.moveItem; moveItem.gameObject.SetActive(true); moveItem.OpenHighter(true); lerp = isReverse ? (1 - timer / moveItem.m_Duration) : timer / moveItem.m_Duration; if (moveItem.m_UserGlobalDirection) { moveItemInfo.moveItem.transform.position = Vector3.Lerp(moveItemInfo.initPosition, moveItemInfo.initPosition + moveItem.m_Direction, lerp); } else { moveItemInfo.moveItem.transform.position = Vector3.Lerp(moveItemInfo.initPosition, moveItemInfo.initPosition + moveItem.transform.TransformDirection(moveItem.m_Direction), lerp); } timer += Time.deltaTime; yield return new WaitForEndOfFrame(); } moveItemInfo.moveItem?.OpenHighter(false); finishedCallBack.Invoke(); } /// /// 获取所有的组件 /// [TabGroup("配置相关")] [Button("获取所有的组件")] [GUIColor("GetColor")] private void GetAllMoveItems() { moveItemInfos = new List(); MoveItem[] moveItems = transform.GetComponentsInChildren(); ModelItem[] modelItems = transform.GetComponentsInChildren(); int count = Mathf.Min(moveItems.Length, modelItems.Length); for (int i = 0; i < count; i++) { MoveItemInfo moveItemInfo = new MoveItemInfo(); moveItemInfo.moveItem = moveItems[i]; moveItemInfo.modelItem = modelItems[i]; moveItemInfo.initPosition = moveItems[i].transform.position; moveItemInfos.Add(moveItemInfo); } //foreach (var item in moveItems) //{ // MoveItemInfo moveItemInfo = new MoveItemInfo(); // moveItemInfo.moveItem = item; // moveItemInfo.initPosition = item.transform.position; // moveItemInfos.Add(moveItemInfo); //} } private Color GetColor() { if (moveItemInfos.Count == 0 || moveItemInfos == null) { return Color.red; } else { return Color.green; } } /// /// 导出部件数据 /// [TabGroup("工具相关")] [Button("1.导出所有部件名称到表格(先去根据表格配音)")] private void ExportPartData() { List partMarkNames = new List(); foreach (var item in moveItemInfos) { MoveData partMarkName = new MoveData(); partMarkName.moveName = item.moveItem.name; if (partMarkNames.Find(t => t.moveName == partMarkName.moveName) == null) partMarkNames.Add(partMarkName); } ExcelHelper.WriteInfoToExcel(Application.streamingAssetsPath + "/" + deviceName + GlobalConfig.excelSuffix, partMarkNames); } /// /// 重命名音频名称,只能重命名数字的文件 /// [TabGroup("工具相关")] [Button("2.重命名音频名称(初始配音名字为数字)")] private void ReNameAudioClipName() { //表格路径 string tmpExcelPath = PathHelper.CombineFilePath(PathHelper.m_ReadOnlyPath, deviceName) + GlobalConfig.excelSuffix; //音频路径完整路径信息 string audioPath = PathHelper.CombineFilePath(PathHelper.m_ReadOnlyPath, "Config/Audios/内部结构拆解/" + deviceName); List tmpMoveDatas = ExcelHelper.ReadInfoFromExcel(tmpExcelPath, 1); DirectoryInfo directoryInfo = new DirectoryInfo(audioPath); FileInfo[] files = directoryInfo.GetFiles("*.mp3"); FileInfo[] sortedFiles = files.OrderBy(f => int.Parse(Path.GetFileNameWithoutExtension(f.FullName))).ToArray(); if (tmpMoveDatas.Count != sortedFiles.Length) { Debug.LogError("重命名失败"); } else { for (int i = 0; i < tmpMoveDatas.Count; i++) { string newPath = Path.Combine(directoryInfo.FullName, tmpMoveDatas[i].moveName) + ".mp3"; sortedFiles[i].MoveTo(newPath); } } } /// /// 设置初始状态 /// ` private void SetInitState() { foreach (var info in moveItemInfos) { MoveItem moveItem = info.moveItem; ModelItem modelItem = info.modelItem; moveItem.SetInitState(info.initPosition); moveItem.OpenHighter(false); moveItem.gameObject.SetActive(true); modelItem.gameObject.SetActive(false); } } /// /// 设置最终状态 /// private void SetFinalState() { foreach (var info in moveItemInfos) { MoveItem moveItem = info.moveItem; ModelItem modelItem = info.modelItem; moveItem.SetFianlState(); moveItem.OpenHighter(false); moveItem.gameObject.SetActive(false); modelItem.gameObject.SetActive(true); } } public void SetModelActive(bool isActive) { foreach (var item in moveItemInfos) { item.modelItem.gameObject.SetActive(isActive); } } } [Serializable] public class MoveItemInfo { [GUIColor("FinishedStateColor")] public MoveItem moveItem; public ModelItem modelItem; [LabelText("父级对象")] /// /// 初始化位置 /// [HideInInspector] public Vector3 initPosition; [LabelText("是否完成")] public bool finish; private Color FinishedStateColor() { if (finish) { return new Color(0.7f, 1f, 0.7f); } return GUI.color; } } public class MoveData { public string moveName; }