240 lines
6.9 KiB
C#
240 lines
6.9 KiB
C#
using HighlightPlus;
|
|
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using UnityEngine;
|
|
|
|
/// <summary>
|
|
/// 一组连线控制
|
|
/// </summary>
|
|
public class Line_group : MonoBehaviour
|
|
{
|
|
public static GameObject lineitemPrefb;
|
|
|
|
public Material redMat;
|
|
public Material greenMat;
|
|
public Material yellowMat;
|
|
public Material blackMat;
|
|
|
|
/// <summary>
|
|
/// 中点
|
|
/// </summary>
|
|
public List<Line_mid> line_Mids=new List<Line_mid>();
|
|
/// <summary>
|
|
/// 线
|
|
/// </summary>
|
|
public List<Line_item> line_Items=new List<Line_item>();
|
|
/// <summary>
|
|
/// 端点
|
|
public List<Line_end> line_Ends= new List<Line_end>();
|
|
|
|
|
|
/// <summary>
|
|
/// 当前选中的线
|
|
/// </summary>
|
|
public Line_item currentLine_Item;
|
|
|
|
/// <summary>
|
|
/// 删除线回调
|
|
/// </summary>
|
|
public Action<Line_item> deletLineAction;
|
|
/// <summary>
|
|
/// 画线回调
|
|
/// </summary>
|
|
public Action<Line_item> drawEndPointAction;
|
|
|
|
private void Awake()
|
|
{
|
|
line_Mids=transform.GetComponentsInChildren<Line_mid>(true).ToList();
|
|
line_Ends = transform.GetComponentsInChildren<Line_end>(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<Line_item>() != null)
|
|
{
|
|
//点击线
|
|
hit.transform.GetComponent<Line_item>().MyOnMouseDown();
|
|
}
|
|
else if(hit.transform.GetComponent<Line_end>()!=null)
|
|
{
|
|
hit.transform.GetComponent<Line_end>().MyOnMDown();
|
|
}
|
|
else if(hit.transform.GetComponent<Line_mid>() != null)
|
|
{
|
|
hit.transform.GetComponent<Line_mid>().MyOnMDown();
|
|
}
|
|
}
|
|
else
|
|
{
|
|
//不在画线中,点到空气取消选择
|
|
if (!(currentLine_Item != null && currentLine_Item.line_Ends[1]==null))
|
|
{
|
|
ChoseLine(null);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 选中线
|
|
/// </summary>
|
|
/// <param name="line"></param>
|
|
public void ChoseLine(Line_item line)
|
|
{
|
|
if (line != null)
|
|
{
|
|
currentLine_Item = line;
|
|
line_Items.ForEach(item =>
|
|
{
|
|
item.transform.GetComponent<HighlightEffect>().highlighted = (item == line);
|
|
});
|
|
Debug.Log("选择线"+ line.name);
|
|
}
|
|
else
|
|
{
|
|
currentLine_Item = null;
|
|
line_Items.ForEach(item =>
|
|
{
|
|
item.transform.GetComponent<HighlightEffect>().highlighted = false;
|
|
});
|
|
Debug.Log("取消选择");
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 选择材质
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public Material GetCurrentLineMaterial()
|
|
{
|
|
if(LiveSceneManager.Instance.currentTool!=null && LiveSceneManager.Instance.currentTool.name.Contains("绝缘导线"))
|
|
{
|
|
Tool_InsulatedConductor ic= LiveSceneManager.Instance.currentTool.GetComponent<Tool_InsulatedConductor>();
|
|
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;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 直接生成连线
|
|
/// </summary>
|
|
/// <param name="end1"></param>
|
|
/// <param name="end2"></param>
|
|
/// <param name="colorMat"></param>
|
|
public void CreateLine(string end1,List<string> mids,string end2,string colorMat)
|
|
{
|
|
if (!CheckLineItem(end1, end2))
|
|
{
|
|
|
|
if (lineitemPrefb == null)
|
|
lineitemPrefb = Resources.Load<GameObject>("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<GameObject>(lineitemPrefb);
|
|
Line_item item = obj.GetComponent<Line_item>();
|
|
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);
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 删除线
|
|
/// </summary>
|
|
public void DestroyLineItem(Line_item line_Item)
|
|
{
|
|
line_Items.Remove(line_Item);
|
|
deletLineAction?.Invoke(currentLine_Item);
|
|
Destroy(line_Item.gameObject);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 查找线
|
|
/// </summary>
|
|
/// <param name="end1"></param>
|
|
/// <param name="end2"></param>
|
|
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));
|
|
}
|
|
}
|