Unity调用百度地图(可实现模型、UI覆盖)

前言:本文更多是在于分析,实现请下载工程进行查看。
工程下载地址:http://download.csdn.net/detail/zzmkljd/9762173(需要申请百度地图的APIKey填入网页代码中)
测试apk下载地址:http://download.csdn.net/detail/zzmkljd/9762175
这里写图片描述

相信如果在看我这篇博客的朋友应该都已经将度娘上【Unity】+【百度地图】下的搜索结果的都看过一遍了,大概列举一下:
1、使用百度地图的Android SDK,将自己写好的创建地图View的方法打成jar包,并在Unity中调用。
2、使用百度地图的静态图链接上传参数,返回相应的静态图。
3、使用百度地图的网页版,相当于在Unity中打开网页操作。
分析一下这三种方法:
第一种方法是大家所能想到的最直接的接入百度地图的方法,应该说实用性也比较好,但是由于是AddView操作,所以不能实现模型、UI的覆盖(因为Unity最终实现也是一个View,我曾经想尝试找到让两个View进行类似Alpha混合的方法)。
第二种方法可以实现模型、UI的覆盖,但是由于下载静态图需要花费相对较多的时间,所以会有一段等待时间,说白了就是加载不同的图片,效果比较差。
来看看最后一种方法,我们先说说Unity打开网页的插件,最著名的就是uWebKit和UniWebView了,但是他们在安卓端打开网页的实现原理都是调用了安卓的WebView,所以这种方式又该被否决了。但是最近发现了一个叫CoherentUI的插件,该插件的意图就是让Web中制作的UI以View的形式与Unity进行交互。该插件在android端打开网页的实现方式有两种,一种是调用WebView,另一种则是使用Surface,也就是本文重点要阐述的对象。
使用CoherentUI插件的MobileSurfaceUIView打开的网页,虽然不能直接和Unity进行交互,但它可以作为材质贴图使用(本人不太了解Surface)。在几番寻找之后终于在该插件的论坛中找到了一篇关于间接交互的文章:https://forums.coherent-labs.com/index.php/topic,194.msg560.html,大概意思就是所有输入事件都由Unity进行判断,如果是网页点击事件的话就将坐标点传给Web,由Web进行输入事件的生成和分发。这样即实现了模型覆盖,又避免了因为层级混合引发的输入响应问题(点击屏幕时到底是Unity做出响应还是Web做出响应)。

最后贴出Unity中的脚本和Web代码以注明间接交互的方式避免大家踩坑:

MobileSurface.cs

using UnityEngine;
#if UNITY_EDITOR || COHERENT_UNITY_STANDALONE
using Coherent.UI.Binding;
using CoherentUI = Coherent.UI;
#elif UNITY_IPHONE || UNITY_ANDROID
using Coherent.UI.Mobile.Binding;
using CoherentUI = Coherent.UI.Mobile;
using Coherent.UI;
#endif

public class MobileSurface : MonoBehaviour
{

    private MobileSurfaceView m_View;
    public GameObject cube;

    void Start()
    {
        // 添加安卓联网权限
        string a = SystemInfo.deviceUniqueIdentifier;

        m_View = GetComponent<MobileSurfaceView>();
    }

    void Update()
    {
        cube.transform.Rotate(Vector3.up,1,Space.World);
    }

    /// <summary>
    /// 更新View
    /// </summary>

    [CoherentUI.CoherentMethod("myUpdateView")]
    private void myUpdateView()
    {
        m_View.UpdateView();
    }

    /// <summary>
    /// 放大
    /// </summary>
    public void ZoomIn()
    {
        m_View.View.ExecuteScript("zoomIn();");
        m_View.UpdateView();
    }

    /// <summary>
    /// 缩小
    /// </summary>
    public void ZoomOut()
    {
        m_View.View.ExecuteScript("zoomOut();");
        m_View.UpdateView();
    }
}

map.html

<!DOCTYPE html>  
<html>  
<head>  
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />  
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />  
<title>Hello, World</title>  
<style type="text/css">  
html{height:100%}  
body{height:100%;margin:0px;padding:0px}  
#container{height:100%}  
</style>  
<script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=申请的Key">
</script>
</head>  

<body>  
<div id="container"></div> 
    <script type="text/javascript" src="javascript/coherent.js"></script>
<script type="text/javascript"> 
var map = new BMap.Map("container");
var point = new BMap.Point(116.404, 39.915);  
map.centerAndZoom(point, 15);
     engine.call("myUpdateView");                 
 function zoomIn()
{
     map.zoomIn();
}

 function zoomOut()
{
     map.zoomOut();
}

</script>  
</body>  
</html>

注明:
Unity调Web代码:
View.ExecuteScript(“方法”);

Web调Unity代码:
在Unity中为该方法添加[CoherentUI.CoherentMethod(“方法”)]属性;
在Web代码中添加<script type="text/javascript" src="javascript/coherent.js"></script>
最后在Web中engine.call(“方法”);

评论 30
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值