如何使用Unity切换和切换组以将切换组中的选定切换保存为播放器首选项?

时间:2016-11-05 18:47:06

标签: c# unity5 togglebutton

我有一个带输入字段和切换组的场景我想在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>());
    }
    }

1 个答案:

答案 0 :(得分:0)

试试这段代码:

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