[英]Unity C# Inherited Class Header
所以我正在處理一個項目,其中包含一些我不想修改的 .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;
}
而且我知道這樣做不是很實用,但我真的無法觸及基類。
這是遲到的答案,但可能會幫助其他人。 而不是使用Header屬性; 您可以使用Serializable數據類對統計信息進行分組。 例如:
[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
}
一些現實生活中的例子是( GalleryVideoContent和GalleryImageContent都派生自GalleryContent類,就像上面的 BaseClass):
我做了一個小例子:
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 屏幕。
PS:當使用 [SyncVar] 時,你的 baseplayer 應該繼承自 NetworkBehaviour,對嗎?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.