using HighlightPlus; using System; using System.Collections; using System.Collections.Generic; using System.Linq; using UnityEngine; /// /// 一组连线控制 /// public class Line_group : MonoBehaviour { public static GameObject lineitemPrefb; public Material redMat; public Material greenMat; public Material yellowMat; public Material blackMat; /// /// 中点 /// public List line_Mids=new List(); /// /// 线 /// public List line_Items=new List(); /// /// 端点 public List line_Ends= new List(); /// /// 当前选中的线 /// public Line_item currentLine_Item; /// /// 删除线回调 /// public Action deletLineAction; /// /// 画线回调 /// public Action drawEndPointAction; private void Awake() { line_Mids=transform.GetComponentsInChildren(true).ToList(); line_Ends = transform.GetComponentsInChildren(true).ToList(); line_Mids.ForEach(a => { a.Init(this); a.gameObject.SetActive(false); }); line_Ends.ForEach(a => { a.Init(this); a.gameObject.SetActive(false); }); } private void Update() { //删除线 if(currentLine_Item!=null && Input.GetKeyDown(KeyCode.Delete)) { DestroyLineItem(currentLine_Item); currentLine_Item =null; } //准备画下一个点 if(currentLine_Item != null) { if(currentLine_Item.line_Ends[1] == null) { currentLine_Item.SetHandPoint(); } else { currentLine_Item.SetNoHandPoint(); } } //选中线 if (Input.GetMouseButtonDown(0)) { Ray ray=Camera.main.ScreenPointToRay(Input.mousePosition); if (Physics.Raycast(ray, out RaycastHit hit,10f,1 << LayerMask.NameToLayer("Line"))) { if (hit.transform.GetComponent() != null) { //点击线 hit.transform.GetComponent().MyOnMouseDown(); } else if(hit.transform.GetComponent()!=null) { hit.transform.GetComponent().MyOnMDown(); } else if(hit.transform.GetComponent() != null) { hit.transform.GetComponent().MyOnMDown(); } } else { //不在画线中,点到空气取消选择 if (!(currentLine_Item != null && currentLine_Item.line_Ends[1]==null)) { ChoseLine(null); } } } } /// /// 选中线 /// /// public void ChoseLine(Line_item line) { if (line != null) { currentLine_Item = line; line_Items.ForEach(item => { item.transform.GetComponent().highlighted = (item == line); }); Debug.Log("选择线"+ line.name); } else { currentLine_Item = null; line_Items.ForEach(item => { item.transform.GetComponent().highlighted = false; }); Debug.Log("取消选择"); } } /// /// 选择材质 /// /// public Material GetCurrentLineMaterial() { if(LiveSceneManager.Instance.currentTool!=null && LiveSceneManager.Instance.currentTool.name.Contains("绝缘导线")) { Tool_InsulatedConductor ic= LiveSceneManager.Instance.currentTool.GetComponent(); if(ic!=null) { if(ic.currentChoseLine != null) { if (ic.currentChoseLine.name == "红") return redMat; else if (ic.currentChoseLine.name == "黄") return yellowMat; else if (ic.currentChoseLine.name == "绿") return greenMat; else if (ic.currentChoseLine.name == "黑") return blackMat; } else { TipPanel.ShowTip("请先选择绝缘导线线圈!"); } } } return null; } /// /// 直接生成连线 /// /// /// /// public void CreateLine(string end1,List mids,string end2,string colorMat) { if (!CheckLineItem(end1, end2)) { if (lineitemPrefb == null) lineitemPrefb = Resources.Load("Prefabs/Objects/Line/线"); Material mat = null; if (colorMat == "红") mat = redMat; else if (colorMat == "黄") mat = yellowMat; else if (colorMat == "绿") mat = greenMat; else if (colorMat == "黑") mat = blackMat; else return; Line_end endL1 = line_Ends.Find(a => a.triggerName == end1); Line_end endL2 = line_Ends.Find(a => a.triggerName == end2); if (endL1 != null && endL2 != null) { GameObject obj = Instantiate(lineitemPrefb); Line_item item = obj.GetComponent(); item.Init(mat, this, endL1); if (mids != null) { mids.ForEach(mid => { Line_mid limemid = line_Mids.Find(a => a.triggerName == mid); item.AddPoint(limemid); }); } item.AddPoint(endL2); } } } /// /// 删除线 /// public void DestroyLineItem(Line_item line_Item) { line_Items.Remove(line_Item); deletLineAction?.Invoke(currentLine_Item); Destroy(line_Item.gameObject); } /// /// 查找线 /// /// /// public Line_item CheckLineItem(string end1, string end2) { return line_Items.Find(line => line.line_Ends[1] != null && line.line_Ends.Any(end => end.triggerName == end1) && line.line_Ends.Any(end => end.triggerName == end2)); } }