using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using Newtonsoft.Json; using System; using System.IO; using UnityEngine.EventSystems; using Unity.VisualScripting; using DG.Tweening.Plugins.Core.PathCore; //using static Siccity.GLTFUtility.GLTFAccessor.Sparse; using static RobotManager; using UnityEngine.EventSystems; public class CustomPanel : MonoBehaviour { public static CustomPanel instance; public InputField timeinput; public List items; List datas = new List(); public Button backBtn; public Button playBtn; public Button saveBtn; public Button addMons; public Button refreshBtn; public Button upMoveBtn; public Button angularReadBackBtn; public Button deleteBtn; public Button insertBtn; public Button downMoveBtn; public Button handProBtn; public Button readDeviationBtn; public Button downloadBiasBtn; public Button steeringBackCenterBtn; public Button openActionFileBtn; public Button inseriesFileBtn; public Button singleErasureBtn; public Button allErasureBtn; public Button actionPlayBtn; public Button actionStopBtn; public Transform Content; public static GameObject _currItem; public static GameObject robot; public Toggle cirToggle; public Toggle msToggle; public bool isPlayAction; public Dropdown dropdown; public Scrollbar actionContentScrollbar; public Button ResetBtn; public Text allTime; // public static int[] actionArray = new int[16]; string path = Application.streamingAssetsPath + "/RobotFiles/Motions"; public static List actions; public Button StopRunBtn; public List all_motions; public List all_Objects; public static Sprite _currItemSprite; public static Sprite defSprite; Vector3 robotPos; Quaternion robotRota; private void Awake() { robot.GetComponent().Isflag = true; instance = this; var str = Resources.Load("stand").text; datas = JsonConvert.DeserializeObject>(str); _currItemSprite = Resources.Load("Prefabs/RobotUI/Select_Item"); defSprite = Resources.Load("Prefabs/RobotUI/defSprite"); } private void OnEnable() { isPlayAction = false; robot.SetActive(true); Stant(); } void Start() { robotPos = robot.transform.position; robotRota = robot.transform.rotation; all_motions = new List(); all_Objects = new List(); actions = new List(); backBtn.onClick.AddListener(ClosePanel); playBtn.onClick.AddListener(PlayAction); saveBtn.onClick.AddListener(SvaeAction); addMons.onClick.AddListener(AddMons); refreshBtn.onClick.AddListener(RefreshBtn); upMoveBtn.onClick.AddListener(UpMoveActon); deleteBtn.onClick.AddListener(DeleteAction); insertBtn.onClick.AddListener(InsertAction); downMoveBtn.onClick.AddListener(DownMoveAction); openActionFileBtn.onClick.AddListener(OpenActionFile); inseriesFileBtn.onClick.AddListener(InseriesFile); actionPlayBtn.onClick.AddListener(ActionPlay); singleErasureBtn.onClick.AddListener(SingleErasure); allErasureBtn.onClick.AddListener(AllErasure); actionStopBtn.onClick.AddListener(StopAction); StopRunBtn.onClick.AddListener(StopRun); ResetBtn.onClick.AddListener(ResetRobot); cirToggle.onValueChanged.AddListener(x => { if (x == false) { cirToggle.isOn = false; } }); OnRefreshOptions(); //计算环绕点 roudPoint = new Vector3(robot.transform.position.x, RobotManager.instance.robotTempCamera.transform.position.y, robot.transform.position.z); } void asd() { } void Stant() { for (int i = 0; i < items.Count; i++) { items[i].GetComponent().Init(datas[0], items.Count - i); } } //重置机器人状态 private void ResetRobot() { Stant(); robot.transform.position = robotPos; robot.transform.rotation = robotRota; } /// /// 刷新下拉框列表 /// private void OnRefreshOptions() { //dropdown.ClearOptions(); dropdown.options.Clear(); dropdown.ClearOptions(); dropdown.value = 0; var fileNames = GetFilesNmae(); if (fileNames.Count > 0) { dropdown.AddOptions(fileNames); CurAction(0); dropdown.onValueChanged.AddListener(CurAction); } } /// /// 删除全部 /// /// private void AllErasure() { if (Directory.Exists(path)) { DirectoryInfo direction = new DirectoryInfo(path); FileInfo[] files = direction.GetFiles("*.txt"); for (int i = 0; i < files.Length; i++) { File.Delete(path + "/" + files[i].Name); } OnRefreshOptions(); } } /// /// 动作停止 /// /// private void StopAction() { if (isPlayAction) { isPlayAction = false; robot.GetComponent().ClearQueue(); actionContentScrollbar.interactable = true; } } /// /// 单个擦除 /// /// private void SingleErasure() { if (dropdown.options.Count > 0) { string filepath = path + "/" + dropdown.options[dropdown.value].text; if (File.Exists(filepath)) { File.Delete(filepath); OnRefreshOptions(); Debug.Log("删除成功"); } } } List motions; private void CurAction(int arg0) { if (dropdown.options.Count > 0) { motions = new List(); string name = dropdown.options[arg0].text.ToString(); string actionPath = path + "/" + name; string str = File.ReadAllText(actionPath); List cus = JsonConvert.DeserializeObject>(str); for (int i = 0; i < cus.Count; i++) { RobotMotorValues values = new RobotMotorValues(); values.index = cus[i].index; values.time = cus[i].time; List list = new List(); for (int j = 0; j < cus[i].values.Count; j++) { list.Add(cus[i].values[j]); } values.values = list.ToArray(); motions.Add(new Motion(values)); } } } /// /// 动作运行 /// public void ActionPlay() { if (motions != null && motions.Count > 0) { StopRunBtn.gameObject.SetActive(false); playBtn.gameObject.SetActive(true); isPlayAction = true; robot.GetComponent().ClearQueue(); robot.GetComponent().UpdateMotions(motions); } } /// /// 运行 /// public void PlayAction() { actionContentScrollbar.interactable = false; AddMotion(); if (motion != null && motion.Count > 0) { isPlayAction = true; robot.GetComponent().ClearQueue(); float time = 0; for (int i = 0; i < motion.Count; i++) { time += motion[i].duration_time; } allTime.text = time.ToString(); robot.GetComponent().isPlaying = true; StopRunBtn.gameObject.SetActive(true); playBtn.gameObject.SetActive(false); robot.GetComponent().UpdateMotions(motion); robot.GetComponent().RefreshCur(all_Objects, actionContentScrollbar); actionContentScrollbar.interactable = true; } } /// /// 停止运行 /// private void StopRun() { playBtn.gameObject.SetActive(true); StopRunBtn.gameObject.SetActive(false); isPlayAction = false; robot.GetComponent().isPlaying = false; robot.GetComponent().ClearQueue(); for (int i = 0; i < items.Count; i++) { items[i].IsActivation(true); } } public List GetFilesNmae() { if (Directory.Exists(path)) { DirectoryInfo direction = new DirectoryInfo(path); FileInfo[] files = direction.GetFiles("*.txt"); List names = new List(); for (int i = 0; i < files.Length; i++) { if (!files[i].Name.EndsWith("meta")) { names.Add(files[i].Name); } } return names; } return null; } /// /// 串联动作文件 /// /// private void InseriesFile() { if (isPlayAction == false) { FolderBrowserHelper.SelectFile(InserActionFile, FolderBrowserHelper.TEXTGEFILTER); } else { MyPrint(); } } public void InserActionFile(string path) { string str2 = path.Replace(@"\", "/"); int index = str2.IndexOf("\0"); string str3 = str2.Substring(0, index); string str = File.ReadAllText(str3.ToString()); List cus = JsonConvert.DeserializeObject>(str); for (int i = 0; i < cus.Count; i++) { CustomItemData data = new CustomItemData(); data.index = actions.Count + 1; data.time = cus[i].time; data.values = new List(); for (int j = 0; j < cus[i].values.Count; j++) { data.values.Add(cus[i].values[cus[i].values.Count - 1 - j]); } actions.Add(data); } RefreshContent(); } /// /// 打开动作文件 /// private void OpenActionFile() { if (isPlayAction == false) { FolderBrowserHelper.SelectFile(GetFiledPath, FolderBrowserHelper.TEXTGEFILTER); } else { MyPrint(); } } public void GetFiledPath(string path) { actions.Clear(); string str2 = path.Replace(@"\", "/"); int index = str2.IndexOf("\0"); string str3 = str2.Substring(0, index); string str = File.ReadAllText(str3.ToString()); List cus = JsonConvert.DeserializeObject>(str); for (int i = 0; i < cus.Count; i++) { CustomItemData data = new CustomItemData(); data.index = actions.Count + 1; data.time = cus[i].time; data.values = new List(); for (int j = 0; j < cus[i].values.Count; j++) { data.values.Add(cus[i].values[cus[i].values.Count - 1 - j]); } actions.Add(data); } RefreshContent(); } /// /// 刷新动作 /// /// private void RefreshBtn() { if (isPlayAction == false) { if (_currItem != null) { var temp = _currItem.GetComponent(); for (int i = 0; i < actions.Count; i++) { if (actions[i].index == temp.index) { if (timeinput.text != "") { actions[i].time = int.Parse(timeinput.text); } else { actions[i].time = temp.data.time; } for (int j = 0; j < items.Count; j++) { actions[i].values[j] = int.Parse(items[j].value); } } } RefreshContent(); } else { Debug.Log("未选择要更新的动作"); } } else { MyPrint(); } } /// /// 添加动作 /// public void AddMons() { if (isPlayAction == false) { CustomItemData data = new CustomItemData(); data.values = new List(); data.index = actions.Count + 1; if (msToggle.isOn) { if (timeinput.text != "") { data.time = int.Parse(timeinput.text); } else { data.time = 200; } } else { if (timeinput.text != "") { data.time = int.Parse(timeinput.text) * 1000; } else { data.time = 200; } } for (int i = 0; i < items.Count; i++) { data.values.Add(int.Parse(items[i].value)); } actions.Add(data); Debug.Log("添加动作成功"); RefreshContent(); } else { MyPrint(); } } /// /// 上移动作 /// public void UpMoveActon() { if (isPlayAction == false) { if (_currItem != null) { var temp = _currItem.GetComponent(); for (int i = 0; i < actions.Count; i++) { if (actions[i].index == temp.index) { if (i > 0) { var actiondata = actions[i]; actions[i] = actions[i - 1]; actions[i].index = actions[i].index + 1; actions[i - 1] = actiondata; actions[i - 1].index = actiondata.index - 1; } } } RefreshContent(); Debug.Log("上移成功"); } else { Debug.Log("请选择要上移的动作"); } } else { MyPrint(); } } /// /// 提示信息 /// public void MyPrint() { Debug.Log("请先停止运行调试"); } /// /// 刷新列表 /// public void RefreshContent() { all_Objects.Clear(); for (int i = Content.childCount - 1; i >= 1; i--) { Destroy(Content.GetChild(i).gameObject); } for (int i = 0; i < actions.Count; i++) { GameObject item = Instantiate(Resources.Load("Prefabs/UIItem/MonsUIItem"), Content); item.GetComponent().Init(actions[i]); all_Objects.Add(item); } } /// /// 删除动作 /// public void DeleteAction() { if (isPlayAction == false) { if (_currItem != null) { var temp = _currItem.GetComponent(); for (int i = actions.Count - 1; i >= 0; i--) { if (actions[i].index == temp.index) { actions.Remove(actions[i]); } } for (int i = 0; i < actions.Count; i++) { actions[i].index = i + 1; } Debug.Log("动作删除成功"); RefreshContent(); } else { Debug.Log("请选择要删除的动作"); } } else { MyPrint(); } } /// /// 关闭面板 /// public void ClosePanel() { RobotUI.Instance.ClosePanel(RobotePanelEnum.CustomPanel.ToString()); for (int i = 0; i < robotgames.Count; i++) { robotgames[i].SetActive(true); } } /// /// 将动作文件添加到数组里 /// List motion; public void AddMotion() { if (actions.Count > 0) { motion = new List(); for (int i = 0; i < actions.Count; i++) { RobotMotorValues values = new RobotMotorValues(); values.index = actions[i].index; values.time = actions[i].time; List list = new List(); for (int j = actions[i].values.Count - 1; j >= 0; j--) { list.Add(actions[i].values[j]); } values.values = list.ToArray(); motion.Add(new Motion(values)); } } } /// /// 插入动作 /// public void InsertAction() { if (isPlayAction == false) { if (_currItem != null) { var temp = _currItem.GetComponent(); for (int i = 0; i < actions.Count; i++) { if (actions[i].index == temp.index) { CustomItemData data = new CustomItemData(); data.values = new List(); data.index = i; if (timeinput.text != "") { data.time = int.Parse(timeinput.text); } else { data.time = 200; } for (int j = 0; j < items.Count; j++) { data.values.Add(int.Parse(items[j].value)); } actions.Insert(temp.index, data); } } for (int i = 0; i < actions.Count; i++) { actions[i].index = i + 1; } Debug.Log("插入成功"); RefreshContent(); } else { Debug.Log("请选择插入的行"); } } else { MyPrint(); } } /// /// 下移动作 /// public void DownMoveAction() { if (isPlayAction == false) { if (_currItem != null) { var temp = _currItem.GetComponent(); for (int i = 0; i < actions.Count; i++) { if (actions[i].index == temp.index) { if (i < actions.Count - 1) { var actiondata = actions[i]; actions[i] = actions[i + 1]; actions[i].index = actions[i + 1].index - 1; actions[i + 1] = actiondata; actions[i + 1].index = actiondata.index + 1; } } } RefreshContent(); Debug.Log("下移成功"); } else { Debug.Log("请选择要上移的动作"); } } else { MyPrint(); } } /// /// 保存文件 /// public void SvaeAction() { FolderBrowserHelper.SaveFile(path, SaveFiledPath, FolderBrowserHelper.TEXTGEFILTER); } public void SaveFiledPath(string path) { if (actions != null) { string path2 = path.Replace(@"\", "/"); int index = path2.IndexOf("\0"); string path3 = path2.Substring(0, index); List items = new List(); for (int i = 0; i < actions.Count; i++) { CustomItemData customItemData = new CustomItemData(); customItemData.index = actions[i].index; customItemData.time = actions[i].time; customItemData.values = new List(); for (int j = actions[i].values.Count - 1; j >= 0; j--) { customItemData.values.Add(actions[i].values[j]); } items.Add(customItemData); } string actionsData = JsonConvert.SerializeObject(items); File.WriteAllText(path3, actionsData); OnRefreshOptions(); } } /// /// 旋转值 /// private float RotateValue=0; /// /// 环绕点 /// private Vector3 roudPoint; void Update() { if (cirToggle.isOn == true && isPlayAction && robot.GetComponent().isPlaying == false) { robot.GetComponent().UpdateMotions(motion); robot.GetComponent().RefreshCur(all_Objects, actionContentScrollbar); } if (robot.GetComponent().isPlaying == false && cirToggle.isOn == false) { StopRunBtn.gameObject.SetActive(false); playBtn.gameObject.SetActive(true); isPlayAction = false; actionContentScrollbar.interactable = true; } RobotManager.instance.robotTempCamera.transform.RotateAround(roudPoint, Vector3.up, Time.deltaTime * RotateValue); } /// /// 刷新当前选中的Item /// /// public static void FreshCurrItem(GameObject game) { if (_currItem != null) { //之前选中状态取消 _currItem.GetComponent().playBtn.gameObject.SetActive(false); _currItem.GetComponent().sprite = defSprite; } //设置当前选中状态 _currItem = game; _currItem.GetComponent().sprite = _currItemSprite; _currItem.GetComponent().playBtn.gameObject.SetActive(true); } private void OnDisable() { robot.SetActive(false); RobotManager.instance.robotTempCamera.SetActive(false); playBtn.onClick.RemoveAllListeners(); saveBtn.onClick.RemoveAllListeners(); addMons.onClick.RemoveAllListeners(); refreshBtn.onClick.RemoveAllListeners(); upMoveBtn.onClick.RemoveAllListeners(); deleteBtn.onClick.RemoveAllListeners(); downMoveBtn.onClick.RemoveAllListeners(); openActionFileBtn.onClick.RemoveAllListeners(); inseriesFileBtn.onClick.RemoveAllListeners(); actionPlayBtn.onClick.RemoveAllListeners(); singleErasureBtn.onClick.RemoveAllListeners(); allErasureBtn.onClick.RemoveAllListeners(); actionStopBtn.onClick.RemoveAllListeners(); StopRunBtn.onClick.RemoveAllListeners(); dropdown.onValueChanged.RemoveAllListeners(); } /// /// 左转 /// public void RotateLeft() { RotateValue = 20; } /// /// 右转 /// public void RotateRight() { RotateValue = -20; } /// /// 停止旋转 /// public void RotateStop() { RotateValue = 0; } }