Unity-Technologies / com.unity.netcode.gameobjects

Netcode for GameObjects is a high-level netcode SDK that provides networking capabilities to GameObject/MonoBehaviour workflows within Unity and sits on top of underlying transport layer.
MIT License
2.09k stars 430 forks source link

NetworkTransformEditor.OnEnable is not overridable #2800

Open Gretsok opened 6 months ago

Gretsok commented 6 months ago

Hello,

I was making a custom NetworkTransform inheriting from NetworkTransform such as the ClientNetworkTransform, and I wanted to add a new exposed field to it. This field doesn't show up in the inspector due to the NetworkTransformEditor, so I started making my own CustomNetworkTransformEditor inheriting from NetworkTransformEditor but I can't reuse the OnEnable method without totally overriding the properties set up in the base class.

I would like to have a way to set up new field in this editor without breaking the existing or re-writing everything. It could be as simple as overriding the OnEnable method or having another dedicated method called in the base OnEnable to handle custom logic.

Thank you,

NoelStephensUnity commented 6 months ago

Hi Gretsok, You can actually accomplish this by adding a blank custom editor like such:

using Unity.Netcode.Components;
using UnityEngine;
#if UNITY_EDITOR
using UnityEditor;
// This bypases the default custom editor for NetworkTransform
// and lets you modify your custom NetworkTransform's properties
// within the inspector view
[CustomEditor(typeof(CustomNetworkTransform), true)]
public class CustomNetworkTransformEditor : Editor
{
}
#endif

/// <summary>
/// Projectiles will be owner driven
/// </summary>
public class CustomNetworkTransform : NetworkTransform
{
    public enum AuthorityModes
    {
        ServerAuthority,
        OwnerAuthority
    }

    public AuthorityModes AuthorityMode;

    protected override bool OnIsServerAuthoritative()
    {
        return AuthorityMode == AuthorityModes.ServerAuthority;
    }
}

Let me know if this works for your needs?

Gretsok commented 6 months ago

Hello,

Thank you for your quick response! Sorry, I was not clear enough on what I was trying to achieve. It would indeed work but I would lose all the logic inside NetworkTransformEditor, but I actually like it and would like to keep it for my custom class.

Creating a new editor class directly inheriting UnityEditor.Editor is what I am doing for now, I also copy pasted the content of NetworkTransformEditor in my custom editor class to get the result that I want, but I would prefer to rely directly on NetworkTransformEditor to handle future changes.

Happy new year !

NoelStephensUnity commented 6 months ago

Happy new year indeed! 🎆 That has been an outstanding issue for awhile now. We could just make the OnEnable method virtual which seems to work on my end. Will look into getting that added into our queue for the update after the coming v1.8.0 update.

As I understand the issue: NetworkTransformEditor cannot be extended because the OnEnable method was declared but not made virtual, which makes the initial setup for any customized extension of NetworkTransformEditor very difficult (i.e. cannot be extended without completely copying the original source code).

To resolve this issue, we can just make the OnEnable method virtual which would allow for the customization of the NetworkTransformEditor.

Marked as a bug and should be added to our queue.