0
\$\begingroup\$

Say that a project needs to be created from scratch for later use in Unity, for scripts. What is the ideal way to setup this without using a template in Visual Studio?

Can this be created in C# with some simple classes and one or two game loops?

\$\endgroup\$
6
  • \$\begingroup\$ Do you mean a purely external code library that will be compiled and referred to by a Unity project, or an executable project that will be converted to Unity later? Does the code have Unity dependencies (e.g. implementations of MonoBehaviour)? \$\endgroup\$ Commented Oct 7, 2016 at 15:39
  • \$\begingroup\$ Likely, an executable project that can be compiled and added to Unity as a script later. I would expect to not use MonoBehaviour if it is not required for some basic projects. \$\endgroup\$ Commented Oct 7, 2016 at 15:47
  • 1
    \$\begingroup\$ This sounds like a really bad idea to me. If you want to use Unity successfully, you should use the Unity editor as your central tool from the start. \$\endgroup\$ Commented Oct 7, 2016 at 15:53
  • \$\begingroup\$ It may help to give us more context about why you want to do this without setting up a Unity project & running via the editor. Depending on the problem you want to solve, there may be particular strategies that are better/worse for your use case. It's a somewhat unusual request, so there's not a simple/stock answer I'd be confident is appropriate for all needs. \$\endgroup\$ Commented Oct 7, 2016 at 16:03
  • \$\begingroup\$ Yeah, specifically, I think it be ideal to create a project that does not require Unity to run. Essentially, I could maybe run the application to look at statistics and not necessary the game scene or game play. \$\endgroup\$ Commented Oct 7, 2016 at 16:19

1 Answer 1

1
\$\begingroup\$

TLDR: Kinda but it's a lot of work for something that you will swap out in the future anyway.

I don't know that if you can get access to the unity namespaces and classes without using a template or starting a new project in unity (which essentially generates a template for you).

You could write a dummy class with the same methods that Unity uses. So a class called MonoBehaviour then simply list the names of the classes that you need (Start, Update, Awake, etc.) so that when you copy the code to unity you don't have to rename and adjust every method you write. Problem is that you won't be able to test this as easily.

When you want to test this you would have to create a basic game loop manually calling all the standard methods in the correct order then repeating update until your done. in a similar way as bellow:

public class FakeGameLoop {

    List<MonoBehaviour> scripts = new List<MonoBehaviour>();

    public void Main(string[] args)
    {
        //add all the scripts here one by one
        scripts.Add(new Thing());
        scripts.Add(new OtherScript());
        //etc, more scripts

        //initialise the scripts
        foreach (MonoBehaviour script in scripts)
        {
            script.Start();
        }

        //simulate the update cycle
        while (true)
        {
            foreach (MonoBehaviour script in scripts)
            {
                script.Update();
            }
        }
    }
}

The problem that you will face is that you need to implement more and more of unity the deeper you get into it and the more you want to use... If you want those scripts to talk to each other you have to make a basic version of the component system that unity has and implement the methods that you want to use.

If you want to just build systems that work around one script without needing any (or very little) unity specific stuff this will work ok as you test the script working on it's own. Once you are then done or ready you can move the code to unity and integrate it fully with other scripts

\$\endgroup\$
3
  • \$\begingroup\$ So, am starting to believe or understand that MonoBehavior is required for Unity? \$\endgroup\$ Commented Oct 7, 2016 at 16:19
  • \$\begingroup\$ well it's the part that unity uses for the game loop, every script has to inherit from it so that unity can effectively use it. what ever that does can be in other classes etc. but it will always need to call start, update and othrt things that interact with unity directly though that class \$\endgroup\$ Commented Oct 7, 2016 at 16:30
  • \$\begingroup\$ It's not exactly required, just spectacularly useful. Most events you want your gameplay to respond to (initialization, enabling/disabling, physics & rendering beats, collisions, destruction, shutdown) all come in as MonoBehaviour messages. With some effort you can rig up your own non-MonoBehaviour scripts to handle some of these cases, but you lose a lot of flexibility. MonoBehaviours also natively display configuration parameters in the Unity inspector, helping you debug and tune content at runtime in a very flexible & intuitive way. \$\endgroup\$ Commented Oct 7, 2016 at 17:11

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.