unity3d 当脚本滑入检查器字段时获取脚本的检查器选项卡

k5ifujac  于 2023-01-31  发布在  其他
关注(0)|答案(1)|浏览(137)

以下是我的问题的所有要素:我有一个“ShotPattern”类,它应该定义一个将由枪支射击的子弹模式。
代码中的放炮模式:

[System.Serializable]
public class ShotPattern : MonoBehaviour
{
[Header("Projectile info")]
[SerializeField]
private GameObject projectile;
[SerializeField]
private AMovementPattern projectileMovementPattern;

    [Header("Pattern shape info")]
    [SerializeField]
    private Vector2 startVector = Vector2.zero;
    [SerializeField]
    private Vector2 endVector = Vector2.zero;
    [SerializeField]
    private int projectileAmount = 1;
    [SerializeField]
    private float timeBetweenShots = 0;
    [SerializeField]
    private float speedIncrementBetweenShots = 0;
    
    private void Start()
    {
        Shoot(transform.position);
    }
    
    public virtual void Shoot(Vector3 origin)
    {
        float angleDelta = (Vector2.SignedAngle(startVector,endVector)) / projectileAmount;
        for (int i = 0 ; i < projectileAmount ; i++)
        {
            GameObject currentProjectile = Instantiate(projectile,origin,quaternion.identity);
            StraightMovePattern currentMovePattern = currentProjectile.AddComponent<StraightMovePattern>();
            currentMovePattern.direction = startVector.normalized;
        }
    }

}

检查器中的放炮模式

我也有一些类定义的运动模式,为产生的炮弹(移动直线,正弦波等),这些类都继承自“AMovementPattern”类。
代码中的AMovementPattern抽象类:

[RequireComponent(typeof(Rigidbody2D))]
public abstract class AMovementPattern : MonoBehaviour
{
    /**
     * Direction of the movement pattern. (is down by default since we assume movement patterns are mostly used by enemies)
     */
    [Header("General movement info")]
    public Vector2 direction = Vector2.down;
    
    /**
     * Speed of the movement pattern.
     */
    [SerializeField]
    protected float speed = 1f;
    
    protected Rigidbody2D rb;
    
    
    protected virtual void Start()
    {
        rb = GetComponent<Rigidbody2D>();
    }
    
    protected virtual void FixedUpdate()
    {
        MovementUpdate();
    }

    /**
     * Method to calculate the new position of the object based on the movement pattern and previous position.
     */
    protected virtual void MovementUpdate()
    {
        throw new NotImplementedException();
    }
}

代码中的移动模式子类:

public class StraightMovePattern : AMovementPattern
{
    protected override void MovementUpdate()
    {
        rb.velocity = direction * speed;
    }
}

Inspector中的移动模式子类

从ShotPattern inspector选项卡中可以看出,我希望能够给予ShotPattern编辑器一个MovementPattern脚本,以便添加到示例化时生成的投射物中。Unity不允许我将MovePattern子类之一滑动到“AMovePattern”字段中,我猜这是因为该字段需要这些类的一个示例,并已设置其参数。有没有一种方法,使它,使我可以滑动任何移动模式脚本在那里,并有相应的检查员标 checkout 现?
我知道我可以制作一个空的游戏对象来保存一个移动模式脚本,并在这个游戏对象编辑器中初始化该移动脚本的值,但这有点违背了在检查器中使用它的目的,在检查器中我可以动态编辑整体模式来测试不同的组合,所以我想避免这种情况。

bwntbbo3

bwntbbo31#

我建议添加这些更改,使MovementUpdate像这样抽象,因此您将需要始终实现此方法。

protected abstract void MovementUpdate();

第二种方法是将模式的数据和执行分离到两个独立的类中,然后像这样重做ShootPattern。

[Header("Projectile info")]
[SerializeField]
private Projectile projectile;
[SerializeField]
private AMovementPattern projectileMovementPattern;

    public virtual void Shoot(Vector3 origin)
    {
        float angleDelta = (Vector2.SignedAngle(startVector,endVector)) / projectileAmount;
        for (int i = 0 ; i < projectileAmount ; i++)
        {
            var currentProjectile = Instantiate(projectile,origin,quaternion.identity);
            currentProjectile.Init(projectileMovementPattern, startVector.normalized);
        }
    }

我建议您使用ScriptableObject来保存这样的数据。
来自Unity/C#的注解它与抽象类无关。

相关问题