Unity与Android交互-Unity接入高德地图实现定位以及搜索周边的功能(使用Android Studio)详细操作

时间:2023-03-09 06:18:58
Unity与Android交互-Unity接入高德地图实现定位以及搜索周边的功能(使用Android Studio)详细操作

刚进公司给安排的任务就是Unity接入高德地图,算是踩了不少坑总算做出来了,抽点时间写个博客记录一下

废话不多说

先上效果图

获取定位并根据手机朝向显示周边信息

Unity与Android交互-Unity接入高德地图实现定位以及搜索周边的功能(使用Android Studio)详细操作          Unity与Android交互-Unity接入高德地图实现定位以及搜索周边的功能(使用Android Studio)详细操作

使用的Unity版本为5.5,Androad Studio 2.3.1

接下来开始讲具体操作

首先是Androad Studio的基本配置


1.创建工程,空白的就行,反正也用不到界面布局

Unity与Android交互-Unity接入高德地图实现定位以及搜索周边的功能(使用Android Studio)详细操作

等待创建完成

2.新建库模块:

切换到Project视图

Unity与Android交互-Unity接入高德地图实现定位以及搜索周边的功能(使用Android Studio)详细操作

右击你的项目 新建一个库模块-用来负责与Unity交互

当然你也可以不选择新建库模块 直接在原生app模块进行操作

Unity与Android交互-Unity接入高德地图实现定位以及搜索周边的功能(使用Android Studio)详细操作

选择Android Library

Unity与Android交互-Unity接入高德地图实现定位以及搜索周边的功能(使用Android Studio)详细操作

等待生成完成

你会看到多出来的

Unity与Android交互-Unity接入高德地图实现定位以及搜索周边的功能(使用Android Studio)详细操作

3.创建MainActivity:我们新建的library中没有启动这个模块的Java类 所以需要手动创建一个

切换到Android视窗下 选中此文件右键创建

Unity与Android交互-Unity接入高德地图实现定位以及搜索周边的功能(使用Android Studio)详细操作

Unity与Android交互-Unity接入高德地图实现定位以及搜索周边的功能(使用Android Studio)详细操作

4.删除布局文件activity_main

布局文件是用来管理Android界面布局的,我们并不需要,所以将它删除,防止发生一些没必要的错误

Unity与Android交互-Unity接入高德地图实现定位以及搜索周边的功能(使用Android Studio)详细操作

5.修改配置文件:AndroidManifest

为了能够正常发布 需要将AndroidManifest进行一些修改

Unity与Android交互-Unity接入高德地图实现定位以及搜索周边的功能(使用Android Studio)详细操作

在这中间插入启动Activity的配置

Unity与Android交互-Unity接入高德地图实现定位以及搜索周边的功能(使用Android Studio)详细操作

        <activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

到这里基本配置就完了

然后进行测试点击这个

Unity与Android交互-Unity接入高德地图实现定位以及搜索周边的功能(使用Android Studio)详细操作

或许你会发现报错了 原因是我们在创建MainActivity的时候他自动引用了布局文件 所以这里应该将这一行删除

Unity与Android交互-Unity接入高德地图实现定位以及搜索周边的功能(使用Android Studio)详细操作

然后再次更新 成功

到这里基本配置以及完成

引入Unity与高德地图的包

下载高德地图包 这里有个定制选你要用的功能下载就好 这里我用到的是定位和搜索

Unity与Android交互-Unity接入高德地图实现定位以及搜索周边的功能(使用Android Studio)详细操作

将下载好的包拖入libs中

Unity与Android交互-Unity接入高德地图实现定位以及搜索周边的功能(使用Android Studio)详细操作

接下来是Unity的包

新建Unity工程

发布平台改Android

设置package name与你新建的library库一致

Unity与Android交互-Unity接入高德地图实现定位以及搜索周边的功能(使用Android Studio)详细操作

然后发布系统ADT 导出

Unity与Android交互-Unity接入高德地图实现定位以及搜索周边的功能(使用Android Studio)详细操作

在libs中找到

Unity与Android交互-Unity接入高德地图实现定位以及搜索周边的功能(使用Android Studio)详细操作

复制进Android Sturio中的libs

选中两个包Add As Library

Unity与Android交互-Unity接入高德地图实现定位以及搜索周边的功能(使用Android Studio)详细操作

选library工程 OK

Unity与Android交互-Unity接入高德地图实现定位以及搜索周边的功能(使用Android Studio)详细操作

等待加载完成

到这里引入包的操作已经完成

更改MainActivity并发布

主要内容为

获取当前定位信息

获取查询指定字符串周边信息

 package com.example.autlibrary;

 import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;
import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;
import com.amap.api.services.core.LatLonPoint;
import com.amap.api.services.core.PoiItem;
import com.amap.api.services.poisearch.PoiResult;
import com.amap.api.services.poisearch.PoiSearch;
import com.unity3d.player.UnityPlayerActivity; public class MainActivity
extends UnityPlayerActivity
implements PoiSearch.OnPoiSearchListener
{
public AMapLocationClient mLocationClient = null;
public AMapLocationClientOption mLocationOption = null;
private String LocationInfo;
private String strRerurnInfo;
private PoiSearch.Query query;
private PoiResult poir;
private double Latitude;
private double Longitude;
private boolean bolIsPoi = false; protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
}
//获取定位信息
public String GetInfo()
{
startLocation();
this.bolIsPoi = true;
return this.LocationInfo;
}
//获取周边POI信息
public String GetPoi(String content, String val, int index)
{
startLocation();
search(content, val, index);
return this.strRerurnInfo;
} protected void onStart()
{
super.onStart();
} private void startLocation()
{
this.mLocationClient = new AMapLocationClient(getApplicationContext());
this.mLocationClient.setLocationListener(this.mLocationListener);
this.mLocationOption = new AMapLocationClientOption();
this.mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
this.mLocationOption.setInterval(2000L);
this.mLocationClient.setLocationOption(this.mLocationOption);
this.mLocationClient.startLocation();
} //三个参数分别为搜索字符串、搜索类型、查询第几页
//前两个参数选其一
//如:酒店、""、1
//第二个参数为poi搜索类型:
//汽车服务|汽车销售|汽车维修|摩托车服务|餐饮服务|购物服务|生活服务|体育休闲服务|
// 医疗保健服务|住宿服务|风景名胜|商务住宅|*机构及社会团体|科教文化服务|
// 交通设施服务|金融保险服务|公司企业|道路附属设施|地名地址信息|公共设施
public void search(String content, String val, int index)
{
if (this.bolIsPoi) {
if (content == null)
{
Toast.makeText(this, "输入为空", Toast.LENGTH_SHORT).show();
}
else
{
this.query = new PoiSearch.Query(content, val, "");
this.query.setPageSize(30);
this.query.setPageNum(index);
PoiSearch poiSearch = new PoiSearch(this, this.query);
if ((this.Latitude != 0.0D) && (this.Longitude != 0.0D))
{
poiSearch.setBound(new PoiSearch.SearchBound(new LatLonPoint(this.Latitude, this.Longitude), 6000)); poiSearch.setOnPoiSearchListener(this);
poiSearch.searchPOIAsyn();
}
else
{
Toast.makeText(this, "定位失败", Toast.LENGTH_SHORT).show();
}
}
}
} public void onPoiSearched(PoiResult result, int code)
{
this.bolIsPoi = false;
System.out.println("Result" + (result.getPois().get(0)).getLatLonPoint());
System.out.println("Code" + code);
this.poir = result;
StringBuffer sb = new StringBuffer(256);
for (int j = 0; j < this.poir.getPois().size(); j++)
{
sb.append("\n名字:");
sb.append((this.poir.getPois().get(j)).getTitle());
sb.append("\n>地址:");
sb.append((this.poir.getPois().get(j)).getSnippet());
sb.append("\n>距离:");
sb.append((this.poir.getPois().get(j)).getDistance());
}
this.strRerurnInfo = sb.toString();
} @Override
public void onPoiItemSearched(PoiItem poiItem, int i) { } public AMapLocationListener mLocationListener = new AMapLocationListener() {
@Override
public void onLocationChanged(AMapLocation location) {
if (location != null) {
if (location.getErrorCode() == 0) {
//获取坐标信息
Latitude = location.getLatitude();
Longitude = location.getLongitude(); StringBuffer sb = new StringBuffer(256);
sb.append("时间: " + location.getTime());
sb.append("\n纬度:" + location.getLatitude());
sb.append("\n经度:" + location.getLongitude());
sb.append("\n精度:" + location.getAccuracy());
sb.append("\n地址:" + location.getAddress());
sb.append("\n国家信息:" + location.getCountry());
sb.append("\n省信息:" + location.getProvince());
sb.append("\n城市信息:" + location.getCity());
sb.append("\n城区信息:" + location.getDistrict());
sb.append("\n街道信息:" + location.getStreet());
sb.append("\n街道门牌号信息:" + location.getStreetNum());
sb.append("\n城市编码:" + location.getCityCode());
sb.append("\n地区编码:" + location.getAdCode());
sb.append("\n定位点AOI信息:" + location.getAoiName());
LocationInfo = sb.toString();
}else {
Log.e("AmapError","location Error, ErrCode:"
+ location.getErrorCode() + ", errInfo:"
+ location.getErrorInfo());
}
}
}
};
}

MainActivity

修改完MainActivity 无错误的话 就可以发布了

发布到library库中

Unity与Android交互-Unity接入高德地图实现定位以及搜索周边的功能(使用Android Studio)详细操作

然后你在

“你的工程目录”\autlibrary\build\intermediates\bundles\debug中会有这些

Unity与Android交互-Unity接入高德地图实现定位以及搜索周边的功能(使用Android Studio)详细操作

右键Show in Exploer   并在文件夹中找到他们

Unity与Android交互-Unity接入高德地图实现定位以及搜索周边的功能(使用Android Studio)详细操作

这样就发布成功了

与Unity间的交互

上一步我们导出了工程包

我们需要将它修改为Unity可用的工程

复制classes.jar

粘贴到libs

删除libs中的unity-classes.jar

Unity与Android交互-Unity接入高德地图实现定位以及搜索周边的功能(使用Android Studio)详细操作

-

Unity与Android交互-Unity接入高德地图实现定位以及搜索周边的功能(使用Android Studio)详细操作

-

Unity与Android交互-Unity接入高德地图实现定位以及搜索周边的功能(使用Android Studio)详细操作

这样的话就修改成功了  然后我们将它导入Unity

复制libs和res文件夹到Unity

Unity与Android交互-Unity接入高德地图实现定位以及搜索周边的功能(使用Android Studio)详细操作

需要创建Plugins和Android文件夹 复制过后是这样的关系

Unity与Android交互-Unity接入高德地图实现定位以及搜索周边的功能(使用Android Studio)详细操作

这样就成功导入进了Unity

配置AndroidManifest.Xml文件,并创建C#脚本

不知道你还记不记得我们在Unity中导出的包 找到它!我们需要里面的Xml文件

Unity与Android交互-Unity接入高德地图实现定位以及搜索周边的功能(使用Android Studio)详细操作

将它复制到\Assets\Plugins\Android下

Unity与Android交互-Unity接入高德地图实现定位以及搜索周边的功能(使用Android Studio)详细操作

接着就要对它进行修改了

Unity与Android交互-Unity接入高德地图实现定位以及搜索周边的功能(使用Android Studio)详细操作

 <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.autlibrary" xmlns:tools="http://schemas.android.com/tools" android:versionName="1.0" android:versionCode="1" android:installLocation="preferExternal">
<supports-screens android:smallScreens="true" android:normalScreens="true" android:largeScreens="true" android:xlargeScreens="true" android:anyDensity="true" />
<application android:theme="@style/UnityThemeSelector" android:icon="@drawable/app_icon" android:label="@string/app_name" android:debuggable="false" android:isGame="true" android:banner="@drawable/app_banner">
<meta-data
android:name="com.amap.api.v2.apikey"
android:value="64c821ae174ab7429fa45535d01f20ae"/>
<activity
android:label="@string/app_name" android:screenOrientation="fullSensor"
android:launchMode="singleTask"
android:configChanges="mcc|mnc|locale|touchscreen|keyboard|keyboardHidden|navigation|orientation|screenLayout|uiMode|screenSize|smallestScreenSize|fontScale"
android:name="com.example.autlibrary.MainActivity">
<service android:name="com.amap.api.location.APSService" ></service>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
<category android:name="android.intent.category.LEANBACK_LAUNCHER" />
</intent-filter>
<meta-data android:name="unityplayer.UnityActivity" android:value="true" />
</activity>
</application>
<uses-sdk android:minSdkVersion="16" android:targetSdkVersion="25" />
<uses-feature android:glEsVersion="0x00020000" />
<uses-feature android:name="android.hardware.touchscreen" android:required="false" />
<uses-feature android:name="android.hardware.touchscreen.multitouch" android:required="false" />
<uses-feature android:name="android.hardware.touchscreen.multitouch.distinct" android:required="false" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_CONFIGURATION" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.WRITE_SETTINGS" />
</manifest>

AndroidManifest

配置完毕就可以搭界面和写C#逻辑了

新建GetLocationAndPoiInfo脚本

 using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI; public class GetLocationAndPoiInfo : MonoBehaviour { public Text locText;
public Text poiText;
public Button locBtn;
public Button poiBtn; AndroidJavaClass jc;
AndroidJavaObject jo; // Use this for initialization
void Start () {
OnStart();
locBtn.onClick.AddListener(() => { GetLocationInfo(); });
poiBtn.onClick.AddListener(() => { GetPoiInfo(); });
}
void OnStart() {
jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
jo = jc.GetStatic<AndroidJavaObject>("currentActivity");
} void GetLocationInfo() {
locText.text = "";
OnStart();
locText.text = jo.Call<string>("GetInfo");
} void GetPoiInfo() {
locText.text = "";
OnStart();
poiText.text = jo.Call<string>("GetPoi", "酒店", "", );
}
}

GetLocationAndPoiInfo

将脚本挂在任何一个物体上

布置界面

Unity与Android交互-Unity接入高德地图实现定位以及搜索周边的功能(使用Android Studio)详细操作

因为是Android工程发布到手机(或模拟器)才能运行

上成果图

Unity与Android交互-Unity接入高德地图实现定位以及搜索周边的功能(使用Android Studio)详细操作

到这里接高德地图SDK的工作就做完了

根据手机朝向显示不同店家的逻辑我就不写了 挺麻烦的

我说一下思路:

在AndroidStudio中获取各个店家的经纬度与自身坐标点的相对位置信息并输出

在Unity中获取到这个信息、解析、并转换为角度、再转换为匹配Input.compass指南针坐标系的角度

然后设置一个视野范围(度数)

最后根据手机朝向显示视野范围内不同的店家

这么做有什么用处呢

做类似pokemon go这样的东西时候这些信息就有用了

写在最后:第一次公开写博,如有不妥之处请多指教