Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[IMPROVE] Let us select private types #60

Closed
Antoshidza opened this issue Jun 5, 2024 · 2 comments · Fixed by #61
Closed

[IMPROVE] Let us select private types #60

Antoshidza opened this issue Jun 5, 2024 · 2 comments · Fixed by #61
Assignees
Labels
enhancement New feature or request

Comments

@Antoshidza
Copy link

Antoshidza commented Jun 5, 2024

It is powerful thing to have implementations private while still be able to assign instances through unity inspector. The whole idea of this is to let code be flexible and not know what particular implementations we use which is polymorphic thing. Sometimes we don't want to expose types for public use in code.

For example I have next implementation (code below):
Here I have some IStartup implementation which have injected fields which is why I don't want to serialize it. So I have IStartupConfig which just creates IStartup being just dumb serializable data container with one method. This way I perfectly can assign composition of IStartup modules through ScriptableObject with IStartupConfig[] array.
But RestoreCardEveryTurn.Config should be public and there is no case I want to use it from code, because again this class specifically for serialization in editor.

public class RestoreCardEveryTurn : IStartup
{
    [AddTypeMenu("GameRules/Restore Card Every Turn")]
    [Serializable]
    public class Config : IStartupConfig
    {
        [SerializeField] private int _restoreCount;
        
        public IStartup Construct() => new RestoreCardEveryTurn(_restoreCount);
    }
    
    [Inject] private readonly ITurn _turn;
    [Inject] private readonly ISelectAndRestoreCardService _selectAndRestoreCardService;
    
    private readonly int _restoreCount;
    private readonly CompositeDisposable _subs = new();

    bool IStartup.Persistent => true;

    public RestoreCardEveryTurn(in int restoreCount)
        => _restoreCount = restoreCount;

    UniTask IInitializable.Initialize()
    {
        _turn.NextTurn
            .SubscribeAsync(async _ =>
            {
                for (int i = 0; i < _restoreCount; i++)
                    await _selectAndRestoreCardService.TrySelectAndRestoreCard();
            })
            .AddTo(_subs);
        return UniTask.CompletedTask;
    }

    void IDisposable.Dispose() => _subs.Dispose();
}
@Antoshidza Antoshidza added the enhancement New feature or request label Jun 5, 2024
@mackysoft
Copy link
Owner

Indeed. Exposing nested classes in an interface is a common technique, so it looks good.

@mackysoft mackysoft linked a pull request Aug 3, 2024 that will close this issue
@mackysoft
Copy link
Owner

Nested private class support is here.
https://github.com/mackysoft/Unity-SerializeReferenceExtensions/releases/tag/1.4.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants