Skip to content

A Unity extension framework that includes features such as object pooling, scene routing, UI manager, and behavior trees.

License

Apache-2.0, Unknown licenses found

Licenses found

Apache-2.0
LICENSE
Unknown
LICENSE.meta
Notifications You must be signed in to change notification settings

MorizeroDev/Minity

Repository files navigation

Minity

Milthm unity framework, a Unity extension framework that includes features such as object pooling, scene routing, UI manager, and behavior trees.

Setup

Unity Editor -> Package Manger -> Add package from git URL...

# Minity Core
https://github.com/MorizeroDev/Minity.git

# Milease Core
https://github.com/MorizeroDev/Milease.git

# Color Tools
https://github.com/ParaParty/ParaPartyUtil.git?path=Colors

Or including these in manifest.json:

"dev.milthm.minity": "https://github.com/MorizeroDev/Minity.git",
"com.morizero.milease": "https://github.com/MorizeroDev/Milease.git",
"party.para.util.colors": "https://github.com/ParaParty/ParaPartyUtil.git?path=Colors",

Resource Mapping by Enum Values

Using enum values as identifiers for various resources enhances the maintainability of your project. For instance, when you're working with scene routing or object pooling, you can associate enum values with specific resources during the initialization phase, and later reference these resources by using the enum values.

Additionally, Milutools does not rely on the integer data of enum values. Even if you have two enums with identical integer values, Milutools can still differentiate between them.

Object Pool

Milutools provides features like object pooling and automatic returning. Object pools are commonly used in Unity development to reduce the overhead of frequent creation and destruction of game objects, thus improving performance.

Milutools will also batch return any excess objects created during peak usage based on the current usage situation.

First, you need to attach the PoolableObject component to the prefab of the object type you want to pool, and configure its parameters.

For example, you can set the objects to automatically return to the pool after being active for a certain period, or wait for manual recycling.

Next, use the following method to register a poolable object prefab and define the lifecycle of the created objects:

ObjectPool.EnsurePrefabRegistered(EnumValue, Prefab, BaseCount);

To retrieve an object managed by the object pool, use the following method:

ObjectPool.Request(EnumValue);

Scene Router

Have you encountered a situation during game development where you need to return to a previous scene, but due to a special game process, an intermediary scene is inserted between two scenes that originally had a parent-child relationship? This might break the "back" functionality, preventing it from returning properly to the previous scene. Alternatively, manually specifying the scene name for the return can be problematic if you need to change a scene’s name later on, and you realize that it's referenced by strings all over your project, making refactoring a complex task.

The scene router was created to solve these issues. It also wraps loading animations, making it easier to use them during scene transitions.

First, we need to configure the scene router:

private enum SceneIdentifier
{
    TitleScreen, MainMenu, StoryMenu, Story
}

[RuntimeInitializeOnLoadMethod]
public static void SetupSceneRouter()
{
    SceneRouter.Setup(new SceneRouterConfig()
    {
        SceneNodes = new[]
        {
            SceneRouter.Root(SceneIdentifier.TitleScreen, "Title"),
            SceneRouter.Node(SceneIdentifier.MainMenu, "main", "Main"),
            SceneRouter.Node(SceneIdentifier.StoryMenu, "main/storymenu", "StoryMenu"),
            SceneRouter.Node(SceneIdentifier.Story, "main/storymenu/story", "Story")
        }
    });
}

This way, if you need to jump from StoryMenu to something like CGScreen, and then to Story due to a special game process, the Story scene will correctly return to its parent StoryMenu.

Use the following method to switch scenes:

SceneRouter.GoTo(SceneIdentifier.MainMenu);

To quickly return to the previous scene, use:

SceneRouter.Back();

Both methods will return a SceneRouterContext, which allows you to pass data between scenes using a fluent interface:

SceneRouter.GoTo(SceneIdentifier.MainMenu).Parameters(data);

You can retrieve the data in another scene like this:

SceneRouter.FetchParameters<Data>();

UI Manager

Quickly build easy-to-use, managed UIs by implementing abstract classes ManagedUIReturnValueOnly<T, R>, ManagedUI<T, P>, and ManagedUI<T, P, R>.

In the design of Minity, we consider that each UI can have "parameters" and "return values", and the results returned by the "UI" are passed to subsequent processing functions via callbacks. Of course, we also provide asynchronous functions for your choice.

For generic parameters:

  • T: A Type ID for the UI. Generally, you should specify this as the specific derived class.
  • P: The Parameter type for the UI.
  • R: The Return Value type for the UI.

Let's assume we have an InputBox, which pops up a window for the player to enter a name, then returns the entered name.

We can use it like this:

public class InputBox : ManagedUI<InputBox, string, string> {}

If you feel that using just string isn't intuitive enough, you can further encapsulate it:

public class InputBoxRequest {
    public string Title;
    public string Prompt;
}

public class InputBoxResponse {
    public string PlayerName;
}

public class InputBox : ManagedUI<InputBox, InputBoxRequest, InputBoxResponse> {}

Next, we need to bind it with the prefab at the point of initializing the UI manager:

[RuntimeInitializeOnLoadMethod]
public static void SetupUI()
{
    UIManager.Setup(new []
    {
        // Both methods are acceptable
        UI.FromPrefab(prefab),
        UI.FromResources("path/to/your/prefab"),
    });
}

Now, we can use the UI directly through any of the following methods:

InputBox.Open("Please enter your name", (name) => Debug.Log($"The player name is {name}"));

var playerName = await InputBox.OpenAsync("Please enter your name");

Custom Loading Animations

You can create custom loading animations by extending LoadingAnimator and assigning it to the scene router.

For example, here’s a default black fade transition that uses Milease, a lightweight animation library designed for Unity UI development:

public class BlackFade : LoadingAnimator
{
    public Image Panel;

    public override void AboutToLoad()
    {
        MilInstantAnimator.Start(
            	0.5f / Panel.MQuad(x => x.color, Color.clear, Color.black)
        	)
            .Then(
                new Action(ReadyToLoad).AsMileaseKeyEvent()
            )
            .UsingResetMode(RuntimeAnimationPart.AnimationResetMode.ResetToInitialState)
            .PlayImmediately();
    }

    public override void OnLoaded()
    {
        MilInstantAnimator.Start(
            	0.5f / Panel.MQuad(x => x.color, Color.black, Color.clear)
        	)
            .Then(
                new Action(FinishLoading).AsMileaseKeyEvent()
            )
            .UsingResetMode(RuntimeAnimationPart.AnimationResetMode.ResetToInitialState)
            .PlayImmediately();
    }
}

In AboutToLoad(), you need to cover the screen with the animation and call ReadyToLoad() at the end of the animation to notify the scene router to begin loading.

During loading, you can get the loading progress via the base.Progress property to update the screen.

Once the scene is fully loaded, the router will call OnLoaded(), where you should play the closing animation and call FinishLoading() to inform the router that everything is complete.

You can then associate these loading animation prefabs with enum values in the scene router configuration and use them during scene transitions.

About

A Unity extension framework that includes features such as object pooling, scene routing, UI manager, and behavior trees.

Topics

Resources

License

Apache-2.0, Unknown licenses found

Licenses found

Apache-2.0
LICENSE
Unknown
LICENSE.meta

Stars

Watchers

Forks

Packages

No packages published

Languages