robotlegs-sharp / robotlegs-sharp-framework

An C# application framework. Ported from Robotlegs for ActionScript 3.
MIT License
84 stars 25 forks source link

Examples? #5

Closed mikecann closed 8 years ago

mikecann commented 8 years ago

Hey,

Nice work on this port! Im a long time RobotLegs user, used Strange and wasnt too impressed.

I was wondering if you have any open-source examples of using this library, particularly how you arrange scenes and contexts you wouldn't mind sharing?

Mike

prankard commented 8 years ago

Hi Mike,

Thanks for your interest in Robotleg's Sharp. If you're a robotlegs fan then this port should be good for you as it's as close to the original that we could provide.

The only example we have to provide at the moment is this one: https://github.com/robotlegs-sharp/unity-hello-world Which shows you how to set-up a single context. Add some view's and get them mediated with a model.

In a real life example, it wouldn't be this simple. I would have lots of view's nested and laid out in my scene. Usually all a child of a 'Root' GameObject which would have a script to define the context and be the ContextView. Then when the scene boots up, the context will start, all mappings will be made in the configure stage. And all view's on the stage will get mediated.

Of course, everyone works differently and I don't want to say any way is the 'right' way. But in terms of my workflow. I have one scene, and usually use Resources.Load and unload in Unity rather than have Unity load in many scenes. I personally only have one context in my project. But the framework is tooled up to handle multiple contexts (with children injector context's too). In terms of how that would work with Unity, if I had to do it, I would load in multiple scenes with LoadSceneMode.Additive, and have each scene with one gameobject containing all it's children needed.

How do you use Robotlegs? Are you a multi-context person? I'd be happy to provide more examples, but right now a full project is something I cannot provide at the moment. If you'd like to know how the multi-context works, I could write a 'Multi-Context' hello world example if it's needed.

mikecann commented 8 years ago

Hi James,

Thanks for such a detailed response. It does look like you guys have done an amazing job with the port, im really impressed and cant wait to get started with it.

In AS3 I wasn't a multi-context person as it wasn't necessary but the way Unity handles scenes and prefabs meant that I had some difficulties when I was trying to use StrangeIoC.

The main issue is if you have a "Main" scene and then you have a number of smaller "Level" scenes. Your main scene would have the context and would be able to load a level scene, that was okay.

The problem was if you were working in the level scene and you wanted to quickly test it out, you would have to switch back to the Main scene then press play, then return to the level scene to make more changes etc. This was tedious and slow workflow.

I experimented around with having each level have its own context in StrangeIoC) and thus you could just press play in the level which would launch and play the level directly but this got really messy really quickly and became difficult to debug.

How would you handle this situation? I guess another way would be to intercept the play button editor press and redirect the scene to the main scene or perhaps even load the main scene into the level scene?

Mike

prankard commented 8 years ago

Hey Mike,

Looks like Unity has handled this particular situation for you in the latest update (version 5.3) to my understanding. That is, they do additive level editing. So in your example, you can Open your Main Scene. Then right click, additive load a different scene in the editor. That should help you if you are additive level loading and need to edit the inner level. See the official docs or the unofficial video

I don't enjoy loading levels, as it took longer than loading and unloading prefabs from Resources. Of course, this isn't always possible when you want to do different lighting effects in different scenes. So I've never experienced this issue, although it can be a pain to keep dragging out prefabs to edit, and then delete them before previewing. (Which has been my workflow for Robotlegs).

However, your issue bring out another patten people would use, and it would be great if we could fix it. Some people like to switch scenes (Preloader -> Main Menu -> Game) and our framework will not work when testing a different scene for this. As the context needs to be instantiated (usually the first loaded scene). You can organise this in a multi context way as your proposed, however Robotlegs will only allow multiple context's to communicate to each other by having another parent context. This means, your stuck with the same issue, you'll need to boot the parent context at level load.

Really, what we want, is a beginning point in Unity that will allow us to kick off our context and begin. You can do this like so:

using UnityEngine;
using robotlegs.bender.framework.impl;
using robotlegs.bender.platforms.unity.extensions.contextview.impl;
using robotlegs.bender.bundles;

public class Main
{
    private static Context _context;

    [RuntimeInitializeOnLoadMethod]
    private static void Init()
    {
        _context = new Context();
        _context.Install<UnityMVCSBundle>();
        _context.Install<UnityFallbackBundle>();
        _context.Configure(new TransformContextView(new GameObject("Context View").transform));
    }
}

I hope this helps you with your workflow (and that I understood the question). It should work as long as each scene views are not dependant on data that might be created in a previous scene.

James

prankard commented 8 years ago

Hey just thought I'd write a bit more here. I was writing some Multi-Context examples in Unity3d and it wasn't behaving as expected. So I'm fixing bugs but will post an example when it's fixed.

mikecann commented 8 years ago

Hi James,

Oh nice, I didn't know about RuntimeInitializeOnLoadMethod, thats cool, what I generally use to ensure a script is invoked before others is to use the script execution order in Unity (not exactly intuitive):

image

Your "Preloader -> Main Menu -> Game" example is exactly the sort of structure that many people take when developing games in Unity.

Im not sure that Multi-contexts are the way or just simply importing a "MainContext" scene or something when the game loads shrug

Mike

prankard commented 8 years ago

Hey Mike,

Yeah, the Script Execution Order I've used a lot before to try and get things running. It's good, but can get annoying to modify. I wish there was a metadata tag you could attach to a class in order to prioritise. I'm sure Unity will get there.

Actually, the RuntimeInitializeOnLoadMethod is something I just found a week or so ago, it's good. But it's still not great of a novice user, as they will be unaware that all these scripts are running in your unity scene. The best thing I think we can do is call the class 'Main' or something to make it obvious that it's important.

I've just updated the 'hello-world' example with a MultiContext example if you wanted to see if it could work in your case. The MutliContext hadn't actually been tested in a real app in Unity (only tested with the Unit Test's Robotlegs originally supplied) and it was a little broken (it worked, but with a lot of strange setup workaround). So I've spent the time fixing the bugs in the Modularity extension and now the modularity works.

I've started a simple project example app today. It will contain at least a menu scene and a game scene so I can demo the framework with a more complete functioning app. Once I've done this I can close this issue.

I don't think multicontext is the solution to your problem. Once I've finished this example app, hopefully it will help your workflow. It's certainly different than using prefabs, but I think it's possible to get a good workflow going, we haven't had issues with our work projects on this because over time we've built a fairly large library of extensions to make it work how we want it.

I've also been doing some updates to speed up production on the framework by adding a Right Click Menu for creating Events/Commands/Mediators/Services/Models/Config/View etc and looking into a few other features in Unity so have been a little distracted to complete the examples so far.

mikecann commented 8 years ago

@prankard awesome! That all sounds fantastic. Im really looking forward to getting started with this. Our project hasn't kicked off yet but im hoping it will be soon, ill let you know how we get on with it :)

prankard commented 8 years ago

Gone ahead an pushed an example game project with extensions etc: https://github.com/robotlegs-sharp/unity-hello-game

It hopefully should be enough to show you how someone would use Robotlegs in Unity with multiple scenes.

mikecann commented 8 years ago

Thats awesome thanks :+1: