简体   繁体   English

只显示一次对话框,关卡重启后不显示

[英]Show dialog only once, not after level restart

I have my own dialog system which is basically a gameobject with a collider. 我有自己的对话系统,基本上是带有对撞机的游戏对象。 After triggering collider, Canvas with a Text component should show up. 触发对撞机后,应显示具有“ Text组件的“ Canvas So far, this works. 到目前为止,这可行。 Now, I want to make it happen only once. 现在,我只想让它发生一次。 Here how its work: I start level trigger showing dialog. 这里是它的工作方式:我启动显示对话框的级别触发器。 Game is pretty hardcore so player probably will die. 游戏是非常顽固的,所以玩家可能会死。 When player dies, I call Application.LoadLevel(Application.loadedLevel); 玩家死亡时,我调用Application.LoadLevel(Application.loadedLevel); (so basically I restart level) (所以基本上我会重新启动级别)

If I use something like this in scrip with collider 如果我在对撞机中使用这样的东西

 private static bool firstTimeText = true;

 void OnTriggerEnter2D(Collider2D coll)
 {
     if (coll.tag == "Player" && firstTimeText)
     {
         GetComponent<BoxCollider2D>().enabled = false;
         firstTimeText = false;
     }
 }

everything works like a charm. 一切都像魅力。 EXCEPT if I make copy of this gameobject, static variable in script will "handle" that every instantion of this object will have firstTimeText on false after trigger dialog first time. 除了复制该游戏对象外,脚本中的静态变量将“处理”该对象的每个实例在首次触发对话框后将false设置为firstTimeText。 So basically I can use it only once. 所以基本上我只能使用一次。

So is there any sollution for making trigger which run only once and not reset after Application.LoadLevel ? 那么是否有任何解决方案可以使触发器仅运行一次,而在Application.LoadLevel之后不会重置?

Also this doesn't work 而且这不起作用

void Awake()
{
    DontDestroyOnLoad(transform.gameObject);
}

Consider just having a static class that will set a flag to indicate if the dialog should appear or not. 考虑只具有一个静态类,该类将设置一个标志来指示是否应显示对话框。 Note that I am not inheriting from MonoBehaviour here. 请注意,我这里不是从MonoBehaviour继承的。

public static class ApplicationData {
    public static bool ShowDialog = true;
}

Usage: 用法:

if (ApplicationData.ShowDialog)
{
    ShowDialog();
    ApplicationData.ShowDialog = false;
}

The static class will retain its values during the lifetime of your application. 静态类将在应用程序的生存期内保留其值。 So, it will retain the FALSE value even if you reload your scene. 因此,即使您重新加载场景,它也将保留FALSE值。

Static variables are globally identical - therefore if you need to have multiple gameobjects exhibiting this behavior, they'll need to hook into different static values in some way. 静态变量在全局上是相同的-因此,如果您需要多个游戏对象展现这种行为,则它们需要以某种方式挂接到不同的静态值中。

One way to do this would be to have a key, and then keeping a static list of all "keys" already displayed. 一种方法是拥有一个键,然后保留所有已显示的“键”的静态列表。 My recommendation would be a HashSet - thus 我的建议是一个HashSet因此

private static HashSet<string> firstTimeSet = new HashSet<string>();

public string singleRunKey;

void OnTriggerEnter2D(Collider2D coll)
{
    if (coll.tag == "Player"
        && firstTimeSet.Add(singleRunKey))
    { GetComponent<BoxCollider2D>().enabled = false; }
}

Note that .Add returns true if the item wasn't in the collection (and adds it), but false if it already was (and does not add it, because no duplicates). 请注意,如果该项目不在集合中(并添加) .Add将返回true ,但如果已经存在(则不添加,因为不会重复),则返回false All you have to do now is assign singleRunKey a unique value per object via the Inspector, and each one will run exactly once 您现在要做的就是通过Inspector为每个对象分配singleRunKey一个唯一的值,每个对象将只运行一次

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

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