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

197 lines
6.1 KiB

using AX.Network.Common;
using System.Collections.Generic;
using UniRx;
using UnityEngine;
using UnityEngine.EventSystems;
public class WaterSource : MonoBehaviour
{
private static WaterSource m_Instance;
public static WaterSource Instance
{
get
{
if (m_Instance == null)
{
m_Instance = new GameObject(typeof(WaterSource).ToString(), typeof(WaterSource)).GetComponent<WaterSource>();
m_Instance.Init();
}
return m_Instance;
}
}
public GameObject SourceMap;
public Transform Parent;
public LayerMask layerMask = -1;
public bool isCreate;
public string Original;
public GameObject Selected;
private Camera dragCamera;
public WaterSourceData Data = new WaterSourceData() ;
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);
}
private void Start()
{
GetData();
Observable.EveryUpdate()
.Subscribe(_=>CloneObjects()).AddTo(gameObject);
Observable.EveryLateUpdate()
.Subscribe(_ =>
{
if (Input.GetKeyDown(KeyCode.Delete))
if (Selected != null)
Destroy(Selected);
if (Input.GetMouseButtonDown(1))
Selected = null;
}).AddTo(gameObject);
}
private async void CloneObjects()
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out RaycastHit hit, Mathf.Infinity, layerMask))
{
if (!EventSystem.current.IsPointerOverGameObject())
{
if (Input.GetMouseButtonDown(0) && isCreate)
{
Vector3 position = new Vector3(hit.point.x, hit.point.y, hit.point.z);
var go = await AnnotationPool.Instance.GetMarked(Original, position, Quaternion.identity, Parent);
go.name = $"{Original}{GUID.NewGuid()}";
go.transform.parent = Parent;
Selected = go;
}
}
}
}
public void OnClearData()
{
MessageBox.Show("确定清除?", Color.white, ClearData);
}
void ClearData()
{
foreach (Transform t in Parent)
Destroy(t.gameObject);
}
//保存数据
public void SaveData()
{
Data.Sources = new List<SourceData>();
foreach (Transform t in Parent)
{
var sd = new SourceData
{
OriginaName = t.GetComponent<SourceController>().OriginaName,
Id = t.gameObject.name,
Position = t.position,
Info = t.Find("info").GetComponent<TextMesh>().text
};
Data.Sources.Add(sd);
}
HttpManager.Instance.Post(HttpManager.Instance.PostWaterSources, Data);
}
//加载数据
public void GetData()
{
var url = HttpManager.Instance.GetWaterSources;
HttpManager.Instance.Get<WaterSourceData>(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.identity, Parent);
go.name = item.Id;
go.transform.Find("info").GetComponent<TextMesh>().text = item.Info;
}
}
/// <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);
}
}