大连中石油电子沙盘
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

827 lines
33 KiB

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using AX.MessageSystem;
using UnityEngine.EventSystems;
using System;
[RequireComponent(typeof(Polygon))]
public class PolygonController : MonoBehaviour
{
[HideInInspector]
public List<Vector3> screenPositions = new List<Vector3>();
[HideInInspector]
public List<Vector3> worldPositions = new List<Vector3>();
[HideInInspector]
public List<GameObject> vertices = new List<GameObject>();
private Polygon polygon;
public Vector3 InfoOffset = new Vector3(0, 1.5f, 0.0f);
public bool Editable = true;
public GameObject VerticePrefab;
public Canvas Canvas;
private GameObject PolygonPlane;
RaycastHit hit;
public RectTransform PolyVerticeFather;
// 面片的高度偏移量,以免跟地面重叠,导致看不到
private static Vector3 HeightOffset = new Vector3(0, 1f, 0.0f);
Transform Info;
/// <summary>
/// 获得多边形面片的高度。
/// </summary>
public float GetHeight()
{
if (worldPositions.Count > 0)
return worldPositions[0].y;
return transform.position.y;
}
/// <summary>
/// 用于调整面片内部填充的颜色,支持透明通道。
/// </summary>
public Color Color
{
get { return polygon.Color; }
set { polygon.Color = value; }
}
void Awake()
{
polygon = GetComponent<Polygon>();
MessageDispatcher.AddListener("ControlPolygonTrue", ControlPolygonTrue);
MessageDispatcher.AddListener("ControlPolygonFalse", ControlPolygonFalse);
if (Canvas == null)
Canvas = GameObject.Find("Canvas").GetComponent<Canvas>();
Info = this.transform.Find("info");
PolyVerticeFather = Canvas.transform.Find("SetAreaPanel/PolyVerticeFather") as RectTransform;
MessageDispatcher.AddListener("ReplayEvent", ExecuteEvent);
}
/// <summary>
/// 回放事件执行
/// </summary>
/// <param name="obj"></param>
private void ExecuteEvent(IMessage obj)
{
var eventData = (EventData)obj.Data;
if (eventData.cloneObjType == CloneObjType.SetArea)
{
if (eventData.eventType == RecordEventType.ToolTask)
{
var arg = Newtonsoft.Json.JsonConvert.DeserializeObject<RecordSetArea>(eventData.json);
if (arg.objectName == gameObject.name)
{
SetPolygon(arg.hitPos, arg.mousePos, arg);
}
}
else if (eventData.eventType == RecordEventType.SetAreaChange)
{
var arg = Newtonsoft.Json.JsonConvert.DeserializeObject<RecordSetAreaChange>(eventData.json);
if (arg.objectName == gameObject.name)
{
ReplayChangePosition(arg.index, arg.position, arg.point, arg);
}
}
else if (eventData.eventType == RecordEventType.SetAreaAdd)
{
var arg = Newtonsoft.Json.JsonConvert.DeserializeObject<RecordSetAreaAdd>(eventData.json);
if (arg.objectName == gameObject.name)
{
AddPolygon(arg.addIndex, arg.hitPos, arg.MousePos, arg);
}
}
else if (eventData.eventType == RecordEventType.SetAreaDel)
{
var arg = Newtonsoft.Json.JsonConvert.DeserializeObject<RecordSetAreaDel>(eventData.json);
if (arg.objectName == gameObject.name)
{
DelPolygon(arg.delIndex);
}
}
}
}
private void Start()
{
//if (worldPositions.Count > 0)
//{
// Info.position = polygon.getCenterOfGravityPoint(worldPositions) + InfoOffset;
//}
}
bool Control = false;
public void ControlPolygonTrue(IMessage Meg)//能否控制面片
{
if ((string)Meg.Data == gameObject.name)
{
Editable = true;
Control = true;//控制第一次点击物体从而会实例化一个新的圆圈
for (var i = 0; i < vertices.Count; ++i)
{
vertices[i].SetActive(true);
}
this.gameObject.layer = 8;
}
else
{
Editable = false;
for (var i = 0; i < vertices.Count; ++i)
{
vertices[i].SetActive(false);
}
this.gameObject.layer = 0;
}
}
public void ControlPolygonFalse(IMessage Meg)
{
Editable = false;
for (var i = 0; i < vertices.Count; ++i)
{
vertices[i].SetActive(false);
}
this.gameObject.layer = 0;
}
private void OnDestroy()
{
MessageDispatcher.RemoveListener("ControlPolygonTrue", ControlPolygonTrue);
MessageDispatcher.RemoveListener("ControlPolygonFalse", ControlPolygonFalse);
for (var i = 0; i < vertices.Count; ++i)
{
Destroy(vertices[i]);
}
MessageDispatcher.RemoveListener("ReplayEvent", ExecuteEvent);
}
public void SetPolygon(Vector3 hitPos, Vector3 MousePos, float offset = 1)
{
var position = MousePos;
var index = FindIndex(position);
if (index == -1)
{
HeightOffset = new Vector3(0, offset, 0);
var worldPos = hitPos + HeightOffset;
if (worldPositions.Count > 0)
worldPos.Set(worldPos.x, GetHeight(), worldPos.z);
var screenPos = position;
if (worldPositions.Count == 0)
{
MessageDispatcher.SendMessage(1f, "NodeTreeHints", (object)"在此区域设置");
}
screenPositions.Add(screenPos);
worldPositions.Add(worldPos);
if (worldPositions.Count == 3)
{
var s = Info.gameObject.AddComponent<CloneGameObjInfo>();
s.buildNum = GetComponent<CloneGameObjInfo>().buildNum;
s.gameObjType = GetComponent<CloneGameObjInfo>().gameObjType;
s.floorNum = GetComponent<CloneGameObjInfo>().floorNum;
s.interlayerNum = GetComponent<CloneGameObjInfo>().interlayerNum;
//s.deltaFloor = GetComponent<CloneGameObjInfo>().deltaFloor;
//Info.position = worldPositions[0] + new Vector3(0, 1f, 0);
Info.gameObject.SetActive(true);
}
//FIXME: 时间紧迫,将来这里可能需要解耦
var go = Instantiate(VerticePrefab);
var rectTransform = go.transform as RectTransform;
// var polyVerticeFather = Canvas.transform.Find("PolyVerticeFather");
rectTransform.parent = PolyVerticeFather;
rectTransform.localScale = Vector3.one;
var script = go.GetComponent<DragVertice>();
script.PolygonController = this;
script.Canvas = Canvas;
vertices.Add(go);
//go.name = GetComponent<CloneGameObjInfo>().gameObjID.ToString() +"-"+ vertices.IndexOf(go).ToString();
if (Canvas.renderMode == RenderMode.ScreenSpaceOverlay)
{
Vector2 anchoredPos;
if (RectTransformUtility.ScreenPointToLocalPointInRectangle(PolyVerticeFather, screenPos, null, out anchoredPos))
rectTransform.anchoredPosition = anchoredPos;
}
else if (Canvas.renderMode == RenderMode.ScreenSpaceCamera)
{
Vector2 mousePos;
if (RectTransformUtility.ScreenPointToLocalPointInRectangle(PolyVerticeFather, screenPos, Canvas.worldCamera, out mousePos))
rectTransform.anchoredPosition = mousePos;
}
if (worldPositions.Count > 2)
{
polygon.SetVertices(worldPositions.ToArray());
Info.position = polygon.getCenterOfGravityPoint(worldPositions) + InfoOffset;
}
}
}
public void SetPolygon(Vector3 hitPos, Vector3 MousePos, RecordSetArea arg, float offset = 1)
{
var position = MousePos;
var index = FindIndex(position);
if (index == -1)
{
HeightOffset = new Vector3(0, offset, 0);
var worldPos = hitPos + HeightOffset;
if (worldPositions.Count > 0)
worldPos.Set(worldPos.x, GetHeight(), worldPos.z);
var screenPos = position;
if (worldPositions.Count == 0)
{
MessageDispatcher.SendMessage(1f, "NodeTreeHints", (object)"在此区域设置");
}
screenPositions.Add(screenPos);
worldPositions.Add(worldPos);
if (worldPositions.Count == 3)
{
var s = Info.gameObject.AddComponent<CloneGameObjInfo>();
s.buildNum = GetComponent<CloneGameObjInfo>().buildNum;
s.gameObjType = GetComponent<CloneGameObjInfo>().gameObjType;
s.floorNum = GetComponent<CloneGameObjInfo>().floorNum;
s.interlayerNum = GetComponent<CloneGameObjInfo>().interlayerNum;
//s.deltaFloor = GetComponent<CloneGameObjInfo>().deltaFloor;
//Info.position = worldPositions[0] + new Vector3(0, 1f, 0);
Info.gameObject.SetActive(true);
}
//FIXME: 时间紧迫,将来这里可能需要解耦
var go = Instantiate(VerticePrefab);
var rectTransform = go.GetComponent<RectTransform>();
// var polyVerticeFather = Canvas.transform.Find("PolyVerticeFather");
rectTransform.parent = PolyVerticeFather;
rectTransform.localScale = Vector3.one;
var script = go.GetComponent<DragVertice>();
script.PolygonController = this;
script.Canvas = Canvas;
vertices.Add(go);
Vector3 nowcamerapos = Camera.main.transform.position;
Quaternion nowrotate = Camera.main.transform.rotation;
Camera.main.transform.localPosition = arg.myTransform.getMyPosition();
Camera.main.transform.localRotation = arg.myTransform.getMyRotation();
//go.name = GetComponent<CloneGameObjInfo>().gameObjID.ToString() +"-"+ vertices.IndexOf(go).ToString();
if (Canvas.renderMode == RenderMode.ScreenSpaceOverlay)
{
Vector2 anchoredPos;
if (RectTransformUtility.ScreenPointToLocalPointInRectangle(PolyVerticeFather, screenPos, null, out anchoredPos))
rectTransform.anchoredPosition = anchoredPos;
}
else if (Canvas.renderMode == RenderMode.ScreenSpaceCamera)
{
Vector2 mousePos;
if (RectTransformUtility.ScreenPointToLocalPointInRectangle(PolyVerticeFather, screenPos, Canvas.worldCamera, out mousePos))
rectTransform.anchoredPosition = mousePos;
}
if (worldPositions.Count > 2)
{
polygon.SetVertices(worldPositions.ToArray());
Info.position = polygon.getCenterOfGravityPoint(worldPositions) + InfoOffset;
}
Camera.main.transform.position = nowcamerapos;
Camera.main.transform.rotation = nowrotate;
}
}
/// <summary>
/// 回放帧状态
/// </summary>
/// <param name="worldPos"></param>
/// <param name="screenPos"></param>
public void ReplaySetPolygon(Vector3 worldPos, Vector3 screenPos)
{
var index = FindIndex(screenPos);
if (index == -1)
{
/*if (worldPositions.Count > 0)
worldPos.Set(worldPos.x, GetHeight(), worldPos.z);*/
if (worldPositions.Count == 0)
{
MessageDispatcher.SendMessage(1f, "NodeTreeHints", (object)"在此区域设置");
}
screenPositions.Add(screenPos);
worldPositions.Add(worldPos);
if (worldPositions.Count == 3)
{
var s = Info.gameObject.AddComponent<CloneGameObjInfo>();
s.buildNum = GetComponent<CloneGameObjInfo>().buildNum;
s.gameObjType = GetComponent<CloneGameObjInfo>().gameObjType;
s.floorNum = GetComponent<CloneGameObjInfo>().floorNum;
s.interlayerNum = GetComponent<CloneGameObjInfo>().interlayerNum;
//s.deltaFloor = GetComponent<CloneGameObjInfo>().deltaFloor;
//Info.position = worldPositions[0] + new Vector3(0, 1f, 0);
Info.gameObject.SetActive(true);
}
//FIXME: 时间紧迫,将来这里可能需要解耦
var go = Instantiate(VerticePrefab);
var rectTransform = go.transform as RectTransform;
// var polyVerticeFather = Canvas.transform.Find("PolyVerticeFather");
rectTransform.parent = PolyVerticeFather;
rectTransform.localScale = Vector3.one;
var script = go.GetComponent<DragVertice>();
script.PolygonController = this;
script.Canvas = Canvas;
vertices.Add(go);
//go.name = GetComponent<CloneGameObjInfo>().gameObjID.ToString() + "-" + vertices.IndexOf(go).ToString();
if (Canvas.renderMode == RenderMode.ScreenSpaceOverlay)
{
Vector2 anchoredPos;
if (RectTransformUtility.ScreenPointToLocalPointInRectangle(PolyVerticeFather, screenPos, null, out anchoredPos))
rectTransform.anchoredPosition = anchoredPos;
}
else if (Canvas.renderMode == RenderMode.ScreenSpaceCamera)
{
Vector2 mousePos;
if (RectTransformUtility.ScreenPointToLocalPointInRectangle(PolyVerticeFather, screenPos, Canvas.worldCamera, out mousePos))
rectTransform.anchoredPosition = mousePos;
}
if (worldPositions.Count > 2)
{
polygon.SetVertices(worldPositions.ToArray());
Info.position = polygon.getCenterOfGravityPoint(worldPositions) + InfoOffset;
}
}
}
public void AddPolygon(int addIndex, Vector3 hitPos, Vector3 MousePos)
{
var position = MousePos;
var index = FindIndex(position);
if (index == -1)
{
var worldPos = hitPos + HeightOffset;
if (worldPositions.Count > 0)
worldPos.Set(worldPos.x, GetHeight(), worldPos.z);
var screenPos = position;
if (worldPositions.Count == 0)
{
MessageDispatcher.SendMessage(1f, "NodeTreeHints", (object)"在此区域设置");
}
screenPositions.Insert(addIndex, screenPos);
worldPositions.Insert(addIndex, worldPos);
if (worldPositions.Count == 3)
{
var s = Info.gameObject.AddComponent<CloneGameObjInfo>();
s.buildNum = GetComponent<CloneGameObjInfo>().buildNum;
s.gameObjType = GetComponent<CloneGameObjInfo>().gameObjType;
s.floorNum = GetComponent<CloneGameObjInfo>().floorNum;
s.interlayerNum = GetComponent<CloneGameObjInfo>().interlayerNum;
//s.deltaFloor = GetComponent<CloneGameObjInfo>().deltaFloor;
//Info.position = worldPositions[0] + new Vector3(0, 1f, 0);
Info.gameObject.SetActive(true);
}
//FIXME: 时间紧迫,将来这里可能需要解耦
var go = Instantiate(VerticePrefab);
var rectTransform = go.transform as RectTransform;
// var polyVerticeFather = Canvas.transform.Find("PolyVerticeFather");
rectTransform.parent = PolyVerticeFather;
rectTransform.localScale = Vector3.one;
var script = go.GetComponent<DragVertice>();
script.PolygonController = this;
script.Canvas = Canvas;
vertices.Insert(addIndex, go);
if (Canvas.renderMode == RenderMode.ScreenSpaceOverlay)
{
Vector2 anchoredPos;
if (RectTransformUtility.ScreenPointToLocalPointInRectangle(PolyVerticeFather, screenPos, null, out anchoredPos))
rectTransform.anchoredPosition = anchoredPos;
}
else if (Canvas.renderMode == RenderMode.ScreenSpaceCamera)
{
Vector2 mousePos;
if (RectTransformUtility.ScreenPointToLocalPointInRectangle(PolyVerticeFather, screenPos, Canvas.worldCamera, out mousePos))
rectTransform.anchoredPosition = mousePos;
}
if (worldPositions.Count > 2)
{
polygon.SetVertices(worldPositions.ToArray());
Info.position = polygon.getCenterOfGravityPoint(worldPositions) + InfoOffset;
}
}
AddRecordAdd(addIndex, hitPos, MousePos);
}
public void AddPolygon(int addIndex, Vector3 hitPos, Vector3 MousePos, RecordSetAreaAdd arg)
{
var position = MousePos;
var index = FindIndex(position);
if (index == -1)
{
var worldPos = hitPos + HeightOffset;
if (worldPositions.Count > 0)
worldPos.Set(worldPos.x, GetHeight(), worldPos.z);
var screenPos = position;
if (worldPositions.Count == 0)
{
MessageDispatcher.SendMessage(1f, "NodeTreeHints", (object)"在此区域设置");
}
screenPositions.Insert(addIndex, screenPos);
worldPositions.Insert(addIndex, worldPos);
if (worldPositions.Count == 3)
{
var s = Info.gameObject.AddComponent<CloneGameObjInfo>();
s.buildNum = GetComponent<CloneGameObjInfo>().buildNum;
s.gameObjType = GetComponent<CloneGameObjInfo>().gameObjType;
s.floorNum = GetComponent<CloneGameObjInfo>().floorNum;
s.interlayerNum = GetComponent<CloneGameObjInfo>().interlayerNum;
//s.deltaFloor = GetComponent<CloneGameObjInfo>().deltaFloor;
//Info.position = worldPositions[0] + new Vector3(0, 1f, 0);
Info.gameObject.SetActive(true);
}
//FIXME: 时间紧迫,将来这里可能需要解耦
var go = Instantiate(VerticePrefab);
var rectTransform = go.GetComponent<RectTransform>();
// var polyVerticeFather = Canvas.transform.Find("PolyVerticeFather");
rectTransform.SetParent(PolyVerticeFather);
rectTransform.localScale = Vector3.one;
var script = go.GetComponent<DragVertice>();
script.PolygonController = this;
script.Canvas = Canvas;
vertices.Insert(addIndex, go);
Vector3 nowcamerapos = Camera.main.transform.position;
Quaternion nowrotate = Camera.main.transform.rotation;
Camera.main.transform.localPosition = arg.myTransform.getMyPosition();
Camera.main.transform.localRotation = arg.myTransform.getMyRotation();
if (Canvas.renderMode == RenderMode.ScreenSpaceOverlay)
{
Vector2 anchoredPos;
if (RectTransformUtility.ScreenPointToLocalPointInRectangle(PolyVerticeFather, screenPos, null, out anchoredPos))
rectTransform.anchoredPosition = anchoredPos;
}
else if (Canvas.renderMode == RenderMode.ScreenSpaceCamera)
{
Vector2 mousePos;
if (RectTransformUtility.ScreenPointToLocalPointInRectangle(PolyVerticeFather, screenPos, Canvas.worldCamera, out mousePos))
rectTransform.anchoredPosition = mousePos;
}
if (worldPositions.Count > 2)
{
polygon.SetVertices(worldPositions.ToArray());
Info.position = polygon.getCenterOfGravityPoint(worldPositions) + InfoOffset;
}
Camera.main.transform.position = nowcamerapos;
Camera.main.transform.rotation = nowrotate;
}
AddRecordAdd(addIndex, hitPos, MousePos);
}
public void AddRecordAdd(int addIndex, Vector3 hitPos, Vector3 MousePos)
{
if (ReplaySetting.PlayStatus == PlayStatus.isEditor && RecordManager.Instance.recordStatus == RecordStatus.normal)
{
var eventData = new EventData();
eventData.time = RecordManager.Instance.RecordTimer;
eventData.cloneObjType = CloneObjType.SetArea;
eventData.eventType = RecordEventType.SetAreaAdd;
var data = new RecordSetAreaAdd();
data.gameObjType = CloneObjType.SetArea;
data.objectName = gameObject.gameObject.name;
data.addIndex = addIndex;
data.hitPos = hitPos;
data.MousePos = MousePos;
data.myTransform.setMyPosition(Camera.main.transform.localPosition);
data.myTransform.setMyRotation(Camera.main.transform.localRotation);
string json = Newtonsoft.Json.JsonConvert.SerializeObject(data);
eventData.json = json;
RecordManager.Instance.jsonData.eventDataList.Add(eventData);
}
}
public void DelPolygon(int delIndex)
{
screenPositions.RemoveAt(delIndex);
worldPositions.RemoveAt(delIndex);
var obj = vertices[delIndex];
vertices.RemoveAt(delIndex);
Destroy(obj);
if (worldPositions.Count > 2)
{
polygon.SetVertices(worldPositions.ToArray());
Info.position = polygon.getCenterOfGravityPoint(worldPositions) + InfoOffset;
}
AddRecordDel(delIndex);
}
public void AddRecordDel(int delIndex)
{
if (ReplaySetting.PlayStatus == PlayStatus.isEditor && RecordManager.Instance.recordStatus == RecordStatus.normal)
{
var eventData = new EventData();
eventData.time = RecordManager.Instance.RecordTimer;
eventData.cloneObjType = CloneObjType.SetArea;
eventData.eventType = RecordEventType.SetAreaDel;
var data = new RecordSetAreaDel();
data.gameObjType = CloneObjType.SetArea;
data.objectName = gameObject.gameObject.name;
data.delIndex = delIndex;
string json = Newtonsoft.Json.JsonConvert.SerializeObject(data);
eventData.json = json;
RecordManager.Instance.jsonData.eventDataList.Add(eventData);
}
}
void Update()
{
RefreshUI();
}
float T = 0;
public int FindIndex(Vector3 position)
{
for (var i = 0; i < screenPositions.Count; ++i)
{
Vector2 left = screenPositions[i];
Vector2 right = position;
var distance = (left - right).sqrMagnitude;
if (distance <= 8 * 8)
return i;
}
return -1;
}
/// <summary>
/// 改变多边形顶点位置,按照屏幕坐标来计算。
/// </summary>
/// <returns>如果可以改变,返回 true;否则返回 false</returns>
public bool ChangePosition(int index, Vector3 position, bool record = false)
{
//Ray ray = Camera.main.ScreenPointToRay(position);
Plane plane = new Plane(Vector3.up, worldPositions[index]);
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
// AddRecordChangePosition(index, position, Input.mousePosition);
//RaycastHit hit;
//int ignoreRaycast = LayerMask.NameToLayer("Ignore Raycast");
//bool a = Physics.Raycast(ray, out hit, Mathf.Infinity, ~(1 << ignoreRaycast));
//if (!a)
// return false;
float enter;
if (plane.Raycast(ray, out enter))
{
var point = ray.GetPoint(enter);
screenPositions[index] = position;
worldPositions[index] = new Vector3(point.x, worldPositions[index].y, point.z);
if (worldPositions.Count > 2)
{
polygon.SetVertices(worldPositions.ToArray());
Info.position = polygon.getCenterOfGravityPoint(worldPositions) + InfoOffset;
}
if (record)
{
AddRecordChangePosition(index, position, point);
}
return true;
}
else
{
return false;
}
}
public void ReplayChangePosition(int index, Vector3 position, Vector3 mousePosition, RecordSetAreaChange arg)
{
screenPositions[index] = position;
worldPositions[index] = new Vector3(arg.point.x, worldPositions[index].y, arg.point.z);
if (worldPositions.Count > 2)
{
polygon.SetVertices(worldPositions.ToArray());
Info.position = polygon.getCenterOfGravityPoint(worldPositions) + InfoOffset;
}
}
/// <summary>
/// 添加拖拽记录
/// </summary>
/// <param name="index"></param>
/// <param name="position"></param>
public void AddRecordChangePosition(int index, Vector3 position,Vector3 point)
{
if (ReplaySetting.PlayStatus == PlayStatus.isEditor && RecordManager.Instance.recordStatus == RecordStatus.normal)
{
var eventData = new EventData();
eventData.time = RecordManager.Instance.RecordTimer;
eventData.cloneObjType = CloneObjType.SetArea;
eventData.eventType = RecordEventType.SetAreaChange;
var data = new RecordSetAreaChange();
data.gameObjType = CloneObjType.SetArea;
data.objectName = gameObject.gameObject.name;
data.index = index;
data.position = position;
data.point = point;
string json = Newtonsoft.Json.JsonConvert.SerializeObject(data);
eventData.json = json;
RecordManager.Instance.jsonData.eventDataList.Add(eventData);
}
}
private bool getRayCastAll(Ray ray)
{
bool rayCast = false;
//Ray ray = Camera.main.ScreenPointToRay(point);
RaycastHit[] hits;
//int notRendering = LayerMask.NameToLayer("NotRendering");
int ignoreRaycast = LayerMask.NameToLayer("Ignore Raycast");
hits = Physics.RaycastAll(ray, Mathf.Infinity, ~(1 << ignoreRaycast));
System.Array.Sort(hits, (r1, r2) => r1.distance.CompareTo(r2.distance));
for (var i = 0; i < hits.Length; i++)
{
var hitpoint = hits[i];
var renderer = hitpoint.collider.GetComponent<Renderer>();
if (renderer)
{
if (renderer.enabled)
{
rayCast = true;
hit = hitpoint;
//Debug.Log("----->"+hit.collider.name);
break;
}
}
else
{
var renderers = hitpoint.transform.GetComponentsInChildren<Renderer>();
for (int k = 0; k < renderers.Length; k++)
{
if (renderers[k].enabled)
{
rayCast = true;
hit = hitpoint;
//Debug.Log("----->"+hit.collider.name);
break;
}
}
if (rayCast)
{
break;
}
}
}
return rayCast;
}
/// <summary>
/// 更改多边形面片的高度。
/// </summary>
public void ChangeHeight(float height)
{
for (var i = 0; i < screenPositions.Count; ++i)
{
var worldPos = worldPositions[i] + new Vector3(0, height, 0);
var screenPos = Camera.main.WorldToScreenPoint(worldPos);
worldPositions[i] = worldPos;
screenPositions[i] = screenPos;
var rectTransform = vertices[i].transform as RectTransform;
if (Canvas.renderMode == RenderMode.ScreenSpaceOverlay)
{
Vector2 anchoredPos;
if (RectTransformUtility.ScreenPointToLocalPointInRectangle(PolyVerticeFather, screenPos, null, out anchoredPos))
rectTransform.anchoredPosition = anchoredPos;
}
else if (Canvas.renderMode == RenderMode.ScreenSpaceCamera)
{
Vector2 mousePos;
if (RectTransformUtility.ScreenPointToLocalPointInRectangle(PolyVerticeFather, screenPos, Canvas.worldCamera, out mousePos))
rectTransform.anchoredPosition = mousePos;
}
if (worldPositions.Count > 2)
polygon.SetVertices(worldPositions.ToArray());
}
Info.position = worldPositions[0] + new Vector3(0, 5, 0);
}
public void Destroy()
{
for (var i = 0; i < vertices.Count; ++i)
Destroy(vertices[i]);
Destroy(gameObject);
}
/// <summary>
/// 显示多边形面片。
/// </summary>
public void Show()
{
for (var i = 0; i < vertices.Count; ++i)
vertices[i].SetActive(true);
gameObject.SetActive(true);
}
/// <summary>
/// 隐藏多边形面片。
/// </summary>
public void Hide()
{
for (var i = 0; i < vertices.Count; ++i)
vertices[i].SetActive(false);
// gameObject.SetActive(false);
}
/// <summary>
/// 创建面片。
/// </summary>
public void CreatePolygon(List<Vector3> worldPositions, Color color, string Name, float offset = 1)
{
for (var i = 0; i < vertices.Count; ++i)
{
Destroy(vertices[i]);
}
this.worldPositions = worldPositions;
this.screenPositions.Clear();
this.vertices.Clear();
Info.gameObject.SetActive(true);
var s = Info.gameObject.AddComponent<CloneGameObjInfo>();
s.buildNum = GetComponent<CloneGameObjInfo>().buildNum;
s.gameObjType = GetComponent<CloneGameObjInfo>().gameObjType;
s.floorNum = GetComponent<CloneGameObjInfo>().floorNum;
s.interlayerNum = GetComponent<CloneGameObjInfo>().interlayerNum;
//s.deltaFloor = GetComponent<CloneGameObjInfo>().deltaFloor;
Info.Find("Name").GetComponent<TextMesh>().text = Name;
this.Color = color;
CreateUI();
if (worldPositions.Count > 2)
Info.position = polygon.getCenterOfGravityPoint(worldPositions) + InfoOffset + new Vector3(0, offset, 0); // Info.transform.position = worldPositions[0] + new Vector3(0, offset, 0);
polygon.SetVertices(worldPositions.ToArray());
}
public void DeleteUI()
{
for (var i = 0; i < vertices.Count; ++i)
{
Destroy(vertices[i]);
}
}
private void CreateUI()
{
for (var i = 0; i < worldPositions.Count; ++i)
{
var screenPos = Camera.main.WorldToScreenPoint(worldPositions[i]);
screenPositions.Add(screenPos);
var go = Instantiate(VerticePrefab);
var rectTransform = go.transform as RectTransform;
rectTransform.parent = PolyVerticeFather;
rectTransform.localScale = Vector3.one;
var script = go.GetComponent<DragVertice>();
script.PolygonController = this;
script.Canvas = Canvas;
vertices.Add(go);
if (Canvas.renderMode == RenderMode.ScreenSpaceOverlay)
{
Vector2 anchoredPos;
if (RectTransformUtility.ScreenPointToLocalPointInRectangle(PolyVerticeFather, screenPos, null, out anchoredPos))
rectTransform.anchoredPosition = anchoredPos;
}
else if (Canvas.renderMode == RenderMode.ScreenSpaceCamera)
{
Vector2 mousePos;
if (RectTransformUtility.ScreenPointToLocalPointInRectangle(PolyVerticeFather, screenPos, Canvas.worldCamera, out mousePos))
rectTransform.anchoredPosition = mousePos;
}
}
Hide();
}
private void RefreshUI()
{
for (var i = 0; i < worldPositions.Count; ++i)
{
var screenPos = Camera.main.WorldToScreenPoint(worldPositions[i]);
var currPos = screenPositions[i];
if (screenPos.x != currPos.x || screenPos.y != currPos.y)
{
screenPositions[i] = screenPos;
// 刷新 UI 位置
var rectTransform = vertices[i].transform as RectTransform;
if (Canvas.renderMode == RenderMode.ScreenSpaceOverlay)
{
Vector2 anchoredPos;
if (RectTransformUtility.ScreenPointToLocalPointInRectangle(PolyVerticeFather, screenPos, null, out anchoredPos))
rectTransform.anchoredPosition = anchoredPos;
}
else if (Canvas.renderMode == RenderMode.ScreenSpaceCamera)
{
Vector2 mousePos;
if (RectTransformUtility.ScreenPointToLocalPointInRectangle(PolyVerticeFather, screenPos, Canvas.worldCamera, out mousePos))
rectTransform.anchoredPosition = mousePos;
}
}
}
}
}