<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
	<channel>
		<title><![CDATA[Infinity Code Forum — UI Marker and gameobject]]></title>
		<link>https://forum.infinity-code.com/viewtopic.php?id=1602</link>
		<atom:link href="https://forum.infinity-code.com/extern.php?action=feed&amp;tid=1602&amp;type=rss" rel="self" type="application/rss+xml" />
		<description><![CDATA[The most recent posts in UI Marker and gameobject.]]></description>
		<lastBuildDate>Tue, 15 Sep 2020 12:19:33 +0000</lastBuildDate>
		<generator>PunBB</generator>
		<item>
			<title><![CDATA[Re: UI Marker and gameobject]]></title>
			<link>https://forum.infinity-code.com/viewtopic.php?pid=6665#p6665</link>
			<description><![CDATA[<p>Makes sense. Thanks for the explanations. </p><p>For ref, below is the updated code if it is of interest to someone:</p><div class="codebox"><pre><code>using UnityEngine;
using UnityEngine.UI;

public class TestMapItem : MonoBehaviour
{
    public float Longitude { get; set; }
    public float Latitude { get; set; }
    private bool PositionToUpdate = false;


    public void Start()
    {
        OnlineMaps.instance.OnChangePosition += this.FlagUpdatePosition;
        OnlineMaps.instance.OnChangeZoom += this.FlagUpdatePosition;
        OnlineMaps.instance.OnMapUpdated += this.UpdatePosition;
        this.GetComponent&lt;Button&gt;().onClick.AddListener(this.HandleButtonClicked);
    }

    private void FlagUpdatePosition()
    {
        this.PositionToUpdate = true;
    }

    private void UpdatePosition()
    {
        if (this.PositionToUpdate)
        {
            float longitude = this.Longitude;
            float latitude = this.Latitude;
            // object is enabled only if it is visible
            this.gameObject.SetActive(OnlineMaps.instance.InMapView(longitude, latitude));
            // works for uGui controller; if you want to use tileset controller, you may use OnlineMapsControlBaseDynamicMesh.instance.GetWorldPosition
            this.gameObject.transform.position = OnlineMapsControlBase.instance.GetScreenPosition(longitude, latitude);
            this.PositionToUpdate = false;
        }
    }

    private void HandleButtonClicked()
    {
        // for test purpose
        Debug.Log(&quot;test click&quot;);
    }

    public void Destroy()
    {
        OnlineMaps.instance.OnChangePosition -= this.FlagUpdatePosition;
        OnlineMaps.instance.OnChangeZoom -= this.FlagUpdatePosition;
        OnlineMaps.instance.OnMapUpdated -= this.UpdatePosition;
        GameObject.Destroy(this.gameObject);
    }

    public void Activate(float longitude, float latitude)
    {
        this.Longitude = longitude;
        this.Latitude = latitude;
        this.PositionToUpdate = true;
        this.UpdatePosition();
    }

    public static TestMapItem Create(float longitude, float latitude, Transform root, GameObject prefab)
    {
        GameObject obj = Instantiate(prefab, root, false);
        TestMapItem result = obj.GetComponent&lt;TestMapItem&gt;();
        result.Activate(longitude, latitude);
        return result;
    }
}</code></pre></div>]]></description>
			<author><![CDATA[null@example.com (emmanuel.g.blanchard)]]></author>
			<pubDate>Tue, 15 Sep 2020 12:19:33 +0000</pubDate>
			<guid>https://forum.infinity-code.com/viewtopic.php?pid=6665#p6665</guid>
		</item>
		<item>
			<title><![CDATA[Re: UI Marker and gameobject]]></title>
			<link>https://forum.infinity-code.com/viewtopic.php?pid=6664#p6664</link>
			<description><![CDATA[<p>The map can be redrawn not only when the position or zoom has changed.<br />Redrawing also occurs when tiles are loaded, marker positions are changed, drawing elements are added, etc.<br />By listening to OnChangePosition and OnChangeZoom, you know for sure that the positions of your objects are out of date, and you need to update this.</p>]]></description>
			<author><![CDATA[null@example.com (Alex Vertax)]]></author>
			<pubDate>Tue, 15 Sep 2020 11:04:21 +0000</pubDate>
			<guid>https://forum.infinity-code.com/viewtopic.php?pid=6664#p6664</guid>
		</item>
		<item>
			<title><![CDATA[Re: UI Marker and gameobject]]></title>
			<link>https://forum.infinity-code.com/viewtopic.php?pid=6663#p6663</link>
			<description><![CDATA[<p>This fixed the issue. Thanks :-)</p><p>But actually, if one use OnMapUpdated... OnChangePosition and OnChangeZoom do not seem to be needed at all?&nbsp; I commented them and the result is the same. Or does it happen frequently that map is redrawn without being moved/zoomed? Just to get a better understanding of the onlineMaps process.</p>]]></description>
			<author><![CDATA[null@example.com (emmanuel.g.blanchard)]]></author>
			<pubDate>Tue, 15 Sep 2020 10:36:47 +0000</pubDate>
			<guid>https://forum.infinity-code.com/viewtopic.php?pid=6663#p6663</guid>
		</item>
		<item>
			<title><![CDATA[Re: UI Marker and gameobject]]></title>
			<link>https://forum.infinity-code.com/viewtopic.php?pid=6662#p6662</link>
			<description><![CDATA[<p>This is because GetScreenPosition returns values for the current (displayed) state of the map.<br />The map is not redrawn immediately, and no one knows how many frames it will take to render the new map state.</p><p>How to work around this problem:<br />1. When calling OnChangePosition and OnChangeZoom, just set the flag, that you need to update the position.<br />2. Subscribe to OnlineMaps.OnMapUpdated and when this action is called update the position and clear the flag.</p>]]></description>
			<author><![CDATA[null@example.com (Alex Vertax)]]></author>
			<pubDate>Mon, 14 Sep 2020 22:11:43 +0000</pubDate>
			<guid>https://forum.infinity-code.com/viewtopic.php?pid=6662#p6662</guid>
		</item>
		<item>
			<title><![CDATA[Re: UI Marker and gameobject]]></title>
			<link>https://forum.infinity-code.com/viewtopic.php?pid=6661#p6661</link>
			<description><![CDATA[<p>thanks for the input. </p><p>One quick note, when dealing with events (zoom especially, but movements as well), the result of OnlineMapsControlBase.instance.GetScreenPosition frequently ends a bit misplaced within the map. That s easy to notice if you refer to a precise and known location (e.g. your house). If you have a big zoom value, it s a question of meters, but if you have smaller ones, it could mean hundreds of kms.</p><p>Looks to me what could explain this temporary lack of precision is the latest event not being caught (hence not triggering position update) because as soon as you slightly move again, it repositions correctly.</p>]]></description>
			<author><![CDATA[null@example.com (emmanuel.g.blanchard)]]></author>
			<pubDate>Mon, 14 Sep 2020 21:59:28 +0000</pubDate>
			<guid>https://forum.infinity-code.com/viewtopic.php?pid=6661#p6661</guid>
		</item>
		<item>
			<title><![CDATA[Re: UI Marker and gameobject]]></title>
			<link>https://forum.infinity-code.com/viewtopic.php?pid=6660#p6660</link>
			<description><![CDATA[<p>One small thing about your script:<br />We do not recommend using singletons in Awake or OnEnable methods, because the singletons may not have been initialized yet. <br />Use singletons in Start method or later.</p>]]></description>
			<author><![CDATA[null@example.com (Alex Vertax)]]></author>
			<pubDate>Mon, 14 Sep 2020 20:07:20 +0000</pubDate>
			<guid>https://forum.infinity-code.com/viewtopic.php?pid=6660#p6660</guid>
		</item>
		<item>
			<title><![CDATA[Re: UI Marker and gameobject]]></title>
			<link>https://forum.infinity-code.com/viewtopic.php?pid=6657#p6657</link>
			<description><![CDATA[<p>Ok. Indeed using the other forum thread, it was super- easy unless there are performance issues i haven&#039;t noticed. Putting a code example here in case someone need it. My prefab also has a button script for test</p><div class="codebox"><pre><code>public class TestMapItem : MonoBehaviour
{
    public float Longitude {get;set;}
    public float Latitude {get;set;}
        
    void Awake()
    {
        OnlineMaps.instance.OnChangePosition += UpdatePosition;
        OnlineMaps.instance.OnChangeZoom += UpdatePosition;
        this.GetComponent&lt;Button&gt;().onClick.AddListener(this.HandleButtonClicked);    
    }

    public void UpdatePosition()
    {
        // the object is disabled when not in the visible map
        this.gameObject.SetActive(OnlineMaps.instance.InMapView(Longitude, Latitude));
        // the following line works with uGui controller
        this.gameObject.transform.position = OnlineMapsControlBase.instance.GetScreenPosition(Longitude, Latitude);
        // if you want to use tileset controller, you may use OnlineMapsControlBaseDynamicMesh.instance.GetWorldPosition
    }
    public void HandleButtonClicked()
    {
        Debug.Log(&quot;test click&quot;);
    }

    public void Activate(float longitude, float latitude)
    {
        this.Longitude = longitude;
        this.Latitude = latitude;
        this.UpdatePosition();
    }

    // to instantiate the prefab i.e. pass it as the parameter; root is where the object is placed. i used the map transform
    // the script is attached to the prefab itself... but could be added with SetComponent instead.
    public static TestMapItem Create(float longitude, float latitude, Transform root, GameObject prefab)
    {
        GameObject obj = Instantiate(prefab, root, false);
        TestMapItem result = obj.GetComponent&lt;TestMapItem&gt;();
        result.Activate(longitude, latitude);
        return result;
    }
}</code></pre></div>]]></description>
			<author><![CDATA[null@example.com (emmanuel.g.blanchard)]]></author>
			<pubDate>Mon, 14 Sep 2020 16:08:57 +0000</pubDate>
			<guid>https://forum.infinity-code.com/viewtopic.php?pid=6657#p6657</guid>
		</item>
		<item>
			<title><![CDATA[Re: UI Marker and gameobject]]></title>
			<link>https://forum.infinity-code.com/viewtopic.php?pid=6655#p6655</link>
			<description><![CDATA[<p>Thanks for the quick reply. <br />Didn t notice the destroy stuff before you mentioned it to me, and i need objects to remain alive (and rather play with their enabled/disabled property)<br />Then i guess i will probably go with something close to this thread, along with ui objects : <a href="https://forum.infinity-code.com/viewtopic.php?id=1571">https://forum.infinity-code.com/viewtopic.php?id=1571</a><br />Looks like a quick and easy approach. Makes sense to you?</p>]]></description>
			<author><![CDATA[null@example.com (emmanuel.g.blanchard)]]></author>
			<pubDate>Mon, 14 Sep 2020 14:55:42 +0000</pubDate>
			<guid>https://forum.infinity-code.com/viewtopic.php?pid=6655#p6655</guid>
		</item>
		<item>
			<title><![CDATA[Re: UI Marker and gameobject]]></title>
			<link>https://forum.infinity-code.com/viewtopic.php?pid=6654#p6654</link>
			<description><![CDATA[<p>Hello.</p><p>In the uGUIMarkerDrawer example, a reference to the instance is stored inside the marker and you can get it from anywhere.<br />But keep in mind that the instance is destroyed when the marker goes out of the map.<br /></p><div class="codebox"><pre><code>GameObject markerInstance = marker [&quot;instance&quot;] as GameObject;</code></pre></div>]]></description>
			<author><![CDATA[null@example.com (Alex Vertax)]]></author>
			<pubDate>Mon, 14 Sep 2020 14:14:01 +0000</pubDate>
			<guid>https://forum.infinity-code.com/viewtopic.php?pid=6654#p6654</guid>
		</item>
		<item>
			<title><![CDATA[UI Marker and gameobject]]></title>
			<link>https://forum.infinity-code.com/viewtopic.php?pid=6653#p6653</link>
			<description><![CDATA[<p>Hello, <br />Quick question before i refactor things, to check if there is an existing option to my issue.<br />- I m having a prefab to instantiate in order to replace classic texture marker, and i use the uGUIMarkerDrawer example to do so. <br />- I would need to access a reference of the gameobject instance created from the prefab, but in UGuiMarker Drawer, it is shown as an inner variable of some function. <br />Any chance a function already exists to retrieve the corresponding gameobject ref from the corresponding onlineMapsMarker created with this call : OnlineMapsMarker marker = OnlineMapsMarkerManager.CreateItem(coords, unit.Title);<br />?</p><p>Thanks a lot.</p>]]></description>
			<author><![CDATA[null@example.com (emmanuel.g.blanchard)]]></author>
			<pubDate>Mon, 14 Sep 2020 13:56:31 +0000</pubDate>
			<guid>https://forum.infinity-code.com/viewtopic.php?pid=6653#p6653</guid>
		</item>
	</channel>
</rss>
