簡體   English   中英

我想編寫一個腳本,從中我可以將 name 變量用於 unity 2d 中的另一個場景

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM