从Realm(mongodb)中在Unity C#中获取游戏级别数据的结果。

huangapple go评论76阅读模式
英文:

Get Results in Unity c# from Realm ( mongodb ) for game level data

问题

以下是您要翻译的内容:

So I am a php develeoper learning unity game development. So all this is quite new to me and I am sure this question has been asked before but I just cant understand what do do..

I Have the following realm instantce set up to keep track of players level data called level stats. Its all working fine but I need to know how to get a list of all rows in the leveldata and then loop through them.

Here is my realm instance :

using Realms;

public class LevelStats : RealmObject
{

[PrimaryKey]
public string Level { get; set; }

public RealmInteger<int> Score { get; set; }

public LevelStats() { }

public LevelStats(string Level, int Score)
{
this.Level = Level;
this Score = Score;
}

}

I then add some sample level data for testing when the app loads :

public class LevelScore : MonoBehaviour
{

private Realm _realm;
private LevelStats _levelStats;

void Start()
{
_realm = Realm.GetInstance();

_realm.Write(() => {
_levelStats = _realm.Add(new LevelStats("1", 100));
_levelStats = _realm.Add(new LevelStats("2", 2000));
_levelStats = _realm.Add(new LevelStats("3", 1500));
_levelStats = _realm.Add(new LevelStats("4", 5000));
});

if (_levelStats is null) // if null create blank table
{
Debug.Log("created row");
_realm.Write(() => {
_levelStats = _realm.Add(new LevelStats("1", 0));
});
}
Debug.Log("created row");
}
}

Then on my level screen I call the following c# script to display all the Level Data stored in the instance of realm

// Start is called before the first frame update
void Start()
{
_realm = Realm.GetInstance();

var theLevelData = _realm.All<LevelStats>();

///// I want to code a loop here for all the rows
/// foreach() or while() loop or something like that but cant figure it out

Debug.Log("Level: " + level + "Score:  " + Score);
}

Any pointers would be a great help !
英文:

So I am a php develeoper learning unity game development. So all this is quite new to me and I am sure this question has been asked before but I just cant understand what do do..

I Have the following realm instantce set up to keep track of players level data called level stats. Its all working fine but I need to know how to get a list of all rows in the leveldata and then loop through them.

Here is my realm instance :

using Realms;
public class LevelStats : RealmObject
{
[PrimaryKey]
public string Level { get; set; }
public RealmInteger<int> Score { get; set; }
public LevelStats() { }
public LevelStats(string Level, int Score)
{
this.Level = Level;
this.Score = Score;
}
}

I then add some sample level data for testing when the app loads :

public class LevelScore : MonoBehaviour
{
private Realm _realm;
private LevelStats _levelStats;
void Start()
{
_realm = Realm.GetInstance();
_realm.Write(() => {
_levelStats = _realm.Add(new LevelStats("1", 100));
_levelStats = _realm.Add(new LevelStats("2", 2000));
_levelStats = _realm.Add(new LevelStats("3", 1500));
_levelStats = _realm.Add(new LevelStats("4", 5000));
});
if (_levelStats is null) // if null create blank table
{
Debug.Log("created row");
_realm.Write(() => {
_levelStats = _realm.Add(new LevelStats("1", 0));
});
}
Debug.Log("created row");
}
}

Then on my level screen I call the following c# script to display all the Level Data stored in the instance of realm

    // Start is called before the first frame update
void Start()
{
_realm = Realm.GetInstance();
var theLevelData = _realm.All<LevelStats>();
///// I want to code a loop here for all the rows
/// foreach() or while() loop or something like that but cant figure it out 
Debug.Log("Level: " + level + "Score:  " + Score);
// Debug.Log(levels.Level);
}

Any pointers would be a great help !

答案1

得分: 1

A regular foreach loop will do the trick:

使用普通的foreach循环即可:

If you need index-based access, you can use LINQ:

如果需要基于索引的访问,可以使用LINQ:

Or you can cast the result to IRealmCollection, which looks more like a readonly list:

或者您可以将结果转换为`IRealmCollection`,它看起来更像是一个只读列表:

Note that there are no performance benefits of using one or the other - the IQueryable returned by _realm.All<..> is implementing most common LINQ methods (including ElementAt and Count) so those are O(1) operations and will not iterate the entire collection.

请注意,使用其中一个与另一个没有性能优势 - _realm.All<..> 返回的IQueryable 实现了大多数常见的LINQ方法(包括ElementAtCount),因此这些操作的时间复杂度为O(1),不会遍历整个集合。

英文:

A regular foreach loop will do the trick:

var theLevelData = _realm.All&lt;LevelStats&gt;();
foreach (var data in theLevelData)
{
Console.WriteLine($&quot;Level: {data.Level} - Score: {data.Score}&quot;)
}

If you need index-based access, you can use LINQ:

var thirdLevel = theLevelData.ElementAtOrDefault(2);
var levelsCount = theLevelData.Count();

Or you can cast the result to IRealmCollection, which looks more like a readonly list:

var levelsCollection = theLevelData.AsRealmCollection();
var thirdLevel = levelsCollection[2];
var levelsCount = levelsCollection.Count;

Note that there are no performance benefits of using one or the other - the IQueryable returned by _realm.All&lt;..&gt; is implementing most common LINQ methods (including ElementAt and Count) so those are O(1) operations and will not iterate the entire collection.

huangapple
  • 本文由 发表于 2023年3月15日 19:22:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/75744030.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定