[英]I want to write a script from which I can use the name variable into another scene in unity 2d
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class VisualNovelScript : MonoBehaviour
{
public static string InputName;
public GameObject InputField;
public void StoreName()
{
InputName = InputField.GetComponent<Text>().text;
}
}
這是我想要名稱變量的腳本。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class UserNameInput : MonoBehaviour
{
private static string InputNameShow;
public GameObject TextDisplay;
public void SetName()
{
InputNameShow = VisualNovelScript.InputName;
TextDisplay.GetComponent<Text>().text = "Hello " + InputNameShow;
}
}
這是我從第一個腳本中鏈接變量的腳本,我想顯示用戶輸入的名稱,過去幾天我嘗試了不同的可能性,但都沒有奏效。
我認為最好使用PlayerPrefs來完成這個任務,也不需要引用 GO 和 get 組件,你可以直接引用它的類型。 你的代碼應該是這樣的
public InputField InputField;
public void StoreName()
{
PlayerPrefs.SetString("Username", InputField.text);
PlayerPrefs.Save();
}
在你的其他場景中使用GetString
public Text TextDisplay;
public void SetName()
{
TextDisplay.text = "Hello " + PlayerPrefs.GetString("Username");
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.