[Huawei Developers] Freemind R:
More information like this, you can visit HUAWEI Developer Forum​

Original link: https://forums.developer.huawei.com/forumPortal/en/topicview?tid=0201350575696000176&fid=0101187876626530001

Introduction:

In this article, we will cover Integration of Location Kit in Unity Project using Official Plugin (Huawei HMS Core App Services).

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/002/032/901/2640091000002032901.20200909203742.08198694138583802744398746951058:50510923021047:2800:0FF6944CFEAB184BD0751CFDB26AA87957A2CB849324D20CE0E70396CE300B6E.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/002/032/901/2640091000002032901.20200909203742.08198694138583802744398746951058:50510923021047:2800:0FF6944CFEAB184BD0751CFDB26AA87957A2CB849324D20CE0E70396CE300B6E.png[/IMG]

Requirements:

1. Unity Editor

2. Huawei device

3. Visual Studio

Output:

Fetch and show the users current location (Latitude and Longitude) using Huawei Location Kit.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/002/032/901/2640091000002032901.20200909181633.40969942057801185319840115461477:50510923021047:2800:C4240370CDB9712CE991F732AFAF760AD6043E48AA7DF8DB1CF86936BAF2C85D.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/002/032/901/2640091000002032901.20200909181633.40969942057801185319840115461477:50510923021047:2800:C4240370CDB9712CE991F732AFAF760AD6043E48AA7DF8DB1CF86936BAF2C85D.png[/IMG]

Follows the steps.

1. Create Unity Project.

Click unity icon.

Click NEW, select 3D, Project Name and Location.

Click CREATE, as follows.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/002/032/901/2640091000002032901.20200909182613.96819752866054274486390465437934:50510923021047:2800:38F6331F8E0BBC68837A5B10466B237915759AAF1E73929051DEF4825DCED7BD.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/002/032/901/2640091000002032901.20200909182613.96819752866054274486390465437934:50510923021047:2800:38F6331F8E0BBC68837A5B10466B237915759AAF1E73929051DEF4825DCED7BD.png[/IMG]

2. Click Asset Store, search Huawei HMS Core App Services and click Import, as follows.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/002/032/901/2640091000002032901.20200909190024.61267441553150575353399022225824:50510923021047:2800:FD7C055987BF79C1A268EC20D816C09F0D7242FEF5F47D58466A72745315E6C4.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/002/032/901/2640091000002032901.20200909190024.61267441553150575353399022225824:50510923021047:2800:FD7C055987BF79C1A268EC20D816C09F0D7242FEF5F47D58466A72745315E6C4.png[/IMG]

3. Once import is successful, verify directory in Assets > Huawei HMS Core App Services path, as follows.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/002/032/901/2640091000002032901.20200909190648.33556446754008940797573999985728:50510923021047:2800:9448D45AAE5F89BAB978148283CB979BDDD681AA311F9C1B551F60B9E9D15C04.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/002/032/901/2640091000002032901.20200909190648.33556446754008940797573999985728:50510923021047:2800:9448D45AAE5F89BAB978148283CB979BDDD681AA311F9C1B551F60B9E9D15C04.png[/IMG]

4. Click Console and create a New Project.

5. Choose Project Settings > Player and edit the required options in Publishing Settings, as follows.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/002/032/901/2640091000002032901.20200909193633.77140517757452803046760722378315:50510923021047:2800:4875D29A895AF48EC464992B68353A1199C2962413671F0D5BCE73DA1446B16B.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/002/032/901/2640091000002032901.20200909193633.77140517757452803046760722378315:50510923021047:2800:4875D29A895AF48EC464992B68353A1199C2962413671F0D5BCE73DA1446B16B.png[/IMG]

6. Verify the files created in Step 5.

7. Download agconnect-services.json and copy to Assets/Plugins/Android, as follows.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/002/032/901/2640091000002032901.20200909193849.52729613124561551678071615718070:50510923021047:2800:69FBC55698AB0EAD89085B10F7E0BFC76ECD6E92F1A5B71E83834FD970FB3B9F.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/002/032/901/2640091000002032901.20200909193849.52729613124561551678071615718070:50510923021047:2800:69FBC55698AB0EAD89085B10F7E0BFC76ECD6E92F1A5B71E83834FD970FB3B9F.png[/IMG]

8. Update the Package Name.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/002/032/901/2640091000002032901.20200909194457.40612004953515603531973546931472:50510923021047:2800:E4F8412DE518643F68683138440D3B29BF90C19A09A6EB945107B4C154F54FB0.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/002/032/901/2640091000002032901.20200909194457.40612004953515603531973546931472:50510923021047:2800:E4F8412DE518643F68683138440D3B29BF90C19A09A6EB945107B4C154F54FB0.png[/IMG]

9. Open LauncherTemplate.gradle and add below line

implementation 'com.android.support:appcompat-v7:28.0.0'

10. Open AndroidManifest file & Add below permissions.

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION"/>

11. Add Receiver Like shown below in Android Manifest File.

<receiver
android:name="com.unity.hms.location.LocationBroadcastReceiver"
android:exported="true">

</receiver>

12. Open "baseProjectTemplate.gradle" and add lines, as follows.

maven {url 'https://developer.huawei.com/repo/'}

13. Open "mainTemplate.gradle" and add lines like shown below

implementation 'com.huawei.hms:location:5.0.0.301'
implementation 'com.huawei.hms:ads-lite:13.4.30.301'

14. Scripting, create two classes.

TestClass.cs

using System.Collections;
using System.Collections.Generic;
using HuaweiHms;
using UnityEngine;

public class TestClass : IBroadcastReceiver
{

override
public void onReceive(Context arg0, Intent arg1)
{
Debug.LogError("kamal onReceive--->");
}

}

RegisterReceiver.cs

using System.Collections;
using System.Collections.Generic;
using HuaweiHms;
using UnityEngine;
using UnityEngine.UI;

public class RegisterReceiver : MonoBehaviour
{

static FusedLocationProviderClient fusedLocationProviderClient;
static LocationRequest locatinoRequest;

public Text latitude;
public Text longitude;

private void Awake()
{

//AdListener add = new AdListener();

TestClass receiver = new TestClass();
BroadcastRegister.CreateLocationReceiver(receiver);

Debug.LogError("kamal RegisterReceiver--->");
//LocationRequest request = new LocationRequest();
//request.setInterval(10000);
//request.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
locatinoRequest = LocationRequest.create();
locatinoRequest.setInterval(10000);
locatinoRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);

LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder();
builder.addLocationRequest(locatinoRequest);
LocationSettingsRequest locationSettingsRequest = builder.build();

Activity act = new Activity();
//Context context = new Context();

fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(act);
SettingsClient settingsClient = LocationServices.getSettingsClient(act);
settingsClient.checkLocationSettings(locationSettingsRequest)
.addOnSuccessListener(new OnSuccessListenerTemp(this))
.addOnFailureListener(new OnFailureListenerTemp());

Debug.LogError("kamal RegisterReceiver request send--->");
}

class OnSuccessListenerTemp : OnSuccessListener
{
private RegisterReceiver registerReceiver;

public OnSuccessListenerTemp(RegisterReceiver registerReceiver)
{
this.registerReceiver = registerReceiver;
}

public override void onSuccess(AndroidJavaObject arg0) {
Debug.LogError("kamal onSuccess 0--->");
fusedLocationProviderClient.requestLocationUpdates(locatinoRequest, new OnLocationCallback(this.registerReceiver), Looper.getMainLooper())
.addOnSuccessListener(new OnReqSuccessListenerTemp())
.addOnFailureListener(new OnReqFailureListenerTemp())
;
}

};

class OnReqSuccessListenerTemp : OnSuccessListener
{
public override void onSuccess(AndroidJavaObject arg0)
{
Debug.LogError("kamal onSuccess 1--->");
}

};

class OnReqFailureListenerTemp : OnFailureListener
{

public override void onFailure(Exception arg0)
{
Debug.LogError("kamal onFailure 2--->");
}
}

class OnLocationCallback : LocationCallback {
private RegisterReceiver registerReceiver;

public OnLocationCallback(RegisterReceiver registerReceiver)
{
this.registerReceiver = registerReceiver;
}

public override void onLocationAvailability(LocationAvailability arg0) {
Debug.LogError("kamal onLocationAvailability 0--->");

}

public override void onLocationResult(LocationResult locationResult) {
Location location = locationResult.getLastLocation();
HWLocation hWLocation = locationResult.getLastHWLocation();

Debug.LogError("kamal onLocationResult found location--->");

if (location != null) {
Debug.LogError("kamal getLatitude--->" + location.getLatitude() + "<-getLongitude->" + location.getLongitude());
//latitude.text = "Latitude-->" + location.getLatitude();
//longitude.text = "Longitude-->" + location.getLongitude() ;
//RegisterReceiver.this.updateData(location);
registerReceiver.updateData(location);
}

if (hWLocation != null)
{
string country = hWLocation.getCountryName();
string city = hWLocation.getCity();
string countryCode = hWLocation.getCountryCode();
string dd = hWLocation.getPostalCode();
Debug.LogError("kamal country--->"+country + "<-city->"+city+ "<-countrycode->"+countryCode+"<-postal code->"+dd);
}
else {
Debug.LogError("kamal onLocationResult found location hWLocation is null--->");
}
}
}

private void updateData(Location location) {
latitude.text = "Latitude-->" + location.getLatitude();
longitude.text = "Longitude-->" + location.getLongitude() ;
}

class OnFailureListenerTemp : OnFailureListener {

public override void onFailure(Exception arg0) {
Debug.LogError("kamal onFailure--->");
}
}

// Start is called before the first frame update
void Start()
{

}

// Update is called once per frame
void Update()
{

}
}

15. Code explanation, follow below URL.

https://developer.huawei.com/consumer/en/codelab/HMSLocationKit/index.html#5

16. Run Attach Script to Scene, create two Text Views and assign them to script for showing latitude and longitude.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/002/032/901/2640091000002032901.20200909201834.02976063064938983921196894708866:50510923021047:2800:79B39276B080CC734709926AE49BB36623CA8B1D674C6BD3A5432446EC8CAF93.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/002/032/901/2640091000002032901.20200909201834.02976063064938983921196894708866:50510923021047:2800:79B39276B080CC734709926AE49BB36623CA8B1D674C6BD3A5432446EC8CAF93.png[/IMG]

17. Run the project and enable location permission manually, as its demo I did not do it programmatically.

Conclusion

Latitude and Longitude both are coming on game screen, as shown below

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/002/032/901/2640091000002032901.20200909181633.40969942057801185319840115461477:50510923021047:2800:C4240370CDB9712CE991F732AFAF760AD6043E48AA7DF8DB1CF86936BAF2C85D.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/002/032/901/2640091000002032901.20200909181633.40969942057801185319840115461477:50510923021047:2800:C4240370CDB9712CE991F732AFAF760AD6043E48AA7DF8DB1CF86936BAF2C85D.png[/IMG]

References

Integrate Unity & Account Kit

https://forums.developer.huawei.com/forumPortal/en/topicview?tid=0202334275608690050&fid=0101187876626530001

Integrate Unity & In App purchase

https://forums.developer.huawei.com/forumPortal/en/topicview?tid=0201335993101470045&fid=0101187876626530001

HMS Ads integration with Unity

https://forums.developer.huawei.com/forumPortal/en/topicview?tid=0201337158850280060&fid=0101187876626530001