2016-08-21 29 views
0

我在計算如何收集用戶輸入並將其保存以供以後使用時遇到問題。我找不到堆棧溢出的類似問題,並且我已經請求其他程序員尋求幫助,但無濟於事。Unity Csharp - 保存用戶輸入以備後用?

希望有人在這裏知道。在一個場景中,您可以輸入您選擇的任何輸入,並顯示您所寫的內容。這裏是我的代碼:

using UnityEngine; 
    using System.Collections; 
    using UnityEngine.UI; 
    public class TextInput : MonoBehaviour { 
InputField input; 
InputField.SubmitEvent se; 
public Text output; 
//public static string[] tasks = new string[] 


//variable of array of strings so that you can save output text 
void Start() { 

    input = gameObject.GetComponent<InputField>(); 
    se = new InputField.SubmitEvent(); 
    se.AddListener(SubmitInput); 

    input.onEndEdit = se; 

} 


public void SubmitInput(string arg0) 
{ 


    string currentText = output.text; 
    string newText = currentText + arg0 + " has been added to your schedule." + "\n"; 
    output.text = newText; 
    input.text = ""; 

    // save currentTezt for later use outline 
    //tasks.add(currentText) 

    /*Want to make each new text input equal a different amount of time 
    */ 



} 

//Need public array so that you can choose from the different strings --- you need the 'append' so that you can continue adding new tasks 

}

我將如何延續的輸入,這樣我可以使用它,並在接下來的場景顯示。我很抱歉,如果答案非常簡單,我仍然在學習,我希望能夠變得更好。再次感謝你。

回答

0

你看過使用DontDestroyOnLoad嗎?它的文檔被發現here

關於統一論壇的問題類似於您的問題here

0

由統一(*)所建議的解決方案:

  1. 創建一個遊戲物體,優選在根,例如給它一個名稱「持久數據」
  2. 將[DontDestroyOnLoad]放置在文件頂部「類」部分的正上方,該部分告訴Unity保留所有場景
  3. 創建組件以存儲數據。通常,爲要保存的每組數據創建一個不同的數據 - 例如, 「DataFromPlayerCreation」,「DataFromWinningTheGame」
  4. 根據需要添加儘可能多的字段。 DataFromPlayerCreation.UserInput(你沒有descrive您輸入的是什麼,所以我不知道,如果它是一個名,密碼,等等)
  5. 附上這些組件的上述1
  6. 使用Application.LoadLevel正常 - 你從上面1.的對象是唯一不會被銷燬,並將被拖入下一個場景

(*) - 注:這是一個非常糟糕的做法,但它是唯一的一個由Unity開箱即用支持。這就是爲什麼它是官方的。

0

您可以使用DontDestroyOnload與遊戲物體如在其他的答案,或者您可以使用PlayerPrefs:

public void SubmitInput(string arg0) 
{ 


    string currentText = output.text; 
    string newText = currentText + arg0 + " has been added to your schedule." + "\n"; 
    output.text = newText; 
    input.text = ""; 

    PlayerPrefs.SetString("Text", currentText); 
} 


void GetCurrentText() 
{ 
    string currentText = PlayerPrefs.GetString("Text", null); 
    if(currenttext != null){ 
     // Do something with currentText 
    } 
}