团结游戏管理员。脚本仅一次 [英] Unity game manager. Script works only one time

查看:164
本文介绍了团结游戏管理员。脚本仅一次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在做简单的游戏管理器。我有一个剧本,那会是游戏中的所有场景访问。我需要加载新的场景后,要检查它的变量的值。但我的code只能一次使用此脚本开始模拟对象之后,存在于所有的场景。哪里不对?为什么它不会加载一个新场景后whork?

I'm making simple game manager. I have a script, which will be accessible from all scenes in the game. And I need to check values of it's variables after loading new scene. But my code works only one time after starting simulation and object with this script exists in all scenes. What is wrong? Why it doesn't whork after loading a new scene?

推荐答案

第1步

请了一个名为preLOAD场景。确保这就是现场0 在构建管理。

Make a scene named "preload". Ensure this is scene 0 in Build Manager.

第2步

在preLOAD的场景只是有一个叫空游戏物体说_app。 简而言之 DontDestroyOnLoad 在'_app。

In the "preload" scene simply have an empty GameObject called say "_app". Simply put DontDestroyOnLoad on '_app'.

请注意 - 这是的整个项目唯一的地方的使用 DontDestroyOnLoad 。它就是这么简单。

Note - this is the only place in the whole project you use DontDestroyOnLoad. It's really that simple.

第3步

您必须,且只能,把你的常规的行为......之类的东西音效,配乐等..在_app。

You must, and can only, put your "general" behaviors ... things like sound effects, scoring, etc ... on "_app".

始终从preLOAD现场游戏。

这是令人难以置信的简单。

It's that incredibly simple.

注意的 - 你的应用程序将几乎肯定有早场景像扑通场景屏幕或菜单的场景你的无法的使用场景为preLOAD的一幕。你必须从字面上有preLOAD的场景。(在preLOAD场面会再加载启动画面场景,菜单场景,或什么的。))

(Note - your app will almost certainly have "early" scenes like a "splash scene" screen or a "menu" scene. You can not use that scene as the preload scene. You have to literally have a preload scene. (The preload scene would then load your splash-screen-scene, menu-scene, or whatever.))

您再有问题,很简单,寻找在您的任何场景,任何脚本说SoundEffects或游戏管理。幸运的是死很容易,它是code的一行:

You then have the problem of, quite simply, finding say "SoundEffects" or "GameManager" from any script in any of your scenes. Fortunately it is dead easy, it is one line of code:

Sound sound = Object.FindObjectOfType<Sound>();
Game game = Object.FindObjectOfType<Game>();

做,在唤醒对于任何需要它的脚本。

Do that in Awake for any script that needs it.

这是诚实的这么简单。这是所有有给它。

巨大的混乱的产生是因为中的完全错误的例子codeS在网上看到了100S的。此外,新的程序员不能相信正是这种容易。

Tremendous confusion arises because of the 100s of totally wrong example codes seen online. Also, new programmers "cannot believe" it is this easy.

这真的是如此简单 - 诚实

这是完全怪异的团结忘了一个内置的preLOAD现场添加到游戏引擎 - 某处附上您的系统,如SoundEffects,游戏管理器等,但他们没有;它只是那些奇怪的事情之一:您在任何项目中统一做的第一件事就是点击一次做出preLOAD场景

It's totally bizarre that Unity forgot to add a built-in "preload scene" to the game engine - somewhere to attach your systems like SoundEffects, GameManager, etc. But they didn't; it's just one of those weird things: the first thing you do in any Unity project is click once to make a preload scene.

DylanB问: 在开发过程中这是很烦人的,你有你点击播放之前每次点击的preLOAD的场景。这个问题能自动?当然,每个团队都有不同的方式来做到这一点。这里有一个简单的例子:

DylanB asks: During development it's quite annoying that you have to click to the preload scene every time before you click "Play". Can this be automated? Sure, every team has a different way to do this. Here's a trivial example:

// this should run absolutely first; use script execution order
// (of course normally never, ever, ever use script execution order,
// this is an unusual case - just for development.)
...
public class DevPreload:MonoBehaviour
 {
 void Awake()
  {
  GameObject check = GameObject.Find("__app");
  if (check==null)
   { UnityEngine.SceneManagement.SceneManager.LoadScene("_preload"); }
  }
 }

这篇关于团结游戏管理员。脚本仅一次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆