Object and Queries unity
Storing, Updating, Retrieving and Deleting Data in a Moralis Server on Unity.

Objects

Create MoralisObject

1
//using namespaces
2
using Moralis.Platform.Objects;
3
using MoralisUnity.Platform.Queries;
4
using MoralisUnity;
5
using Newtonsoft.Json;
6
โ€‹
7
// code
8
public class Monster : MoralisObject
9
{
10
public int strength { get; set; }
11
public string Name { get; set; }
12
public bool canFly { get; set; }
13
// base class is required
14
public Monster() : base("Monster"){}
15
}
16
public async void SaveObjectToDB()
17
{
18
try {
19
Monster monster = await Moralis.Create<Monster>();
20
monster.strength = 1024;
21
monster.Name = "Aegon";
22
monster.canFly = true;
23
await monster.SaveAsync();
24
print("Created new object");
25
}
26
catch (Exception e){
27
print("Failed to create new object, with error code: " + e);
28
}
29
}
Copied!

Queries

Basic Queries

Queries provide a way to retrieve information from your Moralis database
1
//using namespaces
2
using Moralis.Platform.Objects;
3
using MoralisUnity.Platform.Queries;
4
using MoralisUnity;
5
using Newtonsoft.Json;
6
โ€‹
7
// code
8
public async void RetrieveObjectFromDB()
9
{
10
MoralisQuery<Monster> monster = await Moralis.Query<Monster>();
11
monster = monster.WhereEqualTo("Name", "Aegon");
12
IEnumerable<Monster> result = await monster.FindAsync();
13
foreach(Monster mon in result)
14
{
15
print("My strength is " + mon.strength + " and can i fly ? " + mon.canFly);
16
// output : My strength is 1024 and can i fly ? true
17
}
18
}
Copied!

Updating an Object

updating objects in the db
1
public async void updateObjectInDB()
2
{
3
MoralisQuery<Monster> monster = await Moralis.Query<Monster>();
4
monster = monster.WhereEqualTo("Name", "Aegon");
5
IEnumerable<Monster> result = await monster.FindAsync();
6
foreach(Monster mon in result)
7
{
8
mon.Name = "the_great_mage";
9
mon.strength = 6000;
10
await mon.SaveAsync();
11
Debug.Log("The monster is " + mon.Name + " with " + mon.strength + " strength");
12
// output : The monster is the_great_mage with 6000 strength
13
}
14
}
Copied!
you create a query instance
1
MoralisQuery<Monster> monster = await Moralis.Query<Monster>()
Copied!

Query params / Query constraints

you can search for different params like:
Query params / Query constraints

compound queries

1
MoralisQuery<Monster> monster = await Moralis.Query<Monster>();
2
MoralisQuery<Monster> monsterStrength = monster.WhereGreaterThanOrEqualTo("strength", 200 );
3
MoralisQuery<Monster> monstercanFly = monster.WhereEqualTo("canFly", false);
4
MoralisQuery<Monster> monster1 = Moralis.BuildOrQuery<PlayerData>(new MoralisQuery<PlayerData>[] { monsterName,monsterSkill }).OrderByDescending("strength");
5
IEnumerable<PlayerData> result = await monster1.FindAsync();
Copied!

Moralis User object

This is an object reserve for the _User class that handles user data in the dapp
1
MoralisUser user = Moralis.Create<MoralisUser>();
Copied!
OR
1
MoralisUser user = new MoralisUser();
2
// using the above is the same as using (MoralisUser user = Moralis.Create<MoralisUser>;)
Copied!
Last modified 2d ago