【发布时间】: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()
{
}
}
【问题讨论】:
-
你试过
ToJson而不是toJson吗? because case matters