# 1、环境与应用信息
版本名称 | 集成环境 | 测试设备 |
---|---|---|
agconnect-database:1.2.3.301 | Unity 2019.4.17f1c1 | 荣耀magic2 OPPO Reno2 |
AGC地址:https://developer.huawei.com/consumer/cn/service/josp/agc/index.html
2、Unity环境准备:
官方文档:
https://docs.unity.cn/cn/Packages-cn/[email protected]/manual/clouddb.html
1、 下载Unity Hub,安装Unity
https://unity.cn/releases。 下载安装就不详细介绍了。
2、 Android环境配置:可参考如下截图
3、导入HuaweiServices的Package包:
在Assect Store中搜索Huawei Services, 找到AGC的包,然后点击Import,导入相应的包。
3、AGC环境配置
1、配置华为AGC上的参数:回到AGC控制台:找到之前创建的App:
https://developer.huawei.com/consumer/cn/service/josp/agc/index.html
2、选择:我的项目-> 构建 – 云数据库 点击开通,开通云数据库,然后根据华为文档要求,创建对象类型和存储区
在对象类型页签,点击新增,新增一个名为BookInfo的对象类型,配置如下:
在存储区页面,点击新增,新增一个名为QuickStartDemo的存储区
3、服务开通以后,回到项目设置界面,下载最新的json文件。在我的项目-项目设置-常规-应用下,点击agconnect-services.json下载
将刚下载好json文件,放到Unity项目Assets / Plugins / Android目录下:
4、设置Android环境:
1、在Player-Publish Setting中启用Android的自定义清单和gradle
2、在Other Setting中配置包名:注意需要和AG官网的包名保持一致:
3、配置项目级gradle,此处对应Assets\Plugins\Android路径下的baseProjectTmeplate.gradle文件,添加如下内容
allprojects {
buildscript {
repositories {
maven { url 'https://developer.huawei.com/repo/' }
}
}
dependencies {
classpath 'com.android.tools.build:gradle:3.4.0'
classpath 'com.huawei.agconnect:agcp:1.4.2.301'
**BUILD_SCRIPT_DEPS**
}
}
repositories {
maven { url 'https://developer.huawei.com/repo/' }
}
}
4、配置应用级gradle,此处对应Assets\Plugins\Android路径下的LauncherTmeplate.gradle文件,添加如下内容:
apply plugin: 'com.android.application'
apply plugin: 'com.huawei.agconnect'
dependencies {
implementation project(':unityLibrary')
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.huawei.agconnect:agconnect-core:1.2.0.300'
implementation "com.huawei.agconnect:agconnect-database:1.2.3.301"
}
5、回到AGC界面,导出对象类型文件:
1、点击“导出”,选择json,将json文件导出到Unity项目Assets / Plugins / Android目录下
2、点击“导出”,选择java,选择“Android”,配置包名,将zip文件导出并且解压到Unity项目Assets / Plugins / Android目录下
6、生成CloudDB文件:
在Unity中,点击菜单栏的CloudDB Kit - > CloudDB Settting 相应的配置如下:
1、 JavaCode Package Name:配置导出java文件时配置的包名
2、 ObjectType JsonFile Path:选择对应导出的json文件:例如我的路径为‘Assets/Plugins/Andriod/model.json’
3、 Namespace:生成的C# 文件所用的namespace
4、 Export Path:导出的C# 文件的存放路径
5、从零开发云数据库功能
1、点击GameObject- UI –Button, 创建几个按钮:
点击按钮,然后在右侧选择 Add Component, 创建并且添加一个Script文件。对应的界面布局如下:
2、代码前置与集成准备:
using System;
using System.Collections;
using UnityEngine;
using HuaweiService;
using HuaweiService.CloudDB;
using HuaweiService.Auth;
using HuaweiServiceDemo;
public class testDBDemo : MonoBehaviour
{
private static AGConnectCloudDB mCloudDB;
private static CloudDBZone mCloudDBZone;
private static CloudDBZoneConfig mConfig;
private static CloudDBZoneQuery mQuery;
private const string mClouudDBZoneName = "QuickStartDemo";
private const string bookInfoClass = "com.huawei.agc.clouddb.quickstart.model.BookInfo";
private const string testAllClass = "com.huawei.agc.clouddb.quickstart.model.TestAll";
private static CloudDBZoneObjectList mObjectList = new CloudDBZoneObjectList ();
// 成功回调
public delegate void SuccessCallBack(T o);
public class HmsSuccessListener:OnSuccessListener{
public SuccessCallBack CallBack;
public HmsSuccessListener(SuccessCallBack c){
CallBack = c;
}
public void onSuccess(T arg0)
{
Debug.Log("OnSuccessListener onSuccess");
if(CallBack != null)
{
CallBack.Invoke(arg0);
}
}
public override void onSuccess(AndroidJavaObject arg0){
Debug.Log("OnSuccessListener onSuccess");
if(CallBack !=null)
{
Type type = typeof(T);
IHmsBase ret = (IHmsBase)Activator.CreateInstance(type);
ret.obj = arg0;
CallBack.Invoke((T)ret);
}
}
}
// 失败回调
public delegate void FailureCallBack(HuaweiService.Exception e);
public class HmsFailureListener : OnFailureListener
{
public FailureCallBack CallBack;
public HmsFailureListener(FailureCallBack c)
{
CallBack = c;
}
public override void onFailure(HuaweiService.Exception arg0)
{
if (CallBack != null)
{
CallBack.Invoke(arg0);
}
}
}
// Start is called before the first frame update
void Start()
{
login();
}
// Update is called once per frame
void Update()
{
}
}
3、集成认证服务:此处为方便使用,集成匿名认证
public void login()
{
AGConnectAuth auth = AGConnectAuth.getInstance();
auth.signInAnonymously().addOnSuccessListener(new HuaweiOnsuccessListener((signresult) =>
{
Debug.Log("sign in successfully." + signresult.getUser().getUid());
})).addOnFailureListener(new HuaweiOnFailureListener((e) =>
{
Debug.Log("sign in failed");
}));
}
6、集成云数据库接口
1、始化以及创建对象类型
public void CreateObjectType()
{
AGConnectCloudDB.initialize(new Context());
mCloudDB = AGConnectCloudDB.getInstance();
Debug.Log("CloudDBInstance: " + mCloudDB);
try
{
mCloudDB.createObjectType(ObjectTypeInfoHelper.getObjectTypeInfo());
Debug.Log("createObjectType:" + mCloudDB );
}
catch (System.Exception e)
{
Debug.Log("createObjectType: " + e.Message);
}
}
2、openCloudZOne,打开存储区
public void OpenCloudDBZone()
{
// Debug.Log("Start OpenCloudDBZone:" + mCloudDB );
mConfig = new CloudDBZoneConfig("QuickStartDemo",
CloudDBZoneConfig.CloudDBZoneSyncProperty.CLOUDDBZONE_CLOUD_CACHE,
CloudDBZoneConfig.CloudDBZoneAccessProperty.CLOUDDBZONE_PUBLIC);
mConfig.setPersistenceEnabled(true);
Task openDBZoneTask = mCloudDB.openCloudDBZone2(mConfig, true);
openDBZoneTask.addOnSuccessListener(new HmsSuccessListener((cloudDBZone) =>
{
mCloudDBZone = cloudDBZone;
Debug.Log("open clouddbzone2 success");
}))
.addOnFailureListener(new HmsFailureListener((HuaweiService.Exception e) =>
{
Debug.Log("open clouddbzone2 failed " + e.toString());
}));
}
3、插入数据
public void upsertBookInfo(){
if (mCloudDBZone == null)
{
Debug.Log("CloudDBZone is null, try re-open it");
return;
}
var bookInfo = new BookInfo { Id = 4, BookName = "test4" };
Task task = mCloudDBZone.executeUpsert(bookInfo);
task.addOnSuccessListener(new HmsSuccessListener((cloudDBZoneResult) =>
{
Debug.Log("upsert " + cloudDBZoneResult + " records");
}))
4. 查询与处理数据并展示
public void testQueryInfo () {
mQuery = CloudDBZoneQuery.where (new AndroidJavaClass (bookInfoClass)).equalTo ("shadowFlag", true);
if (mCloudDBZone == null || mQuery == null) {
Debug.Log("CloudDBZone or CloudDBZoneQuery is null, try re-open it");
return;
}
Task queryTask = mCloudDBZone.executeQuery (mQuery, CloudDBZoneQuery.CloudDBZoneQueryPolicy.POLICY_QUERY_FROM_CLOUD_ONLY);
queryTask.addOnSuccessListener (new HmsSuccessListener> ((snapshot) => {
processQueryResult (snapshot);
}))
.addOnFailureListener (new HmsFailureListener ((exception) => {
Debug.Log ("Query book list from cloud failed: " + exception.toString ());
}));
}
private void processQueryResult (CloudDBZoneSnapshot snapshot) {
mObjectList = snapshot.getSnapshotObjects ();
List bookInfoList = new List ();
try {
string result = "";
while (mObjectList.hasNext ()) {
BookInfo bookInfo = mObjectList.next ();
bookInfoList.add (bookInfo);
result += $"{bookInfo.BookName} ";
}
Debug.Log ($"QueryResult: {result}");
} catch (System.Exception e) {
Debug.Log ($"QueryResult : {e.Message}");
} finally {
snapshot.release ();
}
}
7、打包测试
1、 点击File – Build and setting,进入打包页面,在该页面切换Android Platform。
2、 选择好对应的设备以后,点击 Build And Run
3、 然后就可以再Android的 Logcat 中,查看Unity的相关日志。
欲了解更多详情,可参考:
云数据库Demo:https://github.com/AppGalleryConnect/agc-demos/tree/main/Android/agc-clouddb-demo-java
原文链接:https://developer.huawei.com/consumer/cn/forum/topic/0203465380835350101?fid=0101271690375130218
原作者:Mayism