且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

统一游戏管理器.脚本只工作一次

更新时间:2023-02-16 12:56:16

在每个 Unity 项目中,您都必须有一个预加载场景.

Unity 没有内置"的预加载场景,这很令人困惑.

In every Unity project you must have A PRELOAD SCENE.

It is quite confusing that Unity does not have a preload scene "built-in".

他们将来会添加这个概念.

They will add this concept in the future.

Fort 现在你必须自己点击添加一个预加载场景.

Fort now you have to click to add a preload scene yourself.

幸运的是,预加载场景非常容易.

Fortunately, it is extremely easy to have a preload scene.

第一步.

制作一个名为预加载"的场景.它必须是构建管理器中的场景 0.

Make a scene named "preload". It must be scene 0 in Build Manager.

第 2 步.

在预加载"中场景创建一个名为__app"的空游戏对象.

In the "preload" scene make an empty GameObject called, say, "__app".

简单地说,将 DontDestroyOnLoad 放在__app"上.

Simply, put DontDestroyOnLoad on '__app'.

注意:

这是整个项目中唯一使用DontDestroyOnLoad的地方.

就这么简单.

在示例中:开发人员制作了一行 DDOL 脚本.

In the example: the developers have made a one-line DDOL script.

将该脚本放在__app"上对象.

Put that script on the "__app" object.

您再也不必考虑 DDOL.

You never have to think about DDOL again.

步骤 3

您的应用将具有(许多)一般行为".因此,例如数据库连接、音效、评分等.

Your app will have (many) "general behaviors". So, things like database connectivity, sound effects, scoring, and so on.

您必须并且只能将您的一般行为放在_app"上.

You must, and can only, put your general behaviors on "_app".

就这么简单.

一般行为 - 当然 - 在项目的任何地方、任何时间和所有场景中都可用.

The general behaviors are then - of course - available everywhere in the project, at all times, and in all scenes.

你还能怎么做?

在上面的图像示例中,请注意Iap"(应用内购买")和其他.

In the image example above, notice "Iap" ("in-app purchase") and the others.

您所有的通常需要的行为"- 音效、得分等 - 就在那个物体上.

All of your "generally-needed behaviors" - sound effects, scoring, and so on - are right there on that object.

这意味着——当然,自然——

This means that - of course, naturally -

您可以使用 Unity 的所有常用功能,这些功能可用于其他所有游戏对象.检查器变量、拖拽连接、设置等.

You can use all the usual features of Unity, which you use on every other game object. Inspector variables, drag to connect, settings, and so on.

(事实上:假设您被聘用从事现有项目.您要做的第一件事就是看一眼预加载场景.您将看到预加载场景中的所有一般行为" - 音效、评分、AI 等.您将立即看到所有设置,例如检查器变量...语音音量、Playstore ID 等)

(Indeed: say you've been hired to work on an existing project. The first thing you will do, is glance at the preload scene. You will see all the "general behaviors" in the preload scene - sound effects, scoring, AI, etc etc. You will instantly see all the settings for those things as Inspector variables ... speech volume, playstore ID, etc etc.)

这是音效"示例一般行为:

Here's an example "Sound effects" general behavior:

看起来还有一个画外音"一般行为和音乐"一般行为".

Looks like there's also a "voice over" general behavior, and a "music" general behavior".

重复.关于你的一般行为".(音效、得分、社交等)这些只能在预加载场景中的游戏对象上运行.

To repeat. Regarding your "general behaviors". (Sound effects, scoring, social, etc etc.) These CAN ONLY GO on a game object in the preload scene.

就这么简单.

有时来自其他环境的工程师会注意到这一点,因为这似乎没那么容易".

Sometimes engineers coming from other environments get caught up on this, because it seems like "it can't be that easy".

再说一遍,Unity 只是简单地忘记了内置"预加载场景.因此,您只需单击即可添加预加载场景.不要忘记添加 DDOL.

To repeat, Unity just plain forgot to "build-in" a preload scene. So, you simply click to add your preload scene. Don't forget to add the DDOL.

所以,在开发过程中:

始终从预加载场景开始游戏.

就这么简单.

重要提示:您的应用肯定会早"场景.示例:

Important: Your app will certainly have "early" scenes. Examples:

  • 启动画面"
  • 菜单"

注意.你不能使用启动画面或菜单作为预加载场景.你必须有一个单独的预加载场景.

预加载场景然后将加载您的启动画面或菜单或其他早期场景.

The preload scene will then load your splash or menu or other early scene.

所以你有一个预加载场景.

So you have a preload scene.

您所有的一般行为"只是在预加载场景中.

All of your "general behaviors" are simply on the preload scene.

接下来的问题很简单,就是找到声音效果".

You next have the problem of, quite simply, finding say "SoundEffects".

您必须能够从任何脚本、任何游戏对象、任何场景中轻松找到它们.

You have to be able to find them easily, from, any script, on any game object, in any of your scenes.

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

Awake 中为任何需要它的脚本执行此操作.

Do that in Awake, for any script that needs it.

老实说就是这么简单.这就是全部.

It's honestly that simple. That's all there is to it.

由于网上看到的数百个完全错误的代码示例,造成了巨大的混乱.

Tremendous confusion arises because of the 100s of absolutely wrong code examples seen online.

这真的很简单 - 诚实!

奇怪的是,Unity 忘记添加内置的预加载场景";- 某个地方可以连接您的系统,如 SoundEffects、GameManager 等.这只是 Unity 的一些奇怪之处之一.因此,您在任何 Unity 项目中做的第一件事就是单击一次以制作预加载场景.

It's bizarre that Unity forgot to add a built-in "preload scene" - somewhere to attach your systems like SoundEffects, GameManager, etc. It's just one of those weird thing about Unity. So, the first thing you do in any Unity project is just click once to make a preload scene.

就是这样!

请注意,如果您真的想输入更少的 (!) 行代码,这非常简单 - 您可以只使用全局来处理这些事情!

Note that, if you really want to type even less (!) lines of code, it's remarkably easy - you can just use a global for each of these things!

详细说明此处(您可以使用我流行的网格.cs 脚本)和下面的@Frits 回答.

This is explained in detail here (you can use my popular Grid.cs script) and in @Frits answer below.

(无论您是使用全局变量,还是在需要该特定变量的每个组件中都有一个局部变量,这只是代码风格的问题.在非常不寻常的情况下,全局"方法可能是高效的(就像任何全球))

(Whether you use a global, or just have a local variable in each component that needs that particular one, is just a matter of code style. In very unusual situations the "global" approach may be performant (as with any global).)

DylanB 问道:在开发过程中每次点击播放"之前都必须点击预加载场景,这很烦人.这可以自动化吗?"

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 to do so.
// (of course, normally never use the script-execution-order feature,
// 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"); }
  }
 }

但不要忘记:你还能做什么?游戏必须从预加载场景开始.除了点击进入预加载场景,你还能做什么来开始游戏?有人可能会问启动 Unity 运行 Unity 很烦人——如何避免启动 Unity?!"当然,游戏绝对必须从预加载场景开始——否则怎么办?所以当然,您必须在单击播放之前单击预加载场景";在 Unity 中工作时 - 还能怎样?

But don't forget: what else can you do? Games have to start from a preload scene. What else can you do, other than click to go to the preload scene, to start the game? One may as well ask "it's annoying launching Unity to run Unity - how to avoid launching Unity?!" Games simply, of course, absolutely have to start from a preload scene - how else could it be? So sure, you have to "click to the preload scene before you click Play" when working in Unity - how else could it be?