Многократные Сохранения в Единстве - PullRequest
0 голосов
/ 21 апреля 2020

Я очень новичок в Unity и в целом в языках программирования, так что простите мою нелюбовь. Lol.

Я делаю блокнот, и я пытался выяснить, как получить несколько заметок, а также название заметки. У меня есть скрипт basi c для сохранения заметок, который работал для одной заметки, но я не знаю, как сохранить несколько заметок. Я даже протестировал создание 2 сохранений и не смог.

Вот мой сценарий, полное раскрытие, я написал половину его после урока, другая половина была моей попыткой сделать несколько сохранений

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

public class SaveControl : MonoBehaviour
{
    void Awake()
    {
        DontDestroyOnLoad(gameObject);
    }

public string theText1;
    public string theName1;
    public string theText2;
    public string theName2;
    public GameObject ourNote;
    public GameObject namePlaceHolder;
    public GameObject placeHolder;
    public GameObject customName;
    [SerializeField]
    public int noteSave = 1;


    public void LoadButton1()
    {
        {
            SceneManager.LoadScene(1);
            noteSave = 1;
            theText1 = PlayerPrefs.GetString("NoteContents1");
            theName1 = PlayerPrefs.GetString("theName1");
            placeHolder.GetComponent<InputField>().text = theText1;
            namePlaceHolder.GetComponent<InputField>().text = theName1;
            string customName = theName1;
        }
    }
    public void LoadButton2()
    {
        noteSave = 2;
        theText2 = PlayerPrefs.GetString("NoteContents2");
        theName2 = PlayerPrefs.GetString("theName2");
        placeHolder.GetComponent<InputField>().text = theText2;
        namePlaceHolder.GetComponent<InputField>().text = theName2;
        string customName = theName2;
        SceneManager.LoadScene(1);
    }

    public void OkayButton()
    {
        if (noteSave == 1)
            {
            string key = "NoteContents" + noteNumber;
            Debug.Log("key is '" + key + "'");
            theText1 = ourNote.GetComponent<Text>().text;
            theName1 = customName.GetComponent<Text>().text;
            PlayerPrefs.SetString("NoteContents1", theText1);
            print(theName1);
            SceneManager.LoadScene(0);
            }
        else if (noteSave == 2)
        {

            theText2 = ourNote.GetComponent<Text>().text;
            theName2 = customName.GetComponent<Text>().text;
            PlayerPrefs.SetString("NoteContents2", theText2);
            print(theName2);
            SceneManager.LoadScene(0);
        }

    }
}

Извините, это супер неряшливо, я только что запустил Unity как неделю go. Я предполагаю, что проблема отчасти в том, что переменная noteSave не является глобальной, поэтому она просто сбрасывается между сценами.

Любая помощь будет принята с благодарностью!

1 Ответ

0 голосов
/ 21 апреля 2020

Вы можете использовать имя заметки, чтобы получить заметку, таким образом, вы можете сохранить несколько заметок.

Вот пример

public void LoadButton() {
   // i assume this is your note's title/name
   string noteName = customName.GetComponent<Text>().text;
   string noteContent = PlayerPrefs.GetString("noteName");

   ourNote.GetComponent<Text>().text = noteContent
}

public void SaveButton() {
   string noteName = customName.GetComponent<Text>().text;
   string noteContent = ourNote.GetComponent<Text>().text;
   PlayerPrefs.SetString(noteName, noteContent);
   PlayerPrefs.Save();
}

Как вы можете видеть в этом случае, вам нужно установить имя заметки, чтобы получить контекст заметки.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...