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.
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.
Hey There!,
Yes you can do that at runtime.
By setting the 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
using System.Collections;
using Unity.MARS.Conditions;
using Unity.MARS.Data;
using Unity.MARS.Providers;
using UnityEngine;
namespace Unity.MARS
{
public class ScriptedGeoFenceTest : MonoBehaviour, IUsesGeoLocation
{
#pragma warning disable 649
[SerializeField]
GeoFenceCondition m_FenceCondition;
#pragma warning restore 649
IEnumerator Start()
{
yield return ScriptedWaits.TwentySeconds;
Debug.Log("Changing fence rule to \"Outside\"");
m_FenceCondition.Rule = GeoFenceCondition.GeoFenceRule.Outside;
Debug.Log("Increasing bounds of geo fence");
var oldBounds = m_FenceCondition.BoundingBox;
m_FenceCondition.BoundingBox = new GeographicBoundingBox()
{
center = oldBounds.center,
latitudeExtents = oldBounds.latitudeExtents * 3.0,
longitudeExtents = oldBounds.longitudeExtents * 3.0,
};
m_FenceCondition.proxy.SyncModifications();
yield return ScriptedWaits.TwentySeconds;
Debug.Log("Changing fence rule to \"Inside\"");
m_FenceCondition.Rule = GeoFenceCondition.GeoFenceRule.Inside;
m_FenceCondition.proxy.SyncModifications();
yield return ScriptedWaits.TwentySeconds;
Debug.Log($"Restoring old bounds");
m_FenceCondition.BoundingBox = oldBounds;
m_FenceCondition.proxy.SyncModifications();
}
}
}
Just make sure to call SyncModifications()
after making the change so new values get applied. Hope it helps and happy coding
Thank you!