简体   繁体   English

Unity中的脚本参考

[英]Script Reference in Unity

I'm creating a game that have multiples references to scripts, one to another. 我正在创建一个具有多个脚本引用的游戏。

For exemple: 举个例子:

  1. Controller script 控制器脚本
  2. SomeBehavior script SomeBehavior脚本
  3. GUI script GUI脚本

My doubt enter when I can do two different things: 当我可以做两种不同的事情时,我的怀疑就进入了:

  1. In SomeBehavior access/edit GUI variable/function that I whant... Like: 在我所希望的SomeBehavior访问/编辑GUI变量/函数中,...

    1.1. 1.1。 variable = GameObject.Find("GuiTag").GetComponent< GUI script >(); 变量= GameObject.Find(“ GuiTag”)。GetComponent <GUI脚本>();
    variable.score.... and go on... variable.score ....然后继续...

  2. In SomeBehavior access/edit GUI variable/function by Controller.gui (gui a public variable on controller script)... Centralization all the base scripts to one, in that way, having less variables in the scripts, less memory (perraps)... 在Controller.gui(gui控制器脚本上的公共变量)的SomeBehavior访问/编辑GUI变量/功能中...将所有基本脚本集中到一个,这样,脚本中的变量较少,内存(perraps)较少。 ..

    2.1. 2.1。 variable = GameObject.Find("ControllerTag").GetComponent< Controller script >(); 变量= GameObject.Find(“ ControllerTag”)。GetComponent <控制器脚本>();
    variable.gui.score.... variable.gui.score ....

Which one is better? 哪一个更好?

In the Scripts that you will only have one of you can set a public static variable in that class with a public get and private set. 在只有一个脚本的脚本中,可以使用公共get和private集在该类中设置公共静态变量。 On the Start or Awake function you set that variable to this and then you can access that script easily from any other scripts. 在“启动”或“唤醒”功能上,将该变量设置为此,然后可以从任何其他脚本轻松访问该脚本。

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

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