GithubHelp home page GithubHelp logo

phillipzeratul / menusystemwithzenject Goto Github PK

View Code? Open in Web Editor NEW
10.0 3.0 3.0 53 KB

A Unity Menu Framework using Dependency Injection, inspired by MenuSystem & Zenject.

C# 100.00%
unity menusystem zenject dependency-injection

menusystemwithzenject's Introduction

MenuSystemWithZenject

This is a Unity Menu Framework inspired by
MenuSystem
https://github.com/YousicianGit/UnityMenuSystem
Check their talk at Unity Europe 2017 at
https://www.youtube.com/watch?v=wbmjturGbAQ
and Zenject, a Dependency Injection Framework for Unity
https://github.com/modesttree/Zenject

I started this because my project is written with Zenject and I want the MenuSystem to work with Zenject as well.

So inspired by the idea of MenuSystem, I did some modification to it and I think the result is quite pleasing.

Introduction

As inspired by MenuSystem, all the menus are stored as canvas prefabs and once you assign them to GlobalInstaller component on the ProjectContext prefab, they will be injected by Zenject automatically on game start so you can access them anywhere in the code by just calling [Inject]. See more about the convenience of Dependency Injection in their Documents.

At runtime, all the instance of menus are under ProjectContext hierarchy in DontDestroyOnLoad, so they are persistent.

How to use:

  1. You need Zenject for this to work. You can download it here: https://github.com/modesttree/Zenject and import it into your project.

  2. You need to create a Zenject Project Context in Resource folder, all the menus will be a child in the hierarchy of this Project Context at runtime.

Assets -> Create -> Zenject -> Project Context
  1. Add GlobalInstaller.cs to Project Context's Installer list.
    GlobalInstaller.cs is where the global binding of Zenject happens.

  2. You need a persistant scene that has a Zenject SceneContext (for Zenject to work) and a EventSystem (for listening UI events) in it.

  3. Create a new script that derives from Menu<> i.e. StartMenu.cs

using Zenject;
using MenuSystemWithZenject;

public class StartMenu : Menu<StartMenu>
{
    private GameMenu.Factory _gameFactory;

    [Inject]
    private void Init(GameMenu.Factory gameFactory)
    {
        _gameFactory = gameFactory;
    }

    public void OnStartClick()
    {
        _gameFactory.Create().Open();
    }
}

The above code declares a StartMenu and open the GameMenu when start button is clicked. And don't forget to create the GameMenu as well, it should derive from Menu<GameMenu>, and you can write your own function behaviours.

  1. All the menus are individual canvases and need to be a prefab, add all the prefabs to GlobalInstaller to the Project Context prefab.

  2. And you are good to go.

Some use tips:

  1. You need to use the Menu.Factory.Create() to find the instance of the menu so it get injected by Zenject when created.

  2. All the menus inhereit from Menu has OnBackPress() method that will destroy/disable it self when called based on the setting of this menu.

  3. OnMenuEnabled Action in MenuManager.cs is called when Menu is enabled, you can override OnEnable() method to do some notification stuff.

  4. You can override OpenAnimation() and CloseAnimation() to add animation when Menu is opened or closed.

  5. public void GoToMenu(Menu instance, bool shouldCloseAlwaysOnTopMenu = false) will let you jump to Menu instance, closing all the menus in between.

  6. AlwaysKeepOnTop is a tag that will make the Menu on top of all the Menus without it, OnBackPressed() on other Menus will not close Menus with AlwaysKeepOnTop set to true, you can close it specifically call this Menu's OnBackPressed(). Good to use for overlay menus.

menusystemwithzenject's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.