123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674 |
- using SC.XR.Unity;
- using ShadowStudio.Tool;
- using ShadowStudio.UI;
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Dynamic;
- using System.Text.RegularExpressions;
- using UnityEngine;
- using UnityEngine.UI;
- using XRTool.UI;
- using XRTool.Util;
- using XRTool.WorldUI;
- namespace ShadowStudio.Model
- {
- /// <summary>
- /// 资源的编辑
- /// </summary>
- [RequireComponent(typeof(WorldDlg))]
- public class ArtMoreEditor : UnitySingleton<ArtMoreEditor>
- {
- private ArtContainer artContainer;
- private Button lastStepBtn;
- private Button nextStepBtn;
- private ToggleButton editToggleBtn;
- private Button modelInfoBtn;
- private Button copyBtn;
- //private Button deleteBtn;
- private WorldDlg dlg;
- private XRIcon lastIcon;
- private XRIcon nextIcon;
- public float checkTime = 1;
- private float lastTime;
- //private Transform modelInfoBtnBack;
- //private Transform SetDlg;
- private SCInputField posXInputField;
- private SCInputField posYInputField;
- private SCInputField posZInputField;
- private SCInputField rotXInputField;
- private SCInputField rotYInputField;
- private SCInputField rotZInputField;
- private SCInputField scaXInputField;
- private SCInputField scaYInputField;
- private SCInputField scaZInputField;
- private TextMesh distanceText;
- private Toggle lineToggle_Switch;
- private GameObject Distanceline;
- public string defaultShader = "Mixed Reality Toolkit/Standard";
- private TextMesh CentreTextMesh;
- private Material defMate;
- private void Start()
- {
- //if (!dlg)
- //{
- // dlg = GetComponent<WorldDlg>();
- //}
- //XRIcon3D[] bts = dlg.Root.GetComponentsInChildren<XRIcon3D>();
- //var combinedMesh = new Mesh();
- //for (int i = 0; i < bts.Length; i++)
- //{
- // var mesh = bts[i].Body;
- // //combinedMesh.CombineMeshes
- //}
- Init();
- }
- private void Init()
- {
- if (dlg == null)
- {
- dlg = GetComponent<WorldDlg>();
- lastStepBtn = dlg.GetBreadthChild<Button>("LastStepBtn");
- nextStepBtn = dlg.GetBreadthChild<Button>("NextStepBtn");
- editToggleBtn = dlg.GetBreadthChild<ToggleButton>("EditToggleBtn");
- editToggleBtn.OnValueChanged += OnClickEditValueChange;
- modelInfoBtn = dlg.GetBreadthChild<Button>("ModelInfoBtn");
- copyBtn = dlg.GetBreadthChild<Button>("CopyBtn");
- //deleteBtn = dlg.GetBreadthChild<Button>("DeleteBtn");
- lastIcon = dlg.GetBreadthChild<XRIcon>("LastIcon");
- nextIcon = dlg.GetBreadthChild<XRIcon>("NextIcon");
- lastStepBtn.onClick.AddListener(ClickOnLastStep);
- nextStepBtn.onClick.AddListener(ClickOnNextStep);
- copyBtn.onClick.AddListener(ClickOnCopy);
- //deleteBtn.onClick.AddListener(ClickOnDelete);
- //modelInfoBtnBack = dlg.GetBreadthChild<Transform>("ModelInfoBtnBack");
- //SetDlg = dlg.GetBreadthChild<Transform>("SetDlg");
- posXInputField = dlg.GetBreadthChild<SCInputField>("PosXInputField");
- posXInputField.onEndEdit.AddListener(InputPosXEnd);
- posYInputField = dlg.GetBreadthChild<SCInputField>("PosYInputField");
- posYInputField.onEndEdit.AddListener(InputPosYEnd);
- posZInputField = dlg.GetBreadthChild<SCInputField>("PosZInputField");
- posZInputField.onEndEdit.AddListener(InputPosZEnd);
- rotXInputField = dlg.GetBreadthChild<SCInputField>("RotXInputField");
- rotXInputField.onEndEdit.AddListener(InputRotXEnd);
- rotYInputField = dlg.GetBreadthChild<SCInputField>("RotYInputField");
- rotYInputField.onEndEdit.AddListener(InputRotYEnd);
- rotZInputField = dlg.GetBreadthChild<SCInputField>("RotZInputField");
- rotZInputField.onEndEdit.AddListener(InputRotZEnd);
- scaXInputField = dlg.GetBreadthChild<SCInputField>("ScaXInputField");
- scaXInputField.onEndEdit.AddListener(InputScaXEnd);
- scaYInputField = dlg.GetBreadthChild<SCInputField>("ScaYInputField");
- scaYInputField.onEndEdit.AddListener(InputScaYEnd);
- scaZInputField = dlg.GetBreadthChild<SCInputField>("ScaZInputField");
- scaZInputField.onEndEdit.AddListener(InputScaZEnd);
- distanceText = dlg.GetBreadthChild<TextMesh>("DistanceText");
- //modelInfoBtn.onClick.AddListener(ShowSetDlg);
- lineToggle_Switch = dlg.GetBreadthChild<Toggle>("LineXRToggle_Switch");
- lineToggle_Switch.onValueChanged.AddListener(ShowLine);
- RefreshUI();
- }
- }
- private void OnClickEditValueChange(bool ison)
- {
- Cleartime();
- if (artContainer != null)
- {
- if (ison)
- {
- artContainer.Transfer.IsJiXingScale = true;
- }
- else
- {
- artContainer.Transfer.IsJiXingScale = false;
- }
- if (ArtEditorContainer.Instance)
- {
- ArtEditorContainer.Instance.ReFresh();
- }
- }
- }
- private bool IsMatch(string input)
- {
- Regex reg = new Regex(@"^(\-|\+)?\d+(\.\d+)?$");
- bool isMatch = reg.IsMatch(input);
- return isMatch;
- }
- private void InputPosXEnd(string PosX)
- {
- if (artContainer != null)
- {
- bool ismatch = IsMatch(PosX);
- if (ismatch)
- {
- Vector3 pos = artContainer.Position;
- pos.x = float.Parse(PosX);
- artContainer.Position = pos;
- }
- else
- {
- posXInputField.text = artContainer.Position.x.ToString();
- }
- }
- }
- private void InputPosYEnd(string PosY)
- {
- if (artContainer != null)
- {
- bool ismatch = IsMatch(PosY);
- if (ismatch)
- {
- Vector3 pos = artContainer.Position;
- pos.y = float.Parse(PosY);
- artContainer.Position = pos;
- }
- else
- {
- posYInputField.text = artContainer.Position.y.ToString();
- }
- }
- }
- private void InputPosZEnd(string PosZ)
- {
- if (artContainer != null)
- {
- bool ismatch = IsMatch(PosZ);
- if (ismatch)
- {
- Vector3 pos = artContainer.Position;
- pos.z = float.Parse(PosZ);
- artContainer.Position = pos;
- }
- else
- {
- posZInputField.text = artContainer.Position.z.ToString();
- }
- }
- }
- private void InputRotXEnd(string RotX)
- {
- if (artContainer != null)
- {
- bool ismatch = IsMatch(RotX);
- if (ismatch)
- {
- Vector3 rot = artContainer.Angle;
- rot.x = float.Parse(RotX);
- artContainer.Angle = rot;
- }
- else
- {
- rotXInputField.text = artContainer.Angle.x.ToString();
- }
- }
- }
- private void InputRotYEnd(string RotY)
- {
- if (artContainer != null)
- {
- bool ismatch = IsMatch(RotY);
- if (ismatch)
- {
- Vector3 rot = artContainer.Angle;
- rot.y = float.Parse(RotY);
- artContainer.Angle = rot;
- }
- else
- {
- rotYInputField.text = artContainer.Angle.y.ToString();
- }
- }
- }
- private void InputRotZEnd(string RotZ)
- {
- if (artContainer != null)
- {
- bool ismatch = IsMatch(RotZ);
- if (ismatch)
- {
- Vector3 rot = artContainer.Angle;
- rot.z = float.Parse(RotZ);
- artContainer.Angle = rot;
- }
- else
- {
- rotZInputField.text = artContainer.Angle.z.ToString();
- }
- }
- }
- private void InputScaXEnd(string ScaX)
- {
- if (artContainer != null)
- {
- bool ismatch = IsMatch(ScaX);
- if (ismatch)
- {
- Vector3 sca = artContainer.Scale;
- sca.x = float.Parse(ScaX);
- artContainer.Scale = sca;
- }
- else
- {
- scaXInputField.text = artContainer.Scale.x.ToString();
- }
- }
- }
- private void InputScaYEnd(string ScaY)
- {
- if (artContainer != null)
- {
- bool ismatch = IsMatch(ScaY);
- if (ismatch)
- {
- Vector3 sca = artContainer.Scale;
- sca.y = float.Parse(ScaY);
- artContainer.Scale = sca;
- }
- else
- {
- scaYInputField.text = artContainer.Scale.y.ToString();
- }
- }
- }
- private void InputScaZEnd(string ScaZ)
- {
- if (artContainer != null)
- {
- bool ismatch = IsMatch(ScaZ);
- if (ismatch)
- {
- Vector3 sca = artContainer.Scale;
- sca.z = float.Parse(ScaZ);
- artContainer.Scale = sca;
- }
- else
- {
- scaZInputField.text = artContainer.Scale.z.ToString();
- }
- }
- }
- bool isshow;
- //private void ShowSetDlg()
- //{
- // Cleartime();
- // if (!isshow)
- // {
- // SetDlg.gameObject.SetActive(true);
- // modelInfoBtnBack.gameObject.SetActive(true);
- // isshow = true;
- // lineToggle_Switch.onValueChanged.RemoveListener(ShowLine);
- // lineToggle_Switch.onValueChanged.AddListener(ShowLine);
- // }
- // else
- // {
- // SetDlg.gameObject.SetActive(false);
- // modelInfoBtnBack.gameObject.SetActive(false);
- // lineToggle_Switch.onValueChanged.RemoveListener(ShowLine);
- // isshow = false;
- // }
- //}
- private void ShowLine(bool ison)
- {
- if (artContainer != null)
- {
- if (ison)
- {
- ShowLine();
- }
- else
- {
- if (Distanceline)
- {
- if (CentreTextMesh)
- {
- CentreTextMesh.gameObject.SetActive(false);
- }
- Destroy(Distanceline);
- }
- }
- }
- }
- private void ShowLine()
- {
- if (Distanceline)
- {
- Destroy(Distanceline);
- }
- Distanceline = new GameObject("Distanceline", typeof(LineRenderer));
- LineRenderer line = Distanceline.GetComponent<LineRenderer>();
- if (!line)
- {
- Distanceline.AddComponent<LineRenderer>();
- }
- if (!defMate)
- {
- defMate = new Material(Shader.Find(defaultShader));
- }
- if (line.material)
- {
- UnityUtil.CopyMate(line.material, defMate);
- }
- else
- {
- line.material = defMate;
- }
- if (artContainer != null)
- {
- Vector3 Startpos = GameSession.Instance.BoadyPosition;
- Vector3 Endpos = GameNode.Instance.transform.TransformPoint(artContainer.Position);
- line.SetPosition(0, Startpos);
- line.SetPosition(1, Endpos);
- line.startWidth = 0.002f;
- line.endWidth = 0.002f;
- UnityUtil.ChangeMateColor(line, Color.blue);
- if (!CentreTextMesh)
- {
- CentreTextMesh = InstanceTextmesh();
- }
- else
- {
- CentreTextMesh.gameObject.SetActive(true);
- }
- CentreTextMesh.transform.position = BetweenPos(Startpos, Endpos) + new Vector3(0f, 0.03f, 0f);
- CentreTextMesh.transform.LookAt(Endpos);
- CentreTextMesh.text = distanceText.text;
- }
- GameNode.Instance.SetParent(ObjNode.World, Distanceline.transform,
- Vector3.zero, Vector3.zero, Vector3.one, true);
- if (CentreTextMesh.transform.parent == null)
- {
- CentreTextMesh.transform.parent = Distanceline.transform.parent;
- }
- }
- /// <summary>
- /// 实例化TextMesh
- /// </summary>
- /// <returns></returns>
- private TextMesh InstanceTextmesh()
- {
- TextMesh centreTextMesh = null;
- var textmesh = Resources.Load("CentreTextMesh");
- if (textmesh)
- {
- GameObject obj = GameObject.Instantiate(textmesh) as GameObject;
- centreTextMesh = obj.transform.GetComponent<TextMesh>();
- }
- return centreTextMesh;
- }
- /// <summary>
- /// 返回两个点中心点的坐标
- /// </summary>
- /// <param name="pos1"></param>
- /// <param name="pos2"></param>
- /// <returns></returns>
- private Vector3 BetweenPos(Vector3 pos1, Vector3 pos2)
- {
- return (pos1 + pos2) / 2;
- }
- //private void ClickOnDelete()
- //{
- // Cleartime();
- // if (artContainer != null)
- // {
- // if (FileListDlg.ChoiceKuang)
- // {
- // FileListDlg.ChoiceKuang.transform.SetParent(FileListDlg.Instance.transform);
- // FileListDlg.ChoiceKuang.SetActive(false);
- // }
- // if (ItemFileList.Instance)
- // {
- // ItemFileList.Instance.DeleteFileIcon(this.artContainer);
- // }
- // }
- //}
- private void ClickOnCopy()
- {
- Cleartime();
- if (artContainer != null)
- {
- artContainer.CloneContainer();
- }
- }
- private void ClickOnNextStep()
- {
- Cleartime();
- if (artContainer != null)
- {
- if (artContainer.AheadCount() > 0)
- {
- artContainer.GoAhead();
- }
- RefreshUI();
- }
- }
- private void ClickOnLastStep()
- {
- Cleartime();
- if (artContainer != null)
- {
- if (artContainer.BackCount() > 0)
- {
- artContainer.GoBack();
- }
- RefreshUI();
- }
- }
- public void SetTarget(ArtContainer art, bool isDel)
- {
- Init();
- if (isDel)
- {
- try
- {
- if (this.artContainer != null && artContainer.Transfer)
- {
- artContainer.Transfer.TransferStateChange -= OnTransferStateChange;
- }
- }
- catch
- {
- UnityLog.Instance.LogError("菜单编辑器错误");
- }
- this.artContainer = null;
- //gameObject.SetActive(false);
- return;
- }
- this.artContainer = art;
- if (art.Transfer)
- {
- art.Transfer.TransferStateChange += OnTransferStateChange;
- }
- var container = art.GetEditorCenter();
- if (container)
- {
- //transform.position = container.position;
- //transform.rotation = container.rotation;
- RefreshUI();
- }
- RefreshInfo();
- Refreshline();
- ReFresh();
- }
- public void ReFresh()
- {
- if (artContainer != null && artContainer.Transfer)
- {
- if (artContainer.Transfer.IsJiXingScale)
- {
- editToggleBtn.isOn = true;
- editToggleBtn.image.sprite = editToggleBtn.openImg;
- }
- else
- {
- editToggleBtn.isOn = false;
- editToggleBtn.image.sprite = editToggleBtn.closeImg;
- }
- }
- }
- private void OnTransferStateChange(TransferState state)
- {
- try
- {
- if (artContainer != null && artContainer.GetInstace())
- {
- if (state == TransferState.Stop)
- {
- lastTime = 0;
- //if (!gameObject.activeSelf)
- //{
- // gameObject.SetActive(true);
- //}
- RefreshUI();
- }
- else
- {
- //gameObject.SetActive(false);
- }
- }
- }
- catch
- {
- //gameObject.SetActive(false);
- }
- }
- /// <summary>
- /// 编辑同步和
- /// </summary>
- private void Update()
- {
- if (artContainer != null)
- {
- try
- {
- if (!artContainer.GetInstace())
- {
- artContainer = null;
- return;
- }
- }
- catch
- {
- artContainer = null;
- return;
- }
- if (Input.GetKeyDown(KeyCode.Space))
- {
- UnityLog.Instance.Log("Back:" + artContainer.BackCount() + " Ahead:" + artContainer.AheadCount());
- artContainer.CloneContainer();
- }
- //if (Time.time - lastTime > checkTime)
- //{
- // lastTime = Time.time;
- // artContainer.ResetEditorPosition();
- // var container = artContainer.GetEditorCenter();
- // if (container)
- // {
- // transform.position = container.position;
- // transform.rotation = container.rotation;
- // }
- //}
- }
- }
- private void RefreshInfo()
- {
- if (artContainer != null)
- {
- posXInputField.text = artContainer.Position.x.ToString();
- posYInputField.text = artContainer.Position.y.ToString();
- posZInputField.text = artContainer.Position.z.ToString();
- rotXInputField.text = artContainer.Angle.x.ToString();
- rotYInputField.text = artContainer.Angle.y.ToString();
- rotZInputField.text = artContainer.Angle.z.ToString();
- scaXInputField.text = artContainer.Scale.x.ToString();
- scaYInputField.text = artContainer.Scale.y.ToString();
- scaZInputField.text = artContainer.Scale.z.ToString();
- distanceText.text = artContainer.Distance.ToString("f2");//保留两位小数
- }
- }
- private Color activeColor = Color.white;
- private Color disActiveColor = Color.gray;
- private void RefreshUI()
- {
- bool isSelect = artContainer != null;
- Color color = isSelect ? activeColor : disActiveColor;
- lastStepBtn.enabled = isSelect && artContainer.BackCount() > 0;
- lastStepBtn.image.color = isSelect && artContainer.BackCount() > 0? activeColor: disActiveColor;
- nextStepBtn.enabled = isSelect && artContainer.AheadCount() > 0;
- nextStepBtn.image.color = isSelect && artContainer.AheadCount() > 0 ? activeColor : disActiveColor;
- copyBtn.enabled = isSelect;
- copyBtn.image.color = color;
- lineToggle_Switch.enabled = isSelect;
- editToggleBtn.enabled = isSelect;
- editToggleBtn.image.color = color;
- if (!isSelect && lineToggle_Switch.isOn)
- {
- lineToggle_Switch.isOn = false;
- }
- if (!isSelect && editToggleBtn.isOn)
- {
- editToggleBtn.isOn = false;
- }
- }
- private void Refreshline()
- {
- if (artContainer != null && lineToggle_Switch.isOn)
- {
- ShowLine();
- }
- }
- private void Cleartime()
- {
- if (artContainer != null)
- {
- artContainer.Transfer.ClearTime();
- }
- }
- public void DeleteRefesh()
- {
- lastStepBtn.enabled = false;
- lastIcon.color = Color.gray;
- nextStepBtn.enabled = false;
- nextIcon.color = Color.gray;
- lineToggle_Switch.isOn = false;
- ShowLine(false);
- posXInputField.text = "0";
- posYInputField.text = "0";
- posZInputField.text = "0";
- rotXInputField.text = "0";
- rotYInputField.text = "0";
- rotZInputField.text = "0";
- scaXInputField.text = "0";
- scaYInputField.text = "0";
- scaZInputField.text = "0";
- distanceText.text = "";
- }
- }
- }
|