简体   繁体   English

Unity 中的多次保存

[英]Multiple Saves in Unity

I am very new to Unity, and programming languages in general, so forgive my noobiness lol.我对 Unity 和一般的编程语言都很陌生,所以请原谅我的菜鸟哈哈。

I am making a notepad, and I have been trying to figure out how to get multiple notes as well as the title of the note.我正在制作一个记事本,我一直在试图弄清楚如何获取多个笔记以及笔记的标题。 I have a basic script for saving notes, which worked for one note, but I don't know how to save multiple notes.我有一个保存笔记的基本脚本,它适用于一个笔记,但我不知道如何保存多个笔记。 I even tested making 2 saves and wasn't able to.我什至测试了 2 次扑救,但没能成功。

Here's the script I had, full disclosure, I wrote half of it following a tutorial, the other half was my attempt at making multiple saves这是我的脚本,完全公开,我按照教程写了一半,另一半是我尝试进行多次保存

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

Sorry it's super sloppy, I just started Unity like a week ago.抱歉,这太草率了,我一周前才开始使用 Unity。 I'm guessing the problem is partly that the noteSave variable isn't global so it just get's reset between scenes.我猜问题的部分原因是 noteSave 变量不是全局的,所以它只是在场景之间重置。

Any help would be greatly appreciated!任何帮助将不胜感激!

You can use the note name to get the note that way you can save multiple notes.您可以使用笔记名称来获取笔记,这样您就可以保存多个笔记。

Here's an example这是一个例子

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

As you can see in this case you need the note's name to be set to get the note's context.正如您在这种情况下所看到的,您需要设置便笺的名称以获取便笺的上下文。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM