[英]Unity3d declare different script as 'using ___'
I am working on a project that requires underwater physics, and from my WaterController.cs
script I need to get variables from the FirstPersonController.cs
script. 我正在一个需要水下物理学的项目上工作,我需要从WaterController.cs
脚本中获取FirstPersonController.cs
脚本中的变量。 I was wondering if I could put it at the top with "using" like using FirstPersonController.cs
or something along those lines. 我想知道是否可以像using FirstPersonController.cs
类的方式using FirstPersonController.cs
“ using”将其放在顶部。 Could someone tell me if I could do this and in what way, or if there is another way of getting variables from that script? 有人可以告诉我是否可以执行此操作以及以哪种方式执行此操作,或者是否存在从该脚本获取变量的另一种方法?
The FirstPersonController.cs
that comes with Unity3d's standard assets belongs to the UnityStandardAssets.Characters.FirstPerson
namespace so you need Unity3d的标准资产随附的FirstPersonController.cs
属于UnityStandardAssets.Characters.FirstPerson
命名空间,因此您需要
using UnityStandardAssets.Characters.FirstPerson;
at the top of any script that needs to use that class. 在需要使用该类的任何脚本的顶部。 Since you're trying to access one of its public member variables, you also need a reference to the instance of the FirstPersonController
that you're working with. 由于您尝试访问其公共成员变量之一,因此还需要引用您正在使用的FirstPersonController
实例。
In WaterController.cs
, you'll need a reference to the GameObject
that the FirstPersonController
is attached to (lets call it FPobj
) and then you can get that component like this: 在WaterController.cs
,你需要给一个参考GameObject
的FirstPersonController
连接到(让我们称之为它FPobj
),然后就可以得到该组件是这样的:
FPobj.GetComponent<FirstPersonController>(); // Returns null or the reference
If you're having trouble "getting a reference" to the GameObject
that the FirstPersonController
is attached to, try this: 如果您在“获取引用”到FirstPersonController
附加到的GameObject
遇到麻烦,请尝试以下操作:
Give WaterController
a public variable to hold it: 给WaterController
一个公共变量来保存它:
public class WaterController : MonoBehaviour {
public GameObject FPobj;
private FirstPersonController FPC;
void Start() {
FPC = FPobj.GetComponent<FirstPersonController>();
}
}
Now, when you click on the GameObject
that has the WaterController
attached, you will see a slot in Unity's Inspector for FPobj (becuase it's public
). 现在,当你点击GameObject
具有WaterController
连接,您将看到FPobj在Unity的检查器插槽(监守它是public
)。 You can drag your GameObject
that has FirstPersonController
attached from the Hierarchy into this slot and you should be good to go. 您可以将您的GameObject
已经FirstPersonController
从层次结构连接到这个插槽中,你应该是好去。 Now, anywhere in your WaterController
's functionality, you can use FPC
to get the variables you need. 现在,在WaterController
功能的任何位置,您都可以使用FPC
获取所需的变量。 Since they're all private
, you'll need to modify FirstPersonController.cs
if you'd like to access them: 由于它们都是private
,因此如果您要访问它们,则需要修改FirstPersonController.cs
:
float GetYRotation() { return m_YRotation(); } // or whatever variable you want.
There are also other options for getting the reference without dragging into the Hierarchy . 还有其他一些选择,可以在不拖入层次结构的情况下获取参考。 You can, for example, make use of GameObject.Find()
or GameObject.FindGameObjectWithTag()
. 例如,您可以使用GameObject.Find()
或GameObject.FindGameObjectWithTag()
。 If you go this route, make sure you still do it in Start()
only once and save the reference instead of calling it every time you need it... those two functions are pretty slow. 如果走这条路线,请确保仅在Start()
一次,并保存引用,而不是每次需要时都调用它……这两个函数非常慢。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.