【问题标题】:How to distinguish between different objects using an interface如何使用接口区分不同的对象
【发布时间】:2018-07-05 11:20:51
【问题描述】:

我正在使用 raycast 控制器创建一个平台系统,该控制器使用一个界面来执行基于我的播放器当前碰撞的平台类型的不同任务。一些平台类型包括冰、可通行的块和泥泞的地面。

我想知道如何更好地优化我的代码,因为我目前每帧都调用 Unity 有点昂贵的“GetComponent()”函数,即使我从未在块之间进行更改。我想做的只是在我从一种类型的平台更改为不同类型的平台(即泥泞的地面 --> 冰)时调用 GetComponent(),但不知道如何使用接口来执行此操作。

我以为我可以使用枚举来比较类型,但你不能在接口中声明类型。

        if (hit)
        {
            //I'd only like to run this block of code if the type of platform changes
            var platform = hit.collider.gameObject.GetComponent<IPlatform>();

            State.IsCollidingWithPassablePlatform = platform.IsPassable;
            State.IsJumpBoosted = platform.IsJumpForce;
            State.IsBoosted = platform.IsForce;

            xForce = platform.XForce;
            yForce = platform.YForce;
            zForce = platform.ZForce;

            defaultParameters.accelerationTimeGrounded = platform.AccelerationTimeGrounded;
            defaultParameters.accelerationTimeAirborne = platform.AccelerationTimeAirborne;

接口示例:

interface IPlatform {

float AccelerationTimeGrounded { get; }
float AccelerationTimeAirborne { get; }

float XForce { get; }
float YForce { get; }
float ZForce { get; }

bool IsPassable { get; }

bool IsForce { get; }
bool IsJumpForce { get; }

冰台:

public class PlatformIce : MonoBehaviour, IPlatform {

public float AccelerationTimeGrounded { get { return accelerationTimeGrounded; } }
public float AccelerationTimeAirborne { get { return accelerationTimeAirborne; } }

public float XForce { get { return xForce; } }
public float YForce { get { return yForce; } }
public float ZForce { get { return zForce; } }

public virtual bool IsPassable { get { return false; } }

public bool IsForce { get { return false; } }
public bool IsJumpForce { get { return false; } }

[SerializeField]
private float accelerationTimeGrounded = 1.0f;
[SerializeField]
private float accelerationTimeAirborne = 3.0f;

private float xForce = 0;
private float yForce = 0;
private float zForce = 0;
}

【问题讨论】:

  • 我认为你应该创建一个类实现MonoBehaviour 类。因为 GetComponent&lt;T&gt;() 方法将在 gameObject 上构成,它实现了 Component

标签: c# unity3d types interface


【解决方案1】:

记住你的最后一个游戏对象并检查这个是否改变了

private lastGameObj;
[...]
if(lastGameObj!= hit.collider.gameObject) {
var platform = hit.collider.gameObject.GetComponent<IPlatform>();
// [...] your magic here
lastGameObj= hit.collider.gameObject;
}

您将获得一个附加条件,但您不会以 60 次/秒的速度运行您的代码,包括 GetComponent();。

【讨论】:

    【解决方案2】:

    您可以在接口内使用enums,您只需在接口外声明enum 类型。

    IE:

    public enum PlatformType {Normal, Ice, Fire, etc}; //Use public only if needed, of course
    
    interface IPlatform {
        PlatformType platformType { get; }
        //Your other stuff here
    }
    

    这显然会破坏封装,但如果你真的想在接口中使用enum,那就没有办法了。

    【讨论】:

      猜你喜欢
      • 2021-03-26
      • 2016-12-05
      • 2019-01-05
      • 1970-01-01
      • 2018-07-31
      • 1970-01-01
      • 1970-01-01
      • 2015-10-05
      • 1970-01-01
      相关资源
      最近更新 更多