262 lines
9.7 KiB
C#
262 lines
9.7 KiB
C#
using DG.Tweening;
|
||
using Newtonsoft.Json;
|
||
using System;
|
||
using System.Collections;
|
||
using System.Collections.Generic;
|
||
using System.Drawing;
|
||
using TMPro;
|
||
using UnityEngine;
|
||
|
||
public class LineManager : MonoBehaviour
|
||
{
|
||
public static LineManager instance;
|
||
public Transform point1;
|
||
public Transform point2;
|
||
public GameObject Tips;
|
||
public TextMeshProUGUI TipTexts;
|
||
public List<GameObject> Models = new List<GameObject>();
|
||
LineData lineData = new LineData();
|
||
public GameObject Lights;
|
||
private void Awake()
|
||
{
|
||
instance = this;
|
||
}
|
||
void Start()
|
||
{
|
||
Initialization();
|
||
}
|
||
private void Initialization()
|
||
{
|
||
if (PlayerPrefs.GetString("LineData") != "")
|
||
{
|
||
lineData = JsonConvert.DeserializeObject<LineData>(PlayerPrefs.GetString("LineData"));
|
||
|
||
for (int i = 0; i < lineData.Modelname.Count; i++)
|
||
{
|
||
for (int j = 0; j < Models.Count; j++)
|
||
{
|
||
if (lineData.Modelname[i].Contains(Models[j].name))
|
||
{
|
||
Models[j].SetActive(true);
|
||
}
|
||
if (lineData.Modelname[i].Equals(Models[j].name) && Models[j].name.Equals("diannaoxian"))
|
||
{
|
||
Models[j].transform.DOLocalMove(new Vector3(-0.56f, -0.293f, -0.163f), 0.5f);
|
||
}
|
||
}
|
||
}
|
||
|
||
}
|
||
}
|
||
bool isshow;
|
||
void Update()
|
||
{
|
||
if (Input.GetMouseButtonDown(2))
|
||
{
|
||
point1 = null;
|
||
point2 = null;
|
||
PlayerPrefs.SetString("LineData", "");
|
||
}
|
||
if (allline >= Models.Count && !isshow)
|
||
{
|
||
isshow = true;
|
||
TipTexts.text = "线已全部连接!";
|
||
StartCoroutine(WaitHide(Tips));
|
||
}
|
||
}
|
||
/// <summary>
|
||
/// 鼠标点击的物体
|
||
/// </summary>
|
||
/// <param name="point"></param>
|
||
public void ClickPoint(Transform point)
|
||
{
|
||
//if (point1 != null)
|
||
//{
|
||
// point2 = point;
|
||
//}
|
||
//if (point1 == null)
|
||
//{
|
||
// point1 = point;
|
||
//}
|
||
//if (point1 != null && point2 != null)
|
||
//{
|
||
// //StartCoroutine(Checkconnection(point1, point2));
|
||
//}
|
||
if (point.name.Equals("diannaoxian"))
|
||
{
|
||
if (!lineData.Modelname.Contains(point.name))
|
||
{
|
||
allline++;
|
||
lineData.Modelname.Add(point.name);
|
||
string json = JsonConvert.SerializeObject(lineData);
|
||
PlayerPrefs.SetString("LineData", json);
|
||
}
|
||
point.DOLocalMove(new Vector3(-0.56f, -0.293f, -0.163f), 0.5f);
|
||
}
|
||
|
||
if (point.name.Equals("开关"))
|
||
{
|
||
//if (!lineData.Modelname.Contains(point.name))
|
||
//{
|
||
// allline++;
|
||
// lineData.Modelname.Add(point.name);
|
||
// string json = JsonConvert.SerializeObject(lineData);
|
||
// PlayerPrefs.SetString("LineData", json);
|
||
//}
|
||
point.DORotateQuaternion(Quaternion.Euler(-17f, 0, 0), 0.5f);
|
||
}
|
||
if (point.name.Equals("闸"))
|
||
{
|
||
//if (!lineData.Modelname.Contains(point.name))
|
||
//{
|
||
// allline++;
|
||
// lineData.Modelname.Add(point.name);
|
||
// string json = JsonConvert.SerializeObject(lineData);
|
||
// PlayerPrefs.SetString("LineData", json);
|
||
//}
|
||
Material material = Lights.GetComponent<MeshRenderer>().material;
|
||
material.EnableKeyword("_EMISSION");
|
||
material.color = UnityEngine.Color.red;
|
||
point.DORotateQuaternion(Quaternion.Euler(-50f, 0, 0), 0.5f);
|
||
}
|
||
}
|
||
|
||
public IEnumerator Checkconnection(Transform transform, Transform transform2)
|
||
{
|
||
|
||
LineConnect line = transform.GetComponent<LineConnect>();
|
||
LineConnect line2 = transform2.GetComponent<LineConnect>();
|
||
if (line != null && line2 != null)
|
||
{
|
||
if (!JudgmentLine(line, line2))
|
||
{
|
||
yield return null;
|
||
StartCoroutine(WaitHide(Tips));
|
||
}
|
||
|
||
}
|
||
}
|
||
|
||
public bool JudgmentLine(LineConnect line1, LineConnect line2)
|
||
{
|
||
Init();
|
||
if (line1.interfaceType == InterfaceType.负12V && line2.interfaceType != InterfaceType.负12V ||
|
||
line2.interfaceType == InterfaceType.负12V && line1.interfaceType != InterfaceType.负12V)
|
||
{
|
||
TipTexts.text = "连接错误,-12V应该与-12V相连!";
|
||
LineShowModel.Instance.DeleteWiresByCollider(line1.gameObject);
|
||
return false;
|
||
}
|
||
else if (line1.interfaceType == InterfaceType.正12V && line2.interfaceType != InterfaceType.正12V
|
||
|| line2.interfaceType == InterfaceType.正12V && line1.interfaceType != InterfaceType.正12V
|
||
)
|
||
{
|
||
TipTexts.text = "连接错误,12V应该与12V相连!";
|
||
LineShowModel.Instance.DeleteWiresByCollider(line1.gameObject);
|
||
return false;
|
||
}
|
||
//else if (line1.interfaceType == InterfaceType.零V && line2.interfaceType != InterfaceType.GND
|
||
// || line2.interfaceType == InterfaceType.GND && line1.interfaceType != InterfaceType.零V)
|
||
//{
|
||
// TipTexts.text = "连接错误,0V应该与GND相连!";
|
||
// // WireDrawingSystem.instance.currentState = WireDrawingSystem.DrawingState.Idle;
|
||
// //WireDrawingSystem.instance.HandleDelete();
|
||
// //WireDrawingSystem.instance.ClearSnapPreview();
|
||
// LineShowModel.Instance.DeleteWiresByCollider(line1.gameObject);
|
||
// return false;
|
||
//}
|
||
else if (line1.interfaceType == InterfaceType.IN2_200K && line2.interfaceType != InterfaceType.OUT1
|
||
|| line2.interfaceType == InterfaceType.IN2_200K && line1.interfaceType != InterfaceType.OUT1)
|
||
{
|
||
TipTexts.text = "连接错误,200K应该与OUT1相连!";
|
||
LineShowModel.Instance.DeleteWiresByCollider(line1.gameObject);
|
||
return false;
|
||
}
|
||
else if (line1.interfaceType == InterfaceType.OUT1 && (line2.interfaceType != InterfaceType.IN4 && line2.interfaceType != InterfaceType.IN2_200K)
|
||
|| line2.interfaceType == InterfaceType.OUT1 && line1.interfaceType != InterfaceType.IN4)
|
||
{
|
||
TipTexts.text = "连接错误,OUT1应该与IN4相连!";
|
||
LineShowModel.Instance.DeleteWiresByCollider(line1.gameObject);
|
||
return false;
|
||
}
|
||
else if (line1.interfaceType == InterfaceType.AD1 && line2.interfaceType != InterfaceType.OUT2
|
||
|| line2.interfaceType == InterfaceType.AD1 && line1.interfaceType != InterfaceType.OUT2)
|
||
{
|
||
TipTexts.text = "连接错误,AD1应该与OUT2相连!";
|
||
LineShowModel.Instance.DeleteWiresByCollider(line1.gameObject);
|
||
return false;
|
||
}
|
||
else if ((line1.interfaceType == InterfaceType.零V && (line2.interfaceType != InterfaceType.GND && line2.interfaceType != InterfaceType.III)
|
||
|| line2.interfaceType == InterfaceType.GND && line1.interfaceType != InterfaceType.零V)
|
||
)
|
||
{
|
||
TipTexts.text = "连接错误,0V应该与GND相连!";
|
||
LineShowModel.Instance.DeleteWiresByCollider(line1.gameObject);
|
||
return false;
|
||
}
|
||
else if (line1.interfaceType == InterfaceType.OUT && line2.interfaceType != InterfaceType.IN1
|
||
|| line2.interfaceType == InterfaceType.OUT && line1.interfaceType != InterfaceType.IN1)
|
||
{
|
||
TipTexts.text = "连接错误,OUT应该与IN1相连!";
|
||
LineShowModel.Instance.DeleteWiresByCollider(line1.gameObject);
|
||
return false;
|
||
}
|
||
else if (line1.interfaceType == InterfaceType.AD2 && line2.interfaceType != InterfaceType.IN1
|
||
|| line2.interfaceType == InterfaceType.AD2 && line1.interfaceType != InterfaceType.IN1)
|
||
{
|
||
TipTexts.text = "连接错误,AD2应该与IN1相连!";
|
||
LineShowModel.Instance.DeleteWiresByCollider(line1.gameObject);
|
||
return false;
|
||
}
|
||
else if (line1.IsNotLine == true || line2.IsNotLine == true)
|
||
{
|
||
TipTexts.text = "连接错误,不需要此连线";
|
||
// WireDrawingSystem.instance.currentState = WireDrawingSystem.DrawingState.Idle;
|
||
//WireDrawingSystem.instance.HandleDelete();
|
||
//WireDrawingSystem.instance.ClearSnapPreview();
|
||
LineShowModel.Instance.DeleteWiresByCollider(line1.gameObject);
|
||
return false;
|
||
}
|
||
else
|
||
{
|
||
for (int i = 0; i < Models.Count; i++)
|
||
{
|
||
string[] splitname = Models[i].name.Split('连');
|
||
if (splitname[0] == line1.interfaceType.ToString() && splitname[1] == line2.interfaceType.ToString() ||
|
||
splitname[0] == line2.interfaceType.ToString() && splitname[1] == line1.interfaceType.ToString())
|
||
{
|
||
if (!lineData.Modelname.Contains(Models[i].name))
|
||
{
|
||
allline++;
|
||
lineData.Modelname.Add(Models[i].name);
|
||
string json = JsonConvert.SerializeObject(lineData);
|
||
PlayerPrefs.SetString("LineData", json);
|
||
}
|
||
Models[i].SetActive(true);
|
||
}
|
||
|
||
}
|
||
LineShowModel.Instance.DeleteWiresByCollider(line1.gameObject);
|
||
return true;
|
||
}
|
||
}
|
||
int allline = 0;
|
||
public void Init()
|
||
{
|
||
point2 = null;
|
||
point1 = null;
|
||
}
|
||
|
||
IEnumerator WaitHide(GameObject game)
|
||
{
|
||
game.SetActive(true);
|
||
yield return new WaitForSeconds(1.5f);
|
||
game.SetActive(false);
|
||
}
|
||
}
|
||
|
||
public class LineData
|
||
{
|
||
public List<string> Modelname = new List<string>();
|
||
}
|