繁体   English   中英

将标签分配给统一的对象

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

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