👨🏼💻Integrating Huawei AppGallery Connect to Unity: A Quiz Game -Part 2 Cloud DB
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.
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.
Now we will create object type with name GameScore.
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
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
We need to create same class with GameScore name and same properties.
Now we will implement sdk to our project. We have already auth and other parts so we will implement only clouddb and relateds.
And our link xml now be like this:
Firstly we need to initialize CloudDB.We will get instance.
After this we need to initialize object types. We need to negotiate our schema with cloud.
Then we will open CloudDB Zone.
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.
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.
Play one game and then check AppGallery Connect Console. We will see a record with our values.
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.
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.