113 lines
		
	
	
		
			3.5 KiB
		
	
	
	
		
			C#
		
	
	
	
			
		
		
	
	
			113 lines
		
	
	
		
			3.5 KiB
		
	
	
	
		
			C#
		
	
	
	
| using System.Collections;
 | |
| using System.Collections.Generic;
 | |
| using UnityEngine;
 | |
| 
 | |
| /// <summary>
 | |
| /// 电线对象
 | |
| /// </summary>
 | |
| public class WireObject : MonoBehaviour
 | |
| {
 | |
|     private Transform start, end;
 | |
|     private Vector3 startPos, endPos;
 | |
|     private float sagRatio;
 | |
|     private Transform parent;
 | |
| 
 | |
|     /// <summary>
 | |
|     /// 创建
 | |
|     /// </summary>
 | |
|     /// <param name="_parent">父物体</param>
 | |
|     /// <param name="_start">起点</param>
 | |
|     /// <param name="_end">终点</param>
 | |
|     /// <param name="_sagRatio">弧垂</param>
 | |
|     /// <returns></returns>
 | |
|     public static WireObject CreateWire(Transform _parent, Transform _start, Transform _end, float _sagRatio)
 | |
|     {
 | |
|         GameObject wire = new GameObject("wire");
 | |
|         var lr = wire.AddComponent<LineRenderer>();
 | |
|         //Material mat = new Material(WireCreater.Instance.linmaterial);
 | |
|         //mat.color = WireCreater.Instance.wireColor;
 | |
|         lr.material = WireCreater.Instance.linmaterial;
 | |
|         var dev_wire = wire.AddComponent<WireObject>();
 | |
|         dev_wire.start = _start;
 | |
|         dev_wire.end = _end;
 | |
|         dev_wire.sagRatio = _sagRatio;
 | |
|         dev_wire.startPos = _start.position;
 | |
|         dev_wire.endPos = _end.position;
 | |
|         dev_wire.parent = _parent;
 | |
|         if (_parent != null)
 | |
|             wire.transform.SetParent(_parent);
 | |
|         dev_wire.InitWire();
 | |
|         return dev_wire;
 | |
|     }
 | |
| 
 | |
|     /// <summary>
 | |
|     /// 创建
 | |
|     /// </summary>
 | |
|     /// <param name="_parent">父物体</param>
 | |
|     /// <param name="_start">起点</param>
 | |
|     /// <param name="_end">终点</param>
 | |
|     /// <param name="_sagRatio">弧垂</param>
 | |
|     /// <param name="wireName">名称</param>
 | |
|     /// <returns></returns>
 | |
|     public static WireObject CreateWire(Transform _parent, Transform _start, Transform _end, float _sagRatio, string wireName)
 | |
|     {
 | |
|         GameObject wire = new GameObject(string.IsNullOrEmpty(wireName) ? "wire" : wireName);
 | |
|         var lr = wire.AddComponent<LineRenderer>();
 | |
|         //Material mat = new Material(Shader.Find("Unlit/Color"));
 | |
|         //mat.color = WireCreater.Instance.wireColor;
 | |
|         lr.material = WireCreater.Instance.linmaterial;
 | |
|         var dev_wire = wire.AddComponent<WireObject>();
 | |
|         dev_wire.start = _start;
 | |
|         dev_wire.end = _end;
 | |
|         dev_wire.sagRatio = _sagRatio;
 | |
|         dev_wire.startPos = _start.position;
 | |
|         dev_wire.endPos = _end.position;
 | |
|         dev_wire.parent = _parent;
 | |
|         if (_parent != null)
 | |
|             wire.transform.SetParent(_parent);
 | |
|         dev_wire.InitWire();
 | |
|         return dev_wire;
 | |
|     }
 | |
| 
 | |
|     /// <summary>
 | |
|     /// 初始化连线
 | |
|     /// </summary>
 | |
|     public void InitWire()
 | |
|     {
 | |
|         for (int i = transform.childCount - 1; i >= 0; i--)
 | |
|         {
 | |
|             Destroy(transform.GetChild(i).gameObject);
 | |
|         }
 | |
|         WireCreater.Instance.CreateNewLine(this, start.position, end.position, sagRatio);
 | |
|     }
 | |
| 
 | |
|     /// <summary>
 | |
|     /// 设置弧垂
 | |
|     /// </summary>
 | |
|     /// <param name="_sagRatio"></param>
 | |
|     public void SetRatio(float _sagRatio)
 | |
|     {
 | |
|         sagRatio = _sagRatio;
 | |
|         InitWire();
 | |
|     }
 | |
| 
 | |
|     Vector3 GetPointPos(int index)
 | |
|     {
 | |
|         LineRenderer lr = gameObject.GetComponent<LineRenderer>();
 | |
|         return lr.GetPosition(index);
 | |
|     }
 | |
| 
 | |
|     private void Update()
 | |
|     {
 | |
|         if (WireCreater.Instance.realTimeWire)
 | |
|         {
 | |
|             if (start.position != startPos || end.position != endPos)
 | |
|             {
 | |
|                 InitWire();
 | |
|                 startPos = start.position;
 | |
|                 endPos = end.position;
 | |
|             }
 | |
|         }
 | |
|     }
 | |
| }
 |