简体   繁体   English

Unity3d将不同的脚本声明为“使用___”

[英]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 ,你需要给一个参考GameObjectFirstPersonController连接到(让我们称之为它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.

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