简体   繁体   中英

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.

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. 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; you can use Serializable data classes for grouping the stats. 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):

在此处输入图片说明

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.

Your base class is Partial, You could try the below code.

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.

PS: When using [SyncVar] your baseplayer should inherit from NetworkBehaviour, right?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM