Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/visual-studio/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从变量中获取类型并用作泛型方法的参数_C#_Visual Studio_Generics_Xna_Game Engine - Fatal编程技术网

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

C# 从变量中获取类型并用作泛型方法的参数,c#,visual-studio,generics,xna,game-engine,C#,Visual Studio,Generics,Xna,Game Engine,我正在为xna中的一个简单游戏引擎开发一些东西,我将用于未来的项目。它的灵感来自Unity的框架。 我目前正在开发一个游戏对象和组件系统,一直在克隆一个游戏对象 这就是我试图实现它的方式 public static GameObject CloneGameObject(GameObject obj, Vector2 position, float scale) { var clone = new GameObject(); //Create a entirely new gameobje

我正在为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;
}
publicstaticgameobjectclonegameobject(gameobjectobj,矢量2位置,浮动比例)
{
var clone=new GameObject();//创建一个全新的GameObject
foreach(对象组件中的var comp)//克隆所有组件
{
类型t=comp.GetType();
clone.components.Add(Component.CloneComponent());//这显然不起作用
}
clone.position=位置;
clone.scale=scale;
所有游戏对象。添加(克隆);
返回克隆;
}
我要解决的问题是如何将组件类型用作泛型参数

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

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

    clone.gameObject = toClone.gameObject;

    return clone;
}
publicstatictcomponent克隆组件(componenttoclone),其中TComponent:Component,new()
{
var clone=new TComponent();
clone.gameObject=toClone.gameObject;
返回克隆;
}
组件系统很简单,所有组件都继承自组件类

例如:

class PlayerController:组件
{
公众浮力=5;
公共交通速度=2;
刚体rb;
对撞机;
公共覆盖无效开始()
{
base.Start();
rb=gameObject.GetComponent();
col=gameObject.GetComponent();
}
布尔可以跳转{get{return jumps>0;}}
int跳跃=1;
int-maxJumps=1;
公共覆盖无效更新(游戏时间游戏时间)
{
rb.velocity=newvector2(Input.GetAxisInputs(Keyboard.GetState(),“Horizontal”)*walkSpeed,rb.velocity.Y);
if(Input.GetKeyOnDown(Keyboard.GetState(),Keys.Space)&&canJump)
{
rb.velocity=新矢量2(rb.velocity.X,-跳跃力);
跳跃--;
}
如果(!col.PlaceIsFree(新向量2(0,1)))
跳跃=最大跳跃;
更新(游戏时间);
}

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

public static TComponent CloneComponent<TComponent>(TComponent toClone) where TComponent : Component, new()
您还可以使用语法糖简化调用代码:

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;
}
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());
}