using UnityEngine; using Random = System.Random; public class TooltipAlign:MonoBehaviour { public Align align = Align.top; private GUIStyle tooltipStyle; private OnlineMapsTileSetControl control; private OnlineMaps map; private void Start() { map = OnlineMaps.instance; control = OnlineMapsTileSetControl.instance; map.showMarkerTooltip = OnlineMapsShowMarkerTooltip.always; tooltipStyle = new GUIStyle { normal = { background = map.tooltipBackgroundTexture, textColor = new Color32(230, 230, 230, 255) }, border = new RectOffset(8, 8, 8, 8), margin = new RectOffset(4, 4, 4, 4), wordWrap = true, richText = true, alignment = TextAnchor.MiddleCenter, stretchWidth = true, padding = new RectOffset(0, 0, 3, 3) }; OnlineMapsMarkerBase.OnMarkerDrawTooltip += OnMarkerDrawTooltip; double tlx, tly, brx, bry; map.GetCorners(out tlx, out tly, out brx, out bry); Random rnd = new Random(); for (int i = 0; i < 20; i++) { double lng = rnd.NextDouble() * (brx - tlx) + tlx; double lat = rnd.NextDouble() * (tly - bry) + bry; map.AddMarker(lng, lat, i.ToString()); } } private void OnMarkerDrawTooltip(OnlineMapsMarkerBase markerBase) { OnlineMapsMarker marker = markerBase as OnlineMapsMarker; if (marker == null) return; double tlx, tly, brx, bry; map.GetCorners(out tlx, out tly, out brx, out bry); double mx, my; marker.GetPosition(out mx, out my); Vector3 p1 = control.GetWorldPositionWithElevation(mx, my, tlx, tly, brx, bry); Vector3 p2 = p1 + new Vector3(0, 0, map.tilesetSize.y / map.tilesetHeight * marker.height * marker.scale); Vector2 screenPoint1 = control.activeCamera.WorldToScreenPoint(p1); Vector2 screenPoint2 = control.activeCamera.WorldToScreenPoint(p2); float yOffset = (screenPoint1.y - screenPoint2.y) * transform.localScale.x - 10; Vector3 position = screenPoint1 + new Vector2(0, yOffset); GUIContent tip = new GUIContent(marker.label); Vector2 size = tooltipStyle.CalcSize(tip); Rect rect = default(Rect); if (align == Align.top) rect = new Rect(position.x - size.x / 2 - 5, Screen.height - position.y - size.y - 20, size.x + 10, size.y + 5); else if (align == Align.bottom) rect = new Rect(position.x - size.x / 2 - 5, Screen.height - position.y + size.y - 10, size.x + 10, size.y + 5); else if (align == Align.left) rect = new Rect(position.x - size.x - 20, Screen.height - position.y - size.y / 2 - 5, size.x + 10, size.y + 5); else rect = new Rect(position.x + size.x, Screen.height - position.y - size.y / 2 - 5, size.x + 10, size.y + 5); GUI.Label(rect, marker.label, tooltipStyle); } public enum Align { top, left, right, bottom } }