UM4L5——Unity调用对应API
按照官方文档添加相关功能代码
此部分参考:
Android Studio内大致实现思路:
- 声明一个监听类继承
BDAbstractLocationListener
,它用于在MainActivity
内被调用,开始获取定位信息
该类里需要实现一个监听定位成功的函数onReceiveLocation
,定位成功后通过传入的定位结果类BDLocation
对象获取定位的地址信息
将获取到的地址信息通过UnityPlayer.UnitySendMessage()
发送到Unity,再通过Unity输出这些消息
- 在
MainActivity
内实现一个提供给Unity调用的获取地址的方法GetAddress()
,
该方法会先执行隐私合规接口,确保获取地址消息正常,然后实例化LocationClient
和监听类MyLocationListener
,
然后将监听类对象传入到LocationClient
内,待定位成功后就会调用MyLocationListener
的onReceiveLocation
方法
然后设置各个参数,将调整的参数传入到LocationClient
对象内,最后执行LocationClient
对象的开始获取定位方法
具体代码如下:
MyLocationListener.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| package com.taikr.androidsdk;
import com.baidu.location.BDAbstractLocationListener; import com.baidu.location.BDLocation; import com.unity3d.player.UnityPlayer;
public class MyLocationListener extends BDAbstractLocationListener {
@Override public void onReceiveLocation(BDLocation location) {
String addr = location.getAddrStr(); String country = location.getCountry(); String province = location.getProvince(); String city = location.getCity(); String district = location.getDistrict(); UnityPlayer.UnitySendMessage("Lesson6", "GetAddress", country + "_" + province + "_" + city + "_" + district); } }
|
MainActivity.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
| package com.taikr.androidsdk;
import android.os.Bundle;
import com.baidu.location.LocationClient; import com.baidu.location.LocationClientOption; import com.unity3d.player.UnityPlayerActivity;
public class MainActivity extends UnityPlayerActivity {
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);
}
public void GetAddress() { LocationClient.setAgreePrivacy(true); try { LocationClient mLocationClient = null; MyLocationListener myListener = new MyLocationListener(); mLocationClient = new LocationClient(getApplicationContext()); mLocationClient.registerLocationListener(myListener);
LocationClientOption option = new LocationClientOption();
option.setLocationMode(LocationClientOption.LocationMode.Hight_Accuracy);
option.setCoorType("bd09ll");
option.setFirstLocType(LocationClientOption.FirstLocType.ACCURACY_IN_FIRST_LOC);
option.setScanSpan(1000);
option.setOpenGnss(true);
option.setLocationNotify(true);
option.setIgnoreKillProcess(false);
option.SetIgnoreCacheException(false);
option.setWifiCacheTimeOut(5*60*1000);
option.setEnableSimulateGnss(false);
option.setNeedNewVersionRgc(true);
option.setIsNeedAddress(true);
option.setNeedNewVersionRgc(true);
mLocationClient.setLocOption(option);
mLocationClient.setLocOption(option);
mLocationClient.start(); } catch (Exception e) { e.printStackTrace(); } } }
|
将相关信息传递给Unity
Unity脚本内大致实现思路:
- 在界面上添加一个获取地址的按钮,以及在获取到地址后用于显示地址消息在屏幕上的文本控件
- 按下获取地址按钮会调用Android的
GetAddress()
方法,待获取定位结束后,Android会调用Unity的方法,将消息输出到文本控件上
具体实现代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| using UnityEngine; using UnityEngine.UI;
public class Lesson6 : MonoBehaviour { public Button btnOpen; public Text txtInfo;
private void Start() { btnOpen.onClick.AddListener(() => { using (AndroidJavaClass ajc = new AndroidJavaClass("com.unity3d.player.UnityPlayer")) { using (AndroidJavaObject ajo = ajc.GetStatic<AndroidJavaObject>("currentActivity")) { ajo.Call("GetAddress"); } } }); }
public void GetAddress(string address) { txtInfo.text = address; } }
|
将生成的aar包导入Unity
将Android Studio的交互项目生成为arr包,再做必要的处理即可导入到Unity内调用
具体可看:UM3L5——Unity中嵌入Android内容