Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/279.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
Unity C#继承的类头_C#_Unity3d - Fatal编程技术网

Unity C#继承的类头

Unity C#继承的类头,c#,unity3d,C#,Unity3d,因此,我正在处理一个项目,其中包含一些.cs文件,我不想在其他人更新该文件时对其进行修改 所以我继承了这个类。基类中有一个标头: public partial class BasePlayer : MonoBehaviour { [Header("Stats")] [SyncVar] public int stat1 = 0; [SyncVar] public int stat2 = 0; } 在我的类中,我希望能够将另一个var添加到相同的头中。我该怎么做呢

因此,我正在处理一个项目,其中包含一些.cs文件,我不想在其他人更新该文件时对其进行修改

所以我继承了这个类。基类中有一个标头:

public partial class BasePlayer : MonoBehaviour {    
    [Header("Stats")]
    [SyncVar] public int stat1 = 0;
    [SyncVar] public int stat2 = 0;
}
在我的类中,我希望能够将另一个var添加到相同的头中。我该怎么做呢

public partial class MyPlayer : BasePlayer {    
   [SyncVar] public int stat3 = 0;
}

我知道这样做不太实际,但我真的无法触及基类。

您需要在类中添加属性,就像这样

public partial class MyPlayer : BasePlayer {    
    [Header("Stats")]
    [SyncVar] public int stat3 = 0;
}
所以我正在做一个项目,有一些.cs文件,我永远都不想看到 当其他人正在更新时进行修改

您的基类是部分的,您可以尝试下面的代码

public partial class BasePlayer : MonoBehaviour {
        [SyncVar] public int stat3 = 0;
}
编译器应该将它们组合成一个类

以下是输出:


我举了一个小例子:

public class BasePlayer : MonoBehaviour {
    [Header("Stats")]
    public int stat1 = 0;
    public int stat2 = 0;
}

public class MyPlayer : BasePlayer {
    public int stat3 = 0;
    public string duck = "quak";

    [Header("Stats2")]
    public string cow = "mooooo";
}

希望你做对了!据我所知,这是你想要的。如果您不愿意,请随时向我们提供更多信息,如UnityEditor屏幕,以显示您希望看到的内容


注:当使用[SyncVar]时,你的baseplayer应该继承NetworkBehavior,对吗?

回答得晚了,但可能会帮助其他人。而不是使用标题属性;您可以使用可序列化的数据类对统计数据进行分组。例如:

[Serializable]
public class GroupOneData
{
    public int stat1;
    public int stat2;
}

[Serializable]
public class GroupTwoData
{
    public int stat3;
    public int stat4;
}

public abstract class BaseClass<T, U> : MonoBehaviour 
    where T : GroupOneData where U : GroupTwoData
{
    public T stats1;
    public U stats2;
}


[Serializable]
public class DerivedOneData : GroupOneData
{
    public int stat3;
}

[Serializable]
public class DerivedTwoData : GroupTwoData
{
    public string text = "yeeyy";
    public float stat3;
}

public class DerviedClass : BaseClass<DerivedOneData, DerivedTwoData>
{
      // You will see all stats in inspector as foldout 
}
[可序列化]
公共类GroupOneData
{
公共int stat1;
公共int stat2;
}
[可序列化]
公共类GroupTwoData
{
公共int stat3;
公共int stat4;
}
公共抽象类基类:MonoBehavior
其中T:GroupOneData其中U:GroupTwoData
{
公共交通统计1;
美国公共统计局2;
}
[可序列化]
公共类DerivedOneData:GroupOneData
{
公共int stat3;
}
[可序列化]
公共类DerivedTwoData:GroupTwoData
{
公共字符串text=“yeeyy”;
公众浮标stat3;
}
公共类DerviedClass:基类
{
//您将在inspector中看到所有属性都是折页
}
一些现实生活中的示例是(GalleryVideoContent和GalleryImageContent都是从与上面的基类类似的GalleryContent类派生的):


我以前尝试过这个方法,但它只是在检查器中以相同的名称添加了第二个标题。@ParallelPancakes我已经更新了我的答案。您可以尝试第二种方法并让我知道。将继承的类更改为部分类不会有什么区别。@ParallelPancakes否不是您的MyPlayer类,而是再次创建相同的类。BasePlayer就像在我的第二个代码片段中一样,它只是在inspector中为新变量创建了第二个Stats标头。BasePlayer.cs中有其他代码,因此当我在MyPlayer.cs中声明新变量时,它不在同一个“Stats”标头下。代码方面,这一切都很好,但我只是想将附加变量组织在与stat1和stat2相同的头中。我尝试了此解决方案,groupin也起了作用,但其他问题public class createurestats:UnitStats{public float metabody=1.0f;}public class UnitStats{public float Hitpoints=100}
公共类TLUnit:MonoBehavior其中T:UnitStats{public T Stats;}
公共类生物:TLUnit{}
现在我不能再创建一个可以将生物或TLUnit作为参数的函数,例如,在我编写
Kill(TLUnit)
之前,现在它无法编译。即使我将其替换为
Kill(TLUnit单位)
@JacobJensen,它也应该是
Kill(TLUnit单位)
Kill(TLUnit单位,UnitConfig配置)
。如果它不工作,请共享完整的代码源以查找实际错误。