Search Unity

Help Wanted Changing GeoFence lat and long at runtime

Discussion in 'Unity MARS' started by eslicenses, Sep 2, 2021.

  1. eslicenses

    eslicenses

    Joined:
    Oct 10, 2018
    Posts:
    8
    Hi All,

    can we change or set the latitude and longitude of Geofence condition in UnityMars at runtime by C#.

    Any example and suggestion, will really help.

    Thank you.
     
  2. jmunozarUTech

    jmunozarUTech

    Unity Technologies

    Joined:
    Jun 1, 2020
    Posts:
    265
    Hey There!,

    Yes you can do that at runtime.

    By setting the
    Code (csharp):
    1. BoundingBox
    property you can achieve what you are looking for. Checkout this sample code that changes the bounds, sets the rules from inside to outside and finally restores the old bounds

    Code (CSharp):
    1. using System.Collections;
    2. using Unity.MARS.Conditions;
    3. using Unity.MARS.Data;
    4. using Unity.MARS.Providers;
    5. using UnityEngine;
    6.  
    7. namespace Unity.MARS
    8. {
    9.     public class ScriptedGeoFenceTest : MonoBehaviour, IUsesGeoLocation
    10.     {
    11. #pragma warning disable 649
    12.         [SerializeField]
    13.         GeoFenceCondition m_FenceCondition;
    14. #pragma warning restore 649
    15.  
    16.         IEnumerator Start()
    17.         {
    18.             yield return ScriptedWaits.TwentySeconds;
    19.  
    20.             Debug.Log("Changing fence rule to \"Outside\"");
    21.             m_FenceCondition.Rule = GeoFenceCondition.GeoFenceRule.Outside;
    22.  
    23.             Debug.Log("Increasing bounds of geo fence");
    24.  
    25.             var oldBounds = m_FenceCondition.BoundingBox;
    26.  
    27.             m_FenceCondition.BoundingBox = new GeographicBoundingBox()
    28.             {
    29.                 center = oldBounds.center,
    30.                 latitudeExtents = oldBounds.latitudeExtents * 3.0,
    31.                 longitudeExtents = oldBounds.longitudeExtents * 3.0,
    32.             };
    33.  
    34.             m_FenceCondition.proxy.SyncModifications();
    35.  
    36.             yield return ScriptedWaits.TwentySeconds;
    37.  
    38.             Debug.Log("Changing fence rule to \"Inside\"");
    39.  
    40.             m_FenceCondition.Rule = GeoFenceCondition.GeoFenceRule.Inside;
    41.             m_FenceCondition.proxy.SyncModifications();
    42.  
    43.             yield return ScriptedWaits.TwentySeconds;
    44.  
    45.             Debug.Log($"Restoring old bounds");
    46.             m_FenceCondition.BoundingBox = oldBounds;
    47.             m_FenceCondition.proxy.SyncModifications();
    48.         }
    49.     }
    50. }
    Just make sure to call
    Code (csharp):
    1. SyncModifications()
    after making the change so new values get applied. Hope it helps and happy coding :)
     
  3. eslicenses

    eslicenses

    Joined:
    Oct 10, 2018
    Posts:
    8
    Thank you! :)
     
unityunity