从变量中获取类型并用作泛型方法的参数



我正在为xna中的简单游戏引擎做一些东西,我将在未来的项目中使用。它绝对受到Unity框架的启发。 我目前正在开发游戏对象和组件系统,并且一直停留在克隆游戏对象上。

这就是我尝试实现它的方式,

public static GameObject CloneGameObject(GameObject obj, Vector2 position, float scale)
{
var clone = new GameObject(); //Create a entirely new gameobject
foreach (var comp in obj.components) //Clone all the components
{
Type t = comp.GetType(); 
clone.components.Add(Component.CloneComponent<t>()); //This obviously doesn't work
}
clone.position = position;
clone.scale = scale;
AllGameObjects.Add(clone);
return clone;
}

我陷入困境的是如何让组件类型用作泛型参数。

我目前对要克隆的组件所做的只是更改克隆上的所有者:

public static TComponent CloneComponent<TComponent>(Component toClone) where TComponent : Component, new()
{
var clone = new TComponent();
clone.gameObject = toClone.gameObject;
return clone;
}

组件系统很简单,所有组件都继承自组件类。

例:

class PlayerController : Component
{
public float jumpForce = 5;
public float walkSpeed = 2;
RigidBody rb;
Collider col;
public override void Start()
{
base.Start();
rb = gameObject.GetComponent<RigidBody>();
col = gameObject.GetComponent<Collider>();
}
bool canJump { get { return jumps > 0; } }
int jumps = 1;
int maxJumps = 1;
public override void Update(GameTime gameTime)
{
rb.velocity = new Vector2(Input.GetAxisInputs(Keyboard.GetState(), "Horizontal") * walkSpeed, rb.velocity.Y);
if (Input.GetKeyOnDown(Keyboard.GetState(), Keys.Space) && canJump)
{
rb.velocity = new Vector2(rb.velocity.X, -jumpForce);
jumps--;
}
if (!col.PlaceIsFree(new Vector2(0, 1)))
jumps = maxJumps;

base.Update(gameTime);
}
}

(无法正确格式化代码示例,所以我只使用了 css 片段://(

我不要求任何关于克隆本身的信息,只是简单地问我如何获得正确的通用参数,或者是否有另一种方法来实现这一点。

您应该更改CloneComponent函数的签名:

public static TComponent CloneComponent<TComponent>(TComponent toClone) where TComponent : Component, new()

然后你应该像这样调用这个函数:

foreach (var comp in obj.components) //Clone all the components
{
clone.components.Add(Component.CloneComponent(comp));
}

您还可以使用语法糖来简化调用代码:

public static TComponent Clone<TComponent>(this TComponent toClone) where TComponent : Component, new()
{
if (toClone == null)
{
throw new ArgumentNullException(nameof(toClone));
}
var clone = new TComponent();
clone.gameObject = toClone.gameObject;
return clone;
}

循环将是:

foreach (var comp in obj.components) //Clone all the components
{
clone.components.Add(comp.Clone());
}

最新更新