Awesome
OpenFramework
About:
The idea behind this framework is to simply have a main context and some services.
- Context: is a place to register and get services.
- Service: anything in the game/app that offers a service. like uiService, audioService, gameService, backendService etc.
Usage:
- clone OpenFramework:
git clone https://github.com/omid3098/OpenFramework.git
- Make your own context derived from GameContext.
- Implement your services derived from IService interface or use predefined helper services.
- In your gamecontext, register your services inside SetupGameContext method.
- call
Init(this);
after registering all services. this will initialize all services one by one and pass your context as gamecontext of the services. - [Optional] Check sample folder. MyGameContext.cs
So this is the basics you need to do and you are good to go!
Sample Context:
public class MyContext : GameContext
{
// if you need, subscribe to OnReady in your services.
public override event OnReadyHandler OnReady;
public override void SetupGameContext()
{
// Register what ever service you want and/or add/remove them as you need 😄
Register<GameService>();
Register<InputService>();
Register<AsyncService>();
Init(this);
}
// after all services registered and initialized successfully, OnReadyCallback will execute.
protected override void OnReadyCallback()
{
// do your stuffs here or subscribe to OnReady in your services.
if (OnReady != null)
OnReady.Invoke();
}
}
Sample Service Surgery: 💉
Here is how you write a simple InputService:
Interface simply inherits IService. you can add any method/property as you like.
public interface IInputService : IService
{
}
and your concrete service inherits from your own interface
public class InputService : IInputService, IUpdatable
{
these are sample events used for input service
public delegate void TouchDelegate(Vector2 pos);
public event TouchDelegate OnMouseDown;
public event TouchDelegate OnMouseUp;
public event TouchDelegate OnMouse;
Each IService has context so you can have access to other services and ready to check if your service is ready to use yet.
public GameContext context { get; set; }
public bool ready { get; set; }
Init will execute when you register services in your contex and call Init(this);
public IEnumerator Init()
{
// do anythign sync/async here and when your service is ready set that to true.
ready = true;
yield return null;
}
You can start and stop your services as you like. set initial valuse or dispose them on StopService. or don't use them!
public void StartService()
{
}
public void StopService()
{
}
Each service that inherit from IUpdatable will have IUpdate method so we can have update cycle in our services.
public void IUpdate()
{
if (Input.GetMouseButtonDown(0)) if (OnMouseDown != null) OnMouseDown.Invoke(Input.mousePosition);
if (Input.GetMouseButtonUp(0)) if (OnMouseUp != null) OnMouseUp.Invoke(Input.mousePosition);
if (Input.GetMouseButton(0)) if (OnMouse != null) OnMouse.Invoke(Input.mousePosition);
}
Consider checking this benchmark test between Monobehaviour Update and IUpdate:
Task was to add two numbers in update.
Iterations: 1000 </br> OpenFramework IUpdate => 0.16ms </br> MonoBehaviour Update => 0.54ms
Iterations: 5000 </br> OpenFramework IUpdate => 0.76ms </br> MonoBehaviour Update => 2.34ms
How far you can go with this?
- Write custom service and send a pull request.
- You can port any asset/lib as a service and share them as gist. ie. port LeanTouch into InputService! or Tween libs into TweenService.
- after ~year we will have a framework that we only write game logic instead of doing same things for every project!