using Newtonsoft.Json; using PublicTools.Unity; using ShadowStudio.Model; using System; using System.Collections; using System.Collections.Generic; using UnityEngine; using XRTool.Util; namespace ShadowStudio.Tool { public struct LineData { public string lineName; public int index; public Vector3 position; public List positions; public XColor color; public float radius; } [RequireComponent(typeof(LineRenderer))] public class LineSegment : MonoBehaviour { private LineRenderer line; private LineData lineData = new LineData(); private bool isSendWeb = false; private LineContainer container; private static MaterialPropertyBlock block; private bool isSingleLine = true; public LineRenderer Line { get { if (!line) { line = GetComponent(); } return line; } } public LineContainer Container { get => container; set => container = value; } public bool IsSingleLine { get => isSingleLine; set => isSingleLine = value; } /// /// 设置材质 /// /// public void SetLine(Material mate) { if (Line && mate) { UnityUtil.CopyMate(Line.sharedMaterial, mate); } } public void SetLine(LineData lineData) { if (line.positionCount < 1) { SetLine(lineData.color.Trans(), lineData.radius); Vector3[] pos = lineData.positions.ToArray(); line.positionCount = pos.Length; line.SetPositions(pos); } else { if (line.positionCount == lineData.positions.Count - 1) { Line.positionCount = lineData.positions.Count; Line.SetPosition(Line.positionCount - 1, lineData.positions[lineData.positions.Count - 1]); } else { Vector3[] pos = lineData.positions.ToArray(); line.positionCount = pos.Length; line.SetPositions(pos); } } } /// /// 设置线的相关属性 /// /// /// public void SetLine(Color lineColor, float radius, string mainColorName = "_Color") { if (Line) { if (block == null) { block = new MaterialPropertyBlock(); } UnityUtil.ChangeMateColor(block, Line, lineColor, mainColorName); Line.widthMultiplier = radius; lineData.color = new XColor(lineColor); lineData.radius = radius; lineData.lineName = name; } } /// /// 添加新的点 /// 每添加一个点即开始同步 /// /// public void AddPosition(Vector3 newPos) { if (Line) { Line.positionCount = Line.positionCount + 1; newPos = transform.InverseTransformPoint(newPos); newPos.x = (float)Math.Round(newPos.x, 3); newPos.y = (float)Math.Round(newPos.y, 3); newPos.z = (float)Math.Round(newPos.z, 3); Line.SetPosition(Line.positionCount - 1, newPos); if (Line.positionCount > 1) { SendData(newPos); } } } public void SendData(Vector3 newPos) { lineData.index = Line.positionCount; lineData.position = newPos; if (lineData.positions == null) { lineData.positions = new List(); } lineData.positions.Add(newPos); if (!IsSingleLine) { return; } ///没有发送,则发送创建线的事件 ///如果已发送,需要判断容器是否已创建,如果没有创建,则不同步消息 ///如果已创建,则开始同步消息 if (!isSendWeb) { isSendWeb = true; ArtInfo info = ArtInfoMgr.Instance.GetArtInfo(DrawPener.artLineId); if (info != null) { if (WSHandler.Room != null) { //Debug.Log(lineData.color); //Debug.Log(lineData.index); //Debug.Log(lineData.lineName); //Debug.Log(lineData.position); //Debug.Log(lineData.positions); //Debug.Log(lineData.radius); WSHandler.Room.CreateGood(info.AID,info.ArtId, info.ArtName, UnityUtil.TransferToString(transform, 2), JsonConvert.SerializeObject(lineData)); } } else { UnityLog.Instance.Log(DrawPener.artLineId + " is null"); } } else if (Container) { SendData(); } } public void SendData() { if (Container) { Container.SetExtendData(JsonConvert.SerializeObject(lineData)); } } /// /// 此线段已完成绘制,最后同步一次,不再同步 /// public void StopSyn() { if (IsSingleLine && Container) { Container.SetExtendData(JsonConvert.SerializeObject(lineData), false); } } /// /// 删除此线段,同步删除 /// public void DelLine() { if (IsSingleLine && Container) { Container.DelLine(); } Container = null; } public void SetPosition(Vector3 newPos) { newPos = transform.InverseTransformPoint(newPos); newPos.x = (float)Math.Round(newPos.x, 3); newPos.y = (float)Math.Round(newPos.y, 3); newPos.z = (float)Math.Round(newPos.z, 3); Line.SetPosition(Line.positionCount - 1, newPos); SendData(newPos); } } }