How Can I Quickly Integrate AppGallery Connect Cloud DB into a Unity App…
To use the relevant Cloud DB functions in HUAWEI AppGallery Connect, you can visit GitHub to download the code.
1. Configuring the Environment and Version Information
Version:agconnect-database:1.2.3.301
Environment:Unity 2019.4.17f1c1
Test Device:HONOR Magic 2 OPPO Reno2
AppGallery Connect:
https://developer.huawei.com/consumer/en/service/josp/agc/index.html
2. Preparing the Unity Environment
Unity materials:
https://docs.unity.cn/cn/Packages-cn/com.unity.huaweiservice@1.3/manual/cloudstorage.html
1) Download Unity Hub and install Unity Editor.
2) Configure the Android environment.

3) Import the HuaweiService package.
Find the package for AppGallery Connect by searching for it on Unity Asset Store, and click Import to import the package.

3. Completing Configurations in AppGallery Connect
1. Sign in to AppGallery Connect, and click the created app.
2. Go to My projects > Build > Cloud DB and click Enable now. Then, create an object type and a storage zone.
Click Add on the ObjectTypes tab page to add an object type named BookInfo.

On the Cloud DB Zones tab page, click Add to add a storage zone named QuickStartDemo.
3. After the service is enabled, go to My projects > Project settings > General information > App information and download the latest agconnect-services.json file.
Save the file to the Assets\Plugins\Android directory of your Unity project.
4. Configuring Project Information in Unity Editor
1. Choose Edit > Project Settings > Player > Publish Settings. In the Build area, select the items for Android according to your requirements.

2. Choose Edit > Project Settings > Player > Other Settings, and set the package name to match the package name you set in AppGallery Connect.

3. Add the following code to the project-level baseProjectTmeplate.gradle file in the Assets\Plugins\Android directory:
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. Add the following code to the app-level LauncherTmeplate.gradle file in the Assets\Plugins\Android directory:
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. Return to the Cloud DB service page in AppGallery Connect and export object type files.
(1) Click Export and select JSON to export the JSON file to the Assets\Plugins\Android directory of your Unity project.
(2) Click Export, select JAVA and android, and set the package name to export the ZIP file. Then, decompress the file to the Assets\Plugins\Android directory.
6. Generate a Cloud DB file.
In Unity, choose CloudDB Kit > CloudDB Setting from the menu bar.
1) JavaCode Package Name: package name set when the Java file is exported.
2) ObjectType JsonFile Path: path of the exported JSON file, for example, Assets\Plugins\Andriod\model.json.
3) Namespace: namespace used by the generated C# file.
4) Export Path: path for storing the exported C# file.

5. Using Cloud DB
1. Choose GameObject > UI > Button and create some buttons.
Click a button, click Add Component on the right, and create a script file. The page layout is as follows.

2. Add the following code for integration:
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()
{
}
}
3. Integrate Auth Service to use the anonymous user authentication function.
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
1. Initialize and create object types.
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. Call openCloudZOne to open a storage zone.
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());
}));
}
3. Insert data.
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());
}));
}
4. Query, process, and display data.
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
1) Choose File > Build Settings. On the displayed packaging page, click Android under Platform.
2) Select a device, and click Build And Run.
3) You will then be able to view the Unity logs in Logcat for Android.

You will be able to see the inserted and deleted data in AppGallery Connect.
For more details, please refer to:
Cloud DB sample code:
https://github.com/AppGalleryConnect/agc-demos/tree/main/Android/agc-clouddb-demo-java