How Can I Quickly Integrate AppGallery Connect Cloud DB into a Unity App…

1. Configuring the Environment and Version Information

2. Preparing the Unity Environment

3. Completing Configurations in AppGallery Connect

4. Configuring Project Information in Unity Editor

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/' }
}
}
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. Using Cloud DB

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<BookInfo> mObjectList = new CloudDBZoneObjectList<BookInfo> ();

// Callback upon success.
public delegate void SuccessCallBack<T>(T o);
public class HmsSuccessListener<T>:OnSuccessListener{
public SuccessCallBack<T> CallBack;
public HmsSuccessListener(SuccessCallBack<T> 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);
}
}
}
// Callback upon failure.
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()
{

}
}
public void login()
{
AGConnectAuth auth = AGConnectAuth.getInstance();
auth.signInAnonymously().addOnSuccessListener(new HuaweiOnsuccessListener<SignInResult>((signresult) =>
{
Debug.Log("sign in successfully." + signresult.getUser().getUid());

})).addOnFailureListener(new HuaweiOnFailureListener((e) =>
{
Debug.Log("sign in failed");
}));
}

6. Integrating APIs of Cloud DB

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);
}
}
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>((cloudDBZone) =>
{
mCloudDBZone = cloudDBZone;
Debug.Log("open clouddbzone2 success");
}))
.addOnFailureListener(new HmsFailureListener((HuaweiService.Exception e) =>
{
Debug.Log("open clouddbzone2 failed " + e.toString());
}));
}
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<int>((cloudDBZoneResult) =>
{
Debug.Log("upsert " + cloudDBZoneResult + " records");
}))
.addOnFailureListener(new HmsFailureListener((exception) =>
{
Debug.Log("upsert bookinfo failed: " + exception.toString());
}));
}
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<CloudDBZoneSnapshot<BookInfo>> ((snapshot) => {
processQueryResult (snapshot);
}))
.addOnFailureListener (new HmsFailureListener ((exception) => {
Debug.Log ("Query book list from cloud failed: " + exception.toString ());
}));
}

private void processQueryResult (CloudDBZoneSnapshot<BookInfo> snapshot) {
mObjectList = snapshot.getSnapshotObjects ();
List<BookInfo> bookInfoList = new List<BookInfo> ();
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. Packing and Testing Your App

--

--

--

https://github.com/AppGalleryConnect

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Integrating HUAWEI Game Service Based on Cocos SDKHub — Initialization

#TheAndroidShow: Jetpack Compose - our thoughts

Assets or Resource Raw folder of Android?

Accessibility in The Android

The Final Recycler View’s Adapter

How to use gif image in android

Android hacked? Any URL could be deeplinked to your app by default!

Hilt — An optimal dependency injector for Android Projects.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Mayism

Mayism

https://github.com/AppGalleryConnect

More from Medium

Redraw: Metal Gear Solid

What do I need to produce electronic music?

I was thinking… about passwords. xD

Wrapping Up: Senior Graphic Design Studio —Fall ‘21