不能在Unity中继承ConfigurableJoint

问题描述 投票:1回答:2

我有一个配置的Configurable Joint,我在Unity3d项目中经常使用,所以我决定将它的配置实现为ConfigurableJoint的子类,如图所示;

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class FollowJoint : ConfigurableJoint {

    // Use this for initialization
    void Start () {
        this.anchor = Vector3.zero;
        this.axis = new Vector3 (1, 0, 0);
        this.connectedAnchor = Vector3.zero;
        this.secondaryAxis = new Vector3 (0, 1, 0);
        xMotion = ConfigurableJointMotion.Limited;
        yMotion = ConfigurableJointMotion.Limited;
        zMotion = ConfigurableJointMotion.Limited;
        angularXMotion = ConfigurableJointMotion.Locked;
        angularYMotion = ConfigurableJointMotion.Locked;
        angularZMotion = ConfigurableJointMotion.Locked;

        linearLimitSpring.spring = 100;
        linearLimitSpring.damper = 1;
        linearLimit.limit = 0;
        linearLimit.bounciness = 0;
        linearLimit.contactDistance = 0;

    }
}

但是,这给了我错误“FollowJoint:无法从密封类型`UnityEngine.ConfigurableJoint'派生”

如果这是不可能的,为什么?什么是一个好的替代策略(除了实例化可配置的联合并通过代码配置它)?

c# unity3d unity5
2个回答
1
投票

正如你所提到的,问题是如下所述:

FollowJoint:无法从密封类型`UnityEngine.ConfigurableJoint派生

在C#中,密封类型不能继承。

sealed (C# Reference)看:

应用于类时,sealed修饰符会阻止其他类继承它。

How to get around this

要解决这个问题,您可以创建一个工厂函数,以便重新创建它:

public static ConfigurableJoint MakeJoint() {
    var joint = new ConfigurableJoint();

    joint.anchor = Vector3.zero;
    joint.axis = new Vector3 (1, 0, 0);
    joint.connectedAnchor = Vector3.zero;
    joint.secondaryAxis = new Vector3 (0, 1, 0);

    // ...

    return joint;
}

// Use factory each time you need the same
var jointA = MakeJoint();
var jointB = MakeJoint();

您还可以通过更改函数的签名来获取参数,并使用参数来设置返回的ConfigurableJoint的参数。例如,可以添加固定的secondaryAxis而不是固定的var joint = MakeJoint(secondaryAxis:new Vector3(0, 0, 1)); ,以便它可以像:

here

阅读有关可选和命名参数AddComponent的更多信息


2
投票

ConfigurableJoint是一个密封的类,这意味着你不能从它继承。

ConfigurableJoint也是一个组件,这意味着您可以使用GetComponentFollowJoint函数简单地完成您要查找的内容,该函数将ConfigurableJoint脚本附加到GameObject的组件添加或获取。

只需对Awake类进行包装,然后继承。使用ConfigurableJoint函数初始化变量。

1.如果您在编辑器中配置了GetComponent,请使用public class CustomConfigurableJoint : MonoBehaviour { protected ConfigurableJoint configurableJoint; protected virtual void Awake() { configurableJoint = GetComponent<ConfigurableJoint>(); } } 函数来访问它:

CustomConfigurableJoint

然后你可以继承public class FollowJoint : CustomConfigurableJoint { protected override void Awake() { base.Awake(); } void Start() { configurableJoint.xMotion = ConfigurableJointMotion.Limited; } } 类并像这样使用它:

ConfigurableJoint

2.现在,如果你想从代码创建和配置AddComponent,使用Awake函数创建它的新实例,然后在public class CustomConfigurableJoint : MonoBehaviour { protected ConfigurableJoint configurableJoint; protected virtual void Awake() { //Create it configurableJoint = gameObject.AddComponent<ConfigurableJoint>(); //Modify it below configurableJoint.xMotion = ConfigurableJointMotion.Limited; configurableJoint.yMotion = ConfigurableJointMotion.Limited; configurableJoint.zMotion = ConfigurableJointMotion.Limited; configurableJoint.angularXMotion = ConfigurableJointMotion.Locked; configurableJoint.angularYMotion = ConfigurableJointMotion.Locked; configurableJoint.angularZMotion = ConfigurableJointMotion.Locked; } } 函数中修改它:

qazxswpoi
© www.soinside.com 2019 - 2024. All rights reserved.