/* INFINITY CODE 2013-2016 */ /* http://www.infinity-code.com */ #if !UNITY_4_3 && !UNITY_4_5 using System.Collections.Generic; using UnityEngine; #if UNITY_EDITOR using UnityEditor; #endif namespace InfinityCode.OnlineMapsExamples { [AddComponentMenu("Infinity Code/Online Maps/Examples (API Usage)/uGUICustomMarkerEngineExample")] public class uGUICustomMarkerEngineExample : MonoBehaviour { private static uGUICustomMarkerEngineExample _instance; private static List markers; public RectTransform markerContainer; public GameObject markerPrefab; private GameObject container; private bool needUpdateMarkers; public static uGUICustomMarkerEngineExample instance { get { return _instance; } } public static uGUICustomMarkerExample AddMarker(Vector2 position, string text) { return AddMarker(position.x, position.y, text); } public static uGUICustomMarkerExample AddMarker(double lng, double lat, string text) { GameObject markerGameObject = Instantiate(_instance.markerPrefab) as GameObject; (markerGameObject.transform as RectTransform).SetParent(_instance.markerContainer); uGUICustomMarkerExample marker = markerGameObject.GetComponent(); marker.text = text; marker.lng = lng; marker.lat = lat; markers.Add(marker); _instance.UpdateMarker(marker); return marker; } private void OnEnable() { _instance = this; markers = new List(); } public static void RemoveAllMarkers() { foreach (uGUICustomMarkerExample marker in markers) { marker.Dispose(); DestroyImmediate(marker.gameObject); } markers.Clear(); } public static void RemoveMarker(uGUICustomMarkerExample marker) { DestroyImmediate(marker.gameObject); marker.Dispose(); markers.Remove(marker); } public static void RemoveMarkerAt(int index) { if (index < 0 || index >= markers.Count) return; uGUICustomMarkerExample marker = markers[index]; DestroyImmediate(marker.gameObject); marker.Dispose(); markers.RemoveAt(index); } private void Start () { OnlineMaps.instance.OnMapUpdated += StartUpdateMarkers; if (OnlineMapsControlBase.instance is OnlineMapsControlBase3D) OnlineMapsControlBase3D.instance.OnCameraControl += StartUpdateMarkers; AddMarker(new Vector2(), "12"); } private void StartUpdateMarkers() { needUpdateMarkers = true; #if UNITY_EDITOR if (EditorApplication.isPaused) UpdateMarkers(); #endif } private void Update() { if (needUpdateMarkers) { UpdateMarkers(); needUpdateMarkers = false; } } private void UpdateMarkers() { foreach (uGUICustomMarkerExample marker in markers) UpdateMarker(marker); } private void UpdateMarker(uGUICustomMarkerExample marker) { OnlineMaps api = OnlineMaps.instance; double tlx, tly, brx, bry; api.GetTopLeftPosition(out tlx, out tly); api.GetBottomRightPosition(out brx, out bry); double px = marker.lng; double py = marker.lat; if (px < tlx || px > brx || py < bry || py > tly) { marker.gameObject.SetActive(false); return; } Vector2 screenPosition = OnlineMapsControlBase.instance.GetScreenPosition(px, py); RectTransform markerRectTransform = marker.transform as RectTransform; if (!marker.gameObject.activeSelf) marker.gameObject.SetActive(true); screenPosition.y += markerRectTransform.rect.height / 2; Vector2 point; RectTransformUtility.ScreenPointToLocalPointInRectangle(markerRectTransform.parent as RectTransform, screenPosition, null, out point); markerRectTransform.localPosition = point; } } } #endif