我如何使用Unity切换和切换组将选择的切换保存在切换组中作为玩家偏好

本文关键字:存在 玩家 保存 选择 何使用 Unity | 更新日期: 2023-09-27 18:16:33

我有一个带有输入字段和切换组的场景,我想将玩家输入保存在playerprefs中,因为我将在连续的游戏会话中重复使用它。我还想从切换组中选择的切换的标签中获取文本。这是我到目前为止的代码。我有一个编译器错误,说我不能将guitext转换为字符串。我想我把输入字段字符串保存得很好是切换组很棘手。

    using UnityEngine;
    using System.Collections;
    using UnityEngine.UI;
    public class CreateHolder : MonoBehaviour {
    public InputField Input1;
    public InputField Input2;
    public InputField Input3;
    public InputField Input4;
    public ToggleGroup Discount;
    public ToggleGroup Params;
    public ToggleGroup Time;

    public void Grabdata() {
    PlayerPrefs.SetString("Offername", Input1.text);
    if (PlayerPrefs.HasKey("Offername") == true) 
    {
            Debug.Log("something has saved");
            Debug.Log(PlayerPrefs.GetString("Input1").ToString());
    }
    PlayerPrefs.SetString("Offerdesc", Input2.text);
    PlayerPrefs.SetString("Offeramount", Input3.text);
    PlayerPrefs.SetString("Offerpercent", Input4.text);
    PlayerPrefs.SetString("Tog1", Discount.GetComponent<GUIText>());
    PlayerPrefs.SetString("Tog2", Params.GetComponent<GUIText>());
    PlayerPrefs.SetString("Tog3", Time.GetComponent<GUIText>());
    }
    }

我如何使用Unity切换和切换组将选择的切换保存在切换组中作为玩家偏好

试试下面的代码:

 using UnityEngine;
    using System.Collections;
    using UnityEngine.UI;
    public class CreateHolder : MonoBehaviour {
    public InputField Input1;
    public InputField Input2;
    public InputField Input3;
    public InputField Input4;
    public ToggleGroup Discount;
    public ToggleGroup Params;
    public ToggleGroup Time;

    public void Grabdata() {
    PlayerPrefs.SetString("Offername", Input1.text);
    if (PlayerPrefs.HasKey("Offername") == true) 
    {
            Debug.Log("something has saved");
            Debug.Log(PlayerPrefs.GetString("Input1"));
    }
    PlayerPrefs.SetString("Offerdesc", Input2.text);
    PlayerPrefs.SetString("Offeramount", Input3.text);
    PlayerPrefs.SetString("Offerpercent", Input4.text);
    PlayerPrefs.SetString("Tog1", Discount.GetComponent<GUIText>().text);
    PlayerPrefs.SetString("Tog2", Params.GetComponent<GUIText>().text);
    PlayerPrefs.SetString("Tog3", Time.GetComponent<GUIText>().text);
    }
    }