从 Realm ( mongodb ) 获取游戏级别数据的 Unity c# 结果

问题描述 投票:0回答:0

所以我是一个学习unity游戏开发的php开发者。所以这一切对我来说都是全新的,我相信这个问题之前已经被问过,但我只是不明白该做什么..

我设置了以下领域实例来跟踪称为级别统计的玩家级别数据。一切正常,但我需要知道如何获取 leveldata 中所有行的列表,然后遍历它们。

这是我的领域实例:

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;
    }

}

然后我添加一些样本级数据以在应用程序加载时进行测试:

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");
    }
}

然后在关卡屏幕上调用以下 C# 脚本来显示存储在 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);
   
}

任何指示都会有很大帮助!

c# unity3d realm
© www.soinside.com 2019 - 2024. All rights reserved.