英文:
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方法(包括ElementAt
和Count
),因此这些操作的时间复杂度为O(1),不会遍历整个集合。
英文:
A regular foreach loop will do the trick:
var theLevelData = _realm.All<LevelStats>();
foreach (var data in theLevelData)
{
Console.WriteLine($"Level: {data.Level} - Score: {data.Score}")
}
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<..>
is implementing most common LINQ methods (including ElementAt
and Count
) so those are O(1) operations and will not iterate the entire collection.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论