Search Unity

MonoBehaviour and NetworkBehaviour sharing Class

Discussion in 'Scripting' started by nrvllrgrs, Jun 29, 2018.

  1. nrvllrgrs

    nrvllrgrs

    Joined:
    Jan 12, 2010
    Posts:
    62
    I'm attempting to build a flexible weapons system (to be shared with the community). Knowing that users may want to create either single-player and multiplayer experiences, and me not wanting to copy-paste code, I'm considering authoring a MonoBehaviour and NetworkBehaviour that both use a class. For example,

    Code (CSharp):
    1.     public class ShooterController : MonoBehaviour
    2.     {
    3.         #region Variables
    4.  
    5.         [SerializeField]
    6.         private ShooterControllerData m_data;
    7.  
    8.         #endregion
    9.  
    10.         #region Properties
    11.  
    12.         public ShooterControllerData data
    13.         {
    14.             get { return m_data; }
    15.             private set { m_data = value; }
    16.         }
    17.  
    18.         #endregion
    19.  
    20.         #region Methods
    21.  
    22.         private void Reset()
    23.         {
    24.             data = new ShooterControllerData(this);
    25.         }
    26.  
    27.         private void Update()
    28.         {
    29.             data.Tick();
    30.         }
    31.  
    32.         #endregion
    33.     }
    Code (CSharp):
    1.     public class ShooterControllerNet : NetworkBehaviour
    2.     {
    3.         #region Variables
    4.  
    5.         [SerializeField]
    6.         private ShooterControllerData m_data;
    7.  
    8.         #endregion
    9.  
    10.         #region Properties
    11.  
    12.         public ShooterControllerData data
    13.         {
    14.             get { return m_data; }
    15.             private set { m_data = value; }
    16.         }
    17.  
    18.         #endregion
    19.  
    20.         #region Methods
    21.  
    22.         private void Reset()
    23.         {
    24.             data = new ShooterControllerData(this);
    25.         }
    26.  
    27.         private void Update()
    28.         {
    29.             if (!hasAuthority)
    30.                 return;
    31.  
    32.             data.Tick();
    33.         }
    34.  
    35.         #endregion
    36.     }
    Code (CSharp):
    1.     [System.Serializable]
    2.     public class ShooterControllerData
    3.     {
    4.         #region Variables
    5.  
    6.         /// <summary>
    7.         /// Indicates whether player controls this controller
    8.         /// </summary>
    9.         public bool isPlayerControlled;
    10.  
    11.         /// <summary>
    12.         /// Indicates whether "Fire" action triggers every frame
    13.         /// </summary>
    14.         public bool isContinuous = false;
    15.  
    16.         /// <summary>
    17.         /// Indicates whether "Fire" action automatically triggers on button down
    18.         /// </summary>
    19.         public bool isAutoFire = false;
    20.  
    21.         /// <summary>
    22.         /// Seconds between shots
    23.         /// </summary>
    24.         public float timeBetweenShots;
    25.  
    26.         // Etc
    27.     }
    Is this a reasonable approach? If so, is anybody any good with PropertyDrawers? Odin Inspector has spoiled me.