Home > other >  Is there a more simpler way to do this? Such as maybe using a ForLoop?
Is there a more simpler way to do this? Such as maybe using a ForLoop?

Time:07-30

I have 10 InputFields. I wish to have its data saved when the user or player exits the app or switches Scenes. I proceeded with setting the data or text of the InputFields to a string that gets stored via PlayerPrefs via SetString and on Start the script sets the InputFields to the string of the PlayerPrefs via GetString. This is my code:

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

public class Saving : MonoBehaviour
{
public TMP_InputField dumbellPressData;
string dumbellPressString;

public TMP_InputField inclineDumbellPressData;
string inclineDumbellPressString;

public TMP_InputField peckDeckData;
string peckDeckString;


// Start is called before the first frame update
void Start()
{
    dumbellPressString = PlayerPrefs.GetString("DumbellPressData");
    dumbellPressData.text = dumbellPressString;

    inclineDumbellPressString = PlayerPrefs.GetString("InclineDumbellPressData");
    inclineDumbellPressData.text = inclineDumbellPressString;

    peckDeckString = PlayerPrefs.GetString("PeckDeckData");
    peckDeckData.text = peckDeckString;
}

// Update is called once per frame
void Update()
{
    dumbellPressString = dumbellPressData.text;
    PlayerPrefs.SetString("DumbellPressData", dumbellPressString);

    inclineDumbellPressString = inclineDumbellPressData.text;
    PlayerPrefs.SetString("InclineDumbellPressData", inclineDumbellPressString);

    peckDeckString = peckDeckData.text;
    PlayerPrefs.SetString("PeckDeckData", peckDeckString);
}

}

My question is whether there is an easier way or a more simplified way to go abouts coding this. In the code above I merely did them for 3 InputFields and I still have 7 more to fill out. Is copy pasting them the only way to go abouts this? Maybe if I made an array of InputFields and an array of Strings to assign the Inputfields to before storing it via PlayerPrefs?

CodePudding user response:

There doesn't seem to be any need to actually create fields in your code. A dictionary should suffice.

Try this:

public class Saving : MonoBehaviour
{
    private Dictionary<string, TMP_InputField> _data = new Dictionary<string, TMP_InputField>()
    {
        { "DumbellPressData", new TMP_InputField() },
        { "InclineDumbellPressData", new TMP_InputField() },
        { "PeckDeckData", new TMP_InputField() },
    };

    void Start()
    {
        foreach (var kvp in _data)
            kvp.Value.text = PlayerPrefs.GetString(kvp.Key);
    }

    void Update()
    {
        foreach (var kvp in _data)
            PlayerPrefs.SetString(kvp.Key, kvp.Value.text);
    }
}

Or perhaps this:

public class Saving : MonoBehaviour
{
    private string[] _fields =
    {
        "DumbellPressData", "InclineDumbellPressData", "PeckDeckData"
    };
    
    private Dictionary<string, TMP_InputField> _inputs;
    
    public Saving()
    {
        _inputs = _fields.ToDictionary(x => x, _ => new TMP_InputField());
    }

    void Start()
    {
        foreach (var field in _fields)
            _inputs[field].text = PlayerPrefs.GetString(field);
    }

    void Update()
    {
        foreach (var field in _fields)
            PlayerPrefs.SetString(field, _inputs[field].text);
    }
}

The second way is my preferred way. It introduces a bit more code, but it makes configuration slightly easier and the executing code a little clearer to read.

CodePudding user response:

class XXXValue
{
    public string key;
    private string _value;
    public string value{get{return _value;}set{_value = value; save or cache...;}}
}

XXXValue value;

ui_text_component.on_value_changed  = (v){ value.value = v; }

u can save it once it changed

or u can cache them and flush them at some time point

  • Related