首页 > 解决方案 > 如何从另一个类启用 void bool?

问题描述

我尝试使用另一个类的 saveLevel (this.saveLevel(true);) ,但没有找到方法。

public void saveLevel(bool binary = true)
{
List<LevelEditor.ObjectsSource> list = new List<LevelEditor.ObjectsSource>();
if (this.validationResult.hasTDSotMObjects)
{
    list.Add(LevelEditor.ObjectsSource.DLC1);
}
this.levelData.objectSources = list.ToArray();
LevelEditor.levelDirPath = LevelEditor.createLevelDirIfNotExists(LevelEditor.levelDirPath);
string text = System.IO.Path.Combine(LevelEditor.levelDirPath, "Content");
Directory.CreateDirectory(text);
string path = "Level" + ((!binary) ? ".json" : ".sap");
string path2 = System.IO.Path.Combine(text, path);
LevelEditor.saveLevelData(this.levelData, path2, binary);

这是来自 LevelEditor 类,我试图在 Game 类中使用它,但不起作用

谢谢

谢谢

标签: c#unity3d

解决方案


您要么必须创建LevelEditorfirst 的实例,要么将其设为静态方法。查看static 关键字

var levelEditor = new LevelEditor();

// Methods should be CamelCased
levelEditor.SaveLevel(true);

或者

public class LevelEditor
{
    // Marked this method 'static'
    public static void SaveLevel(bool binary = true)
    {
        // implementation
    }
}

// call like this:
LevelEditor.SaveLevel(true);

推荐阅读