[英]Unity C# Inherited Class Header
So I'm working on a project with some .cs files I do not want to ever modify as it's being updated by someone else.所以我正在处理一个项目,其中包含一些我不想修改的 .cs 文件,因为它正在被其他人更新。
So I'm inheriting from the class.所以我继承了这个类。 In the base class is a header:
在基类中是一个标题:
public partial class BasePlayer : MonoBehaviour {
[Header("Stats")]
[SyncVar] public int stat1 = 0;
[SyncVar] public int stat2 = 0;
}
In my class I want to be able to add in another var to the same header.在我的班级中,我希望能够将另一个 var 添加到同一个标题中。 How would I go about doing this?
我该怎么做呢?
public partial class MyPlayer : BasePlayer {
[SyncVar] public int stat3 = 0;
}
And I'm aware it's not very practical to do it this way but I really can't touch the base class.而且我知道这样做不是很实用,但我真的无法触及基类。
It's late answer but may help others.这是迟到的答案,但可能会帮助其他人。 Rather than using Header attribute;
而不是使用Header属性; you can use Serializable data classes for grouping the stats.
您可以使用Serializable数据类对统计信息进行分组。 For example:
例如:
[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
}
Some real life examples are (both GalleryVideoContent and GalleryImageContent are derived from GalleryContent class that like BaseClass above):一些现实生活中的例子是( GalleryVideoContent和GalleryImageContent都派生自GalleryContent类,就像上面的 BaseClass):
You need to put the attribute in your class as well like so,你需要把属性放在你的类中,就像这样,
public partial class MyPlayer : BasePlayer {
[Header("Stats")]
[SyncVar] public int stat3 = 0;
}
So I'm working on a project with some .cs files I do not want to ever modify as it's being updated by someone else.
所以我正在处理一个项目,其中包含一些我不想修改的 .cs 文件,因为它正在被其他人更新。
Your base class is Partial, You could try the below code.你的基类是 Partial,你可以试试下面的代码。
public partial class BasePlayer : MonoBehaviour {
[SyncVar] public int stat3 = 0;
}
The compiler should combine them both into one class.编译器应该将它们合并为一个类。
Here is the output :这是输出:
I made a little example:我做了一个小例子:
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";
}
Hope to got you right!希望你做对了! As far as is understand you this is what you want.
据了解,这就是你想要的。 If not feel free to give us a little more information like a UnityEditor screen of what exactly you want to see.
如果不能随意给我们提供更多信息,例如您想要查看的内容的 UnityEditor 屏幕。
PS: When using [SyncVar] your baseplayer should inherit from NetworkBehaviour, right? PS:当使用 [SyncVar] 时,你的 baseplayer 应该继承自 NetworkBehaviour,对吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.