[英]Assign a tag to an object in a unity
我正在制作地鐵模擬器,我希望StationBox
在按下按鈕后始終具有Created
標簽,但是當游戲重新啟動時它會恢復到以前的值,我該如何解決這個問題?
更新:我將有不止 1 個站,我想為所有人制作一個通用腳本
using System;
using UnityEngine.UI;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Linq;
public class AddStation : MonoBehaviour
{
public InputField Input;
public Text StationName;
public GameObject Button;
public GameObject CreateStation;
public GameObject StationBox;
public void OnMouseDown(){
if (Input.text != ""){
CreateStation.SetActive(false);
StationName.text = Input.text.ToString();
StationBox.tag = "Created";
}
}
}
如果我理解正確,您正在嘗試找到一種方法來保存游戲數據,以免在重新啟動游戲時丟失它。 為此,最實用的解決方案是 PlayerPrefs。 你可以在網上找到很多文檔,因為這是一個簡單的概念。 但是,對於您的問題,您可以這樣做:
public void OnMouseDown(){
if (Input.text != ""){
CreateStation.SetActive(false);
StationName.text = Input.text.ToString();
StationBox.tag = "Created”;
PlayerPrefs.SetInt(“Created”, 1);
}
}
void Start()
{
if (PlayerPrefs.HasKey(“Created”))
if (PlayerPrefs.GetInt(“Created”) == 1)
StationBox.tag = "Created”;
}
我希望我對你有所幫助。 如果是這樣,您可以通過將此答案標記為已接受來感謝我:)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.