👨🏼‍💻Integrating Huawei AppGallery Connect to Unity: A Quiz Game -Part 2 Cloud DB

Necati Semih Özer
Huawei Developers
Published in
4 min readNov 1, 2022

--

AppGallery Connect Cloud DB
Hello There

Hello there little padawan.

We will continue to our journey with Cloud DB. In this article we will add our game results to database. So maybe in later time we could do some statictics.

What is Cloud DB? Cloud DB is a device-cloud synergy database product that provides data synergy management capabilities between the device and cloud, unified data models, and various data management APIs.

If you do not how to open Huawei AppGallery Account and app you can visit first part of story from here

On the left sidebar select Build->Cloud DB then click Enable now in the upper right corner.

AGC CloudDB Service

After enabling CloudDB service now add CloudDB zone. Cloud DB zones are independent of each other. Each cloud DB zone contains object types of the latest version. Give clouddb zone name as SimpleQuizDE.

CloudDB Zone

Now we will create object type with name GameScore.

GameScore table

In table we have fields

id: primary key for each record

userid: user id of player who played game

score: players score

category: category of quiz

As seen user id column is encrypted. We do not want users id seen so we made it encrypted. As we made it encrypted we need so set data permission to only data creator and administrator

Data permission for GameScore

We have to export schema and put it under Assets/Resources folder. next to agconnect-services.json. We will give name to json UnityCloudDB.json

Object Types
Export popup

We need to create same class with GameScore name and same properties.

GameScore class

Now we will implement sdk to our project. We have already auth and other parts so we will implement only clouddb and relateds.

AGC SDK

And our link xml now be like this:

link.xml

Firstly we need to initialize CloudDB.We will get instance.

DBManager start method

After this we need to initialize object types. We need to negotiate our schema with cloud.

DBManager InitJsonObjects method

Then we will open CloudDB Zone.

DBManager OpenCloudDBZone method

We have encrypted column. So we need to set user key. If we do not set user key. We will get encrytion fail exception.

Now we can write our upsert and query function. As upsert we will take a object that inherited from CloudDBZoneObject class.Then if upsert operation is success we will return number upserted items.

DBManager ExecuteUpsert method

At the end of the game we create a GameScore object and give it to ExecuteUpsert method so we can insert our data to AppGallery cloud.

GameManager SetQuestion Method

Play one game and then check AppGallery Connect Console. We will see a record with our values.

Created data

ExecuteQuery method are creating a query with given fieldname equal to given value.We will list user scores at scores scene. So we will give userid fieldname and users id as value.

DBManager ExecuteQuery Method

Conclusion

We have implemented the AGC CloudDB service from the Huawei AppGallery Console for Unity into our app with ease since most of the hard work is handled by the SDK itself. Now, our app can provide upsert and list scores mechanism to users.

Congrats if you followed this tutorial till the end and made your app more convenient for you and your users.

Well Done

--

--

Necati Semih Özer
Huawei Developers

A Computer Engineer which love to build applications and touch people lifes.