上海虹口龙之梦项目
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.
 
 
 
 

267 lines
10 KiB

using System;
using UniRx;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using System.Threading.Tasks;
public class DriveRoute : MonoBehaviour
{
private static DriveRoute m_Instance;
public static DriveRoute Instance
{
get
{
if (m_Instance == null)
{
m_Instance = new GameObject(typeof(DriveRoute).ToString(), typeof(DriveRoute)).GetComponent<DriveRoute>();
m_Instance.Init();
}
return m_Instance;
}
}
public CreationMode SourceMode;
public GameObject SourceMap;
public Transform Parent;
public LayerMask layerMask = -1;
public bool isCreate;
public bool isArrow = false;
private Vector3 posA;//第一碰撞点
private Vector3 posB;//第二碰撞点
private GameObject ArrowGroup;
public string Original;
public GameObject Selected;
private List<GameObject> Sourcelist = new List<GameObject>();
public DriveRouteData Data = new DriveRouteData();
private Camera dragCamera;
private void Init()
{
AssetManager.Instance.SetLoadingPanel(true);
gameObject.layer = LayerMask.NameToLayer("Annotation");
transform.position = new Vector3(3000, 0, 0);
SourceMap = GameObject.CreatePrimitive(PrimitiveType.Plane);
SourceMap.GetComponent<MeshRenderer>().material = new Material(Shader.Find("Unlit/Texture"));
SourceMap.name = "Map";
SourceMap.transform.parent = transform;
SourceMap.transform.localPosition = Vector3.zero;
SourceMap.transform.localScale = new Vector3(20, 1, 12);
SourceMap.gameObject.layer = LayerMask.NameToLayer("Annotation");
Parent = new GameObject("Marks").transform;
Parent.parent = transform;
Parent.transform.localPosition = Vector3.zero;
Parent.gameObject.layer = LayerMask.NameToLayer("Annotation");
dragCamera = new GameObject("DragCamera").AddComponent<Camera>();
dragCamera.transform.localPosition = new Vector3(3000, 100, 0);
dragCamera.transform.localEulerAngles = new Vector3(90, -180, 0);
dragCamera.tag = "MainCamera";
dragCamera.transform.parent = transform;
dragCamera.gameObject.layer = LayerMask.NameToLayer("Annotation");
dragCamera.clearFlags = CameraClearFlags.SolidColor;
dragCamera.backgroundColor = Color.gray;
dragCamera.cullingMask = 1 << 13;
dragCamera.orthographic = true;
dragCamera.orthographicSize = 90f;
dragCamera.nearClipPlane = 0.3f;
dragCamera.farClipPlane = 200f;
dragCamera.depth = -1;
var cam = dragCamera.gameObject.AddComponent<DragCamera2D>();
cam.cam = dragCamera;
cam.panningEnabled = true;
cam.panSpeed = -0.06f;
cam.keyboardInput = false;
cam.inverseKeyboard = false;
cam.zoomEnabled = true;
cam.linkedZoomDrag = true;
cam.maxZoom = 200;
cam.minZoom = 10;
cam.zoomStepSize = 5;
AssetManager.Instance.SetLoadingPanel(false);
}
// Start is called before the first frame update
void Start()
{
GetData();
Observable.EveryUpdate()
.Subscribe(_ => CloneObjectsAsync()).AddTo(gameObject);
Observable.EveryLateUpdate()
.Subscribe(_ =>
{
if (Input.GetKeyDown(KeyCode.Delete))
if (Selected != null)
{
if (Selected.GetComponent<SourceController>())
Sourcelist.Remove(Selected);
else
{
foreach (Transform t in Selected.transform)
Sourcelist.Remove(t.gameObject);
}
Destroy(Selected);
}
if (Input.GetMouseButtonDown(1))
Selected = null;
}).AddTo(gameObject);
}
private async void CloneObjectsAsync()
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out RaycastHit hit, Mathf.Infinity, layerMask))
{
if (!EventSystem.current.IsPointerOverGameObject())
{
switch (SourceMode)
{
case CreationMode.Single:
if (Input.GetMouseButtonDown(0) && isCreate)
{
posA = new Vector3(hit.point.x, hit.point.y, hit.point.z);
var go = await AnnotationPool.Instance.GetMarked(Original, posA, Quaternion.identity, Parent);
go.name = $"{Original}{Guid.NewGuid()}";
Selected = go;
Sourcelist.Add(go);
}
break;
case CreationMode.Multipoint:
if (isCreate)
{
if (Input.GetMouseButtonDown(0))
{
if (!isArrow)
{
posA = new Vector3(hit.point.x, hit.point.y, hit.point.z);
ArrowGroup = GameObject.CreatePrimitive(PrimitiveType.Cube);
ArrowGroup.name = $"{"ArrowGroup"}{Guid.NewGuid()}";
Destroy(ArrowGroup.GetComponent<BoxCollider>());
Destroy(ArrowGroup.GetComponent<MeshRenderer>());
ArrowGroup.transform.parent = Parent;
ArrowGroup.transform.localPosition = new Vector3(0, 0, 0);
isArrow = true;
Selected = ArrowGroup;
}
else
{
Vector3 vertPos;
posB = new Vector3(hit.point.x, hit.point.y, hit.point.z);
var ABdistance = Vector3.Distance(posA, posB);
int number = Mathf.FloorToInt(ABdistance / 3);
if (number < 1) number = 1;
for (int i = 0; i < number; i++)
{
vertPos = new Vector3(posA.x + (i / (float)number) * (posB.x - posA.x), posA.y + (i / (float)number) * (posB.y - posA.y), posA.z + (i / (float)number) * (posB.z - posA.z));
//GameObject go = Instantiate(Original, vertPos, Quaternion.identity) as GameObject;
var go = await AnnotationPool.Instance.GetMarked(Original, vertPos, Quaternion.identity, ArrowGroup.transform);
go.name = $"{Original}{Guid.NewGuid()}";
Sourcelist.Add(go);
go.transform.forward = (-(posB - posA)).normalized;
}
posA = posB;
}
}
}
break;
}
}
}
}
//保存数据
public void SaveData()
{
Data.Sources = new List<RouteData>();
foreach(GameObject go in Sourcelist)
{
var rd = new RouteData()
{
OriginaName = go.GetComponent<SourceController>().OriginaName,
Id=go.name,
Position = go.transform.position,
Rotation = go.transform.rotation.eulerAngles,
Info = go.transform.Find("info").GetComponent<TextMesh>().text,
Parent = go.transform.parent.name
};
Data.Sources.Add(rd);
}
HttpManager.Instance.Post(HttpManager.Instance.PostDriveRoutes, Data);
}
private void GetData()
{
var url = HttpManager.Instance.GetDriveRoutes;
HttpManager.Instance.Get<DriveRouteData>(url, data =>
{
Data = data;
GetImage();
SpawnObjectWithData();
});
}
private async void SpawnObjectWithData()
{
foreach (var item in Data.Sources)
{
var go = await AnnotationPool.Instance.GetMarked(item.OriginaName, item.Position, Quaternion.Euler(item.Rotation), Parent);
go.name = item.Id;
go.transform.Find("info").GetComponent<TextMesh>().text = item.Info;
Sourcelist.Add(go);
if (item.Id.Contains("LX"))
{
string parents = item.Parent;
if (Parent.Find(parents) == null)
{
var p = new GameObject(parents);
p.transform.parent = Parent;
p.transform.localPosition = new Vector3(0, 0, 0);
}
go.transform.parent = Parent.Find(parents);
}
}
}
public void OnClearData()
{
MessageBox.Show("确定清除?", Color.white, ClearData);
}
void ClearData()
{
Sourcelist.Clear();
foreach (Transform t in Parent)
Destroy(t.gameObject);
}
/// <summary>
/// 获取图片
/// </summary>
void GetImage()
{
if (!string.IsNullOrEmpty(Data.ImageUrl))
{
HttpManager.Instance.GetImage($"{Data.ImageUrl}", texture =>
{
SourceMap.GetComponent<Renderer>().material.SetTexture("_MainTex", texture);
SourceMap.transform.localScale = new Vector3(20, 1, (float)texture.height * ((float)20 / (float)texture.width));
});
}
}
public void Show()
{
m_Instance.gameObject.SetActive(true);
}
public void Hide()
{
m_Instance.gameObject.SetActive(false);
}
}