简体   繁体   English

将标签分配给统一的对象

[英]Assign a tag to an object in a unity

I'm making a subway simulator, I want the StationBox to always have the Created tag after pressing a button, but it goes back to the previous value when the game is restarted, how can I solve this?我正在制作地铁模拟器,我希望StationBox在按下按钮后始终具有Created标签,但是当游戏重新启动时它会恢复到以前的值,我该如何解决这个问题?

Update: I will have more than 1 station, I would like to make a universal script for all更新:我将有不止 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";
        }
    }
}

If I understand correctly, you are trying to find a way to save game data so as not to lose it when you restart the game.如果我理解正确,您正在尝试找到一种方法来保存游戏数据,以免在重新启动游戏时丢失它。 To do this the most practical solution is PlayerPrefs.为此,最实用的解决方案是 PlayerPrefs。 You can find a lot of documentation online because it's a simple concept.你可以在网上找到很多文档,因为这是一个简单的概念。 However for your problem you can do this:但是,对于您的问题,您可以这样做:

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”;
    }

I hope I have helped you.我希望我对你有所帮助。 If so, you can thank me by marking this answer as accepted :)如果是这样,您可以通过将此答案标记为已接受来感谢我:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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