【问题标题】:How do I write to Json file in C#? [closed]如何在 C# 中写入 Json 文件? [关闭]
【发布时间】:2020-03-08 12:18:12
【问题描述】:

我正在使用 Unity 制作游戏。我试图制作一个选项菜单,但我的应用按钮不起作用。我想这样做,以便将选项写入 Json 文件。

我试过了,但我得到了这个错误:

Assets/Scripts/SettingsManager.cs(48,39): error CS0117: 'JsonUtility' does not contain a definition for 'toJson'

这是 C# 文件:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System.IO;

public class SettingsManager : MonoBehaviour
{
    public Toggle fullscreenToggle;
    public Dropdown resolutionDropdown;


    public Resolution[] resolutions;
    public GameSettings gameSettings;
    public Button applyButton;

    void OnEnable()
    {
        gameSettings = new GameSettings();

        fullscreenToggle.onValueChanged.AddListener(delegate { OnFullscreenToggle(); });
        resolutionDropdown.onValueChanged.AddListener(delegate { OnResolutionChange(); });
        applyButton.onClick.AddListener(delegate { OnApplyButtonClick(); });

        resolutions = Screen.resolutions;
        foreach (Resolution resolution in resolutions)
        {
            resolutionDropdown.options.Add(new Dropdown.OptionData(resolution.ToString()));
        }
    }

    public void OnFullscreenToggle()
    {
        gameSettings.fullScreen = Screen.fullScreen = fullscreenToggle.isOn;
    }

    public void OnResolutionChange()
    {
        Screen.SetResolution(resolutions[resolutionDropdown.value].width, resolutions[resolutionDropdown.value].height, Screen.fullScreen);
    }

    public void OnApplyButtonClick()
    {
        SaveSettings();
    }
    public void SaveSettings()
    {
        string jsonData = JsonUtility.toJson(gameSettings, true);
        File.WriteAllText(Application.persistentDataPath + "/gamesettings.json",jsonData);
    }

    public void LoadSettings()
    {

    }
}

【问题讨论】:

标签: c# json unity3d


【解决方案1】:

你有一个错字。用大写T写ToJson

JsonUtility.ToJson

【讨论】:

  • 我们不在这里回答基于错字的问题。这个答案应该是一个评论,并且由于错字而导致问题关闭,因此对未来没有真正的帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-22
  • 1970-01-01
  • 1970-01-01
  • 2016-09-05
  • 1970-01-01
相关资源
最近更新 更多