123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209 |
- 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<Vector3> 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<LineRenderer>();
- }
- return line;
- }
- }
- public LineContainer Container { get => container; set => container = value; }
- public bool IsSingleLine { get => isSingleLine; set => isSingleLine = value; }
- /// <summary>
- /// 设置材质
- /// </summary>
- /// <param name="mate"></param>
- 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);
- }
- }
- }
- /// <summary>
- /// 设置线的相关属性
- /// </summary>
- /// <param name="lineColor"></param>
- /// <param name="radius"></param>
- 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;
- }
- }
- /// <summary>
- /// 添加新的点
- /// 每添加一个点即开始同步
- /// </summary>
- /// <param name="newPos"></param>
- 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<Vector3>();
- }
- 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));
- }
- }
- /// <summary>
- /// 此线段已完成绘制,最后同步一次,不再同步
- /// </summary>
- public void StopSyn()
- {
- if (IsSingleLine && Container)
- {
- Container.SetExtendData(JsonConvert.SerializeObject(lineData), false);
- }
- }
- /// <summary>
- /// 删除此线段,同步删除
- /// </summary>
- 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);
- }
- }
- }
|