Friday, April 26, 2019

c# - How can I build a game in Unity with minimum/no use of the visual editor?


I'd like to write a game completely in C#. In my search for an engine, I found Unity3D, but all the tutorials and documentation are speaking about a visual editor and the Unity IDE in which you click and point around to create scenes and scripts.



I don't want to do that. I prefer full code coverage over designers abstracting things away from me. I'd like to only write pure C# code from scratch or if required the Unity scripts as an addition. I couldn't find any explanation or documentation about doing so; how can I build a game using Unity and making minimum (or no) use of the visual editor?



Answer



I am a complete beginner in Unity, but this is how I do it at the moment, and it reduces the editor usage to minimum:


In editor, I only have three objects: An empty GameObject called "main", a camera, and a light. And this is only because so far I only work with a single camera and a single light, so it was faster this way. Later I will probably remove them, and only the "main" will remain.


In "Assets/MyScripts" I have a class "Main", which is added to the "main" GameObject as a behavior. Which means that when the program starts, the "Main" class in instantiated and its method is called. The "Main" class is like this:


using UnityEngine;

public class Main : MonoBehaviour
{


void Start ()
{
// initialize the game
}

void Update ()
{
// update physics
}


}

In the game I dynamically build the environment like this:


GameObject floor = GameObject.CreatePrimitive (PrimitiveType.Cube);
floor.renderer.material.color = RandomGreen ();

But this is because so far I am only making a prototype. Later I will want to replace the cubes with some nice things edited in Blender. Which will require making them in Blender, and importing them to Units as "prefabs". Then, I will similarly instantiate the "prefabs" from the C# code.


If you want to make an object react to events, such as collisions with other objects, you can instantiate the object and add them dynamically a behavior class, which is a C# class derived from MonoBehavior. For example, if you want to have a car, you make a car prefab, and write a "CarBehavior" class for its behavior in the game.


This way you can reduce the interaction with the editor to a minimum, although probably not completely to zero. Now it depends on whether this is an acceptable solution for you.


No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...