【问题标题】:Accessing the Properties in Unity3D在 Unity3D 中访问属性
【发布时间】:2017-07-13 10:45:12
【问题描述】:

我想问为什么要使用'sphere'来访问script1中的sphere.transform.position的transform.position。但是在script2中,我们可以直接评估transform.Rotate之前没有任何对象,为什么不写成:sphere.transform.Rotate

脚本1

void Update () {

}
private IEnumerator SphereIndicator(Vector3 pos)
{
    GameObject sphere = GameObject.CreatePrimitive (PrimitiveType.Sphere);
    sphere.transform.position = pos;
    yield return new WaitForSeconds (1);
    Destroy(sphere)
}

脚本2

void Update () {
    if (axes == RotationAxes.MouseX) {
        transform.Rotate (0, Input.GetAxis ("Mouse X") * sensitivityHor, 0);
    } 

【问题讨论】:

  • Script2 修改当前父对象的旋转变换。 Script1 将修改与该特定脚本无关的对象的旋转变换。这不是关于 Unity 的问题,而是关于 OOP 基础的问题。

标签: c# unity3d


【解决方案1】:

如您所见here.transform 允许您访问游戏对象的变换组件。如果您只使用transform,则与使用this.transform 相同,它将返回附加到调用者脚本附加到的同一个游戏对象的Transform 组件。

因此,在您的示例中,在 srcipt1 中,您从不同的游戏对象调用 Transform 组件,但在 script2 中,您正在调用相同游戏对象的组件.

【讨论】:

    猜你喜欢
    • 2020-04-18
    • 2014-01-03
    • 2017-02-12
    • 1970-01-01
    • 1970-01-01
    • 2011-11-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多