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

Adding Singleton Lifetime Scope #741

Open
wants to merge 7 commits into
base: master
Choose a base branch
from

Conversation

IShix-g
Copy link
Contributor

@IShix-g IShix-g commented Jan 6, 2025

Although VContainer has successfully been kept simple, I believe adding this extension could be beneficial. Therefore, I am submitting this request for your consideration. Thank you for reviewing it.

Reasons for the Addition

This extension proves useful in cases where the Project root LifetimeScope cannot be utilized for some reason.

  • Enables initialization in an arbitrary scene, instead of during game startup.
  • Since it needs to be placed in the hierarchy, it provides better visibility.

Advantages

  • Offers a LifetimeScope that persists throughout the game without being destroyed.
  • Useful for writing processes that should only be initialized once, such as ad initialization or save data setup.
  • Since one-time initialization processes can be handled, there’s no need for repeated initialization in subsequent scenes, improving performance.
  • By placing it in every scene, it ensures proper functioning even when starting the game from any scene. If it is not placed in all scenes, the game can only be started from the title scene (the initial scene).

Key Features of the Implementation

Two Classes Implemented

  • SingletonLifetimeScope<T>: Acts as the parent singleton LifetimeScope.
  • NestedSingletonLifetimeScope<T>: Acts as a child LifetimeScope.

SingletonLifetimeScope

Due to the singleton nature of this class, the Parent option is always set to None and cannot be modified.

Src4

NestedSingletonLifetimeScope

The SingletonLifetimeScope works independently, but if you need to use child LifetimeScopes, you can implement the NestedSingletonLifetimeScope. This class allows specifying the parent type during implementation, making it error-proof. It is particularly useful when implementing LifetimeScopes for each scene.

The Parent option can only be set to the designated parent type and cannot be modified otherwise.

Src3

Testing

All generation tests passed successfully.

You can review the tests here:
Assets/Tests/Unity/SingletonLifetimeScopeTest.cs

Fixes to the ParentReferencePropertyDrawer

Both SingletonLifetimeScope and NestedSingletonLifetimeScope are abstract classes. However, they were still being displayed in the inspector. Since selecting them in the inspector would lead to errors, a fix has been implemented to hide them.

Src1

Copy link

vercel bot commented Jan 6, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
vcontainer ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jan 7, 2025 5:58am

@IShix-g
Copy link
Contributor Author

IShix-g commented Jan 6, 2025

I am creating a custom editor extension to make the LifetimeScope's Parent property intentionally read-only.
It seems that specifying a Generic Type in CustomEditor no longer works in Unity 2022.
I will investigate further and make the necessary fixes.

feat: Modify singleton lifetime scopes to disable the selection of ParentReference.

@IShix-g IShix-g changed the title SingletonLifetimeScopeの追加 Adding Singleton Lifetime Scope Jan 7, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant