I've spent the last several days trying to figure out how to properly place NGUI objects as markers on top of an Infinity Code Online Map implementation, but am running into multiple issues with proper positioning on the screen. I'm pretty sure I've narrowed it down to an issue with coordinate spaces, but I'm stuck at this point.
The Online Maps implementation basically has two spaces: geographical space (longitude/latitude), and screen space, with helpers to convert from geo-space to screen space, with the expectation that the helper provides a location to place a widget's render location with widget.SetRect...unfortunately, the marker doesn't actually render where the map thinks it should.
My scene has one camera with both Camera and UI Camera components, and my map is in it's own Panel. At run time, my script instantiates a marker set (basic UIWidget with a few child sprites) from an object in the hierarchy for each of the markers in the list with the following code:
GameObject go = NGUITools.AddChild(instance.container.gameObject,instance.cityPrefab);
go.transform.localScale = Vector3.one;
go.transform.localPosition = Vector3.one;
and then sets the coordinates based on the long/lat of where the marker should be. (All of this works fine). Note that "instance.container" is the actual Panel that contains the map.
The problem comes when the markers want to update their position based on a map change (scroll, zoom changes), which has to update the position of the markers. The current (not working properly) code is:
foreach (CityMarker marker in cityMarkers)
{
Vector2 p = marker.coords;
GameObject go = marker.gameObject;
if (!go.activeSelf) go.SetActive(true);
Vector2 screenPosition = OnlineMapsControlBase.instance.GetScreenPosition(p);
screenPosition.x -= Screen.width / 2;
screenPosition.y -= Screen.height / 2;
Vector2 buttonOffset
= new Vector2
(-marker
.size.x / 2,
0); marker.widget.SetRect(screenPosition.x + buttonOffset.x, screenPosition.y + buttonOffset.y, marker.size.x, marker.size.y);
}
GetScreenPosition is the primary helper I have from the Online Maps implementation, and I don't have any helpers that are designed to return world space.
My root question is what do I need to do with the NGUI UIWidget to get it to draw at the screen position indicated by the GetScreenPosition helper? I'm sure there are coordinate space and camera relationship issues that the simple widget.setRect call isn't handling properly, but after 2 days of working this I'm totally lost as to how NGUI wants me to place the UIWIdget.