How to make 2D game using Unity3D

Content
    Preface
  • What is Unity3D?
  • Is it possible to make 2D game in Unity?
  • Suggested tools to make 2D game
    • NGUI
    • 2D Toolkit
    • iTween & iTween Visualization Editor
  • How to download these tools and use in our game
  • Scripting
    • Suggested programing architecture
    • Manager class make you easily control game system
    • Template class make you develop game quickly

What is Unity3D?
Unity3D is an engine to create and build game to many platforms such as PC, Android, iOS and else. It can reduce a time and work in multiple platform publishing. We can design and create game object inside editor (move, rotate and scale) easily with visualize editor. Every content and parameter that we need to see or adjust are built in visualization user interface. Also many components such as physics, audio, camera and else are already build-in in the Unity3D. So Unity3D design for every people who interest to make game, this application is very good when you cooperate with other people such as programmer or artist because the same project can be work on different platform, regardless of Windows or iOS. Unity also provide asset store, to let user share script, model or extension via online market. We also can sell our asset product in Unity asset store.
Is it possible to make 2D game in Unity?
Yes, it is possible but it might be painful to apply 3D game technique in 2D game. First of all, you can set camera projection to orthographic and now everything will look like 2D game.




Suggested tools to make 2D game
NGUI
NGUI is an extension in Unity asset store. As its name, it use for manage user interface. We can create background, label, image button, and progress bar by just only one click on NGUI interface. They actually can set animation tween to each widget. For example, if you create image button, you need to specify background image for button, text label and if you want to add more animation effect such as scale up on hover, then you just add component into that image button.
2D Toolkit
2D Toolkit is also an extension that you need to buy from Unity asset store. It use for create 2d sprite and animation. You need not to write you own code to run texture frame by frame because 2D toolkit has provide user interface to drag and drop texture into sprite collection and we can create sprite animation by using the sprite collection.
iTween & iTween Visualization Editor
iTween is a free extension on Unity asset store. We can also use iTween Visualization Editor which is an extension on top of iTween, which make us need not to write code. The iTween extension has many cool features such as moving, rotating, scaling, shaking and so on.













How to download and use these tools in our game
NGUI - Download
New version of NGUI is not free.
Fortunately NGUI also provide free version to us, but it is the old version (NGUI 2.0.7c).
If you download new NGUI version from asset store, the editor will automatically extract NGUI into your project but if you download free version. You need to open your current Unity project and double click on unity package file. It will extract NGUI package into your current project. After you finish extract NGUI package, you will see NGUI menu on menu bar.
NGUI - Usage
  1. Click NGUI menu and choose Create a New UI
  2. Select a layer that 2D camera will able to see, and click Create Your UI
  3. You will get UI Root (2D) game object, if you expand Anchor game object, there is a Panel game object inside which you can create and put widget here.
  4. Simply, please click at panel game object
  5. Click NGUI menu and choose Create a Widget
  6. Choose Atlas, the Atlas we can create by Atlas Maker which stay inside NGUI menu
  7. Choose Font, the Font we can create by Font Maker which stay inside NGUI menu
  8. Choose Template as you want
  9. Then click “Add to” button







2D Toolkit - Download
2D Toolkit is not free, you have to buy from Unity asset store
After you download from asset store, it will automatically extract to your project. Then you will see 2D Toolkit menu.
Figure 1
Figure 2






You can create 2D sprite or 2D animated sprite object via this menu list but you have to create sprite collection. The following steps below is the way to create it.
2D Toolkit - Usage
  1. Create sprite collection folder
  2. Right click at the folder and choose create, then go to tk2d and click sprite collection

  1. Rename your sprite collection and click “Open Editor…” button














  1. Select all textures that you want to use












  1. Drag and drop the textures into Sprite Collection panel and click “Commit” button










iTween & iTween Visual Editor - Download
Download Unity package and extract into your project
iTween & iTween Visual Editor - Usage
  1. Create game object
  2. Add iTween Event component



  1. Choose Event Type as you want
  2. Adjust parameter, make it work perfectly as you want
Scripting
Suggested programming architecture
The suitable programming architecture for Unity is one main class hold a bunch of manager objects. One manager will be responsible for one task such as input, audio, scene and so on. Each manager must implement Singleton design pattern to prevent multiple object initialization. We also apply Façade design pattern to make manager class use easily as much as possible.












Manager class make you easily control game system
You should apply Singleton design pattern in manager class because we want to ensure that every parts of program access the resource via this class. We may provide public static instance property in manager class which other class can call method like this “Audio.Instance.PlaySound(soundEffect)” and Main class must be the class who initialize every manager objects. Any manager objects can be access via Main instance or their own public static instance property.

Example - Main.cs
using EnityEngine;
public class Main : MonoBehavior {
    public static Main Instance {
        get {
            if (_instance == null) {
                GameObject gameObj = new GameObject(“Main”);
                _instance = gameObject.AddComponent<Main>();
            }
            Return _instance;
        }
        private set;
    }
    public AudioManager Audio {
        get;
        private set;
    }
    private static Main _instance;

    private void Awake() {
        _instance = this;
        Audio = AddComponent<AudioManager>();
       
        DontDestroyOnLoad(gameObject);
}

}

Example - AudioManager.cs
using UnityEngine;
public class AudioManager : MonoBahavior {
    public static AudioManager Instance {
        get {
            if (_instance == null) {
                _instance = Main.Instance.Audio;
            }
            return _instance;
        }
        private set;
    }

    Private static AudioManager _instance;

    private void Awake() {
        _instance = this;
    }

    public void PlaySound(SoundEffectType soundEffectType) {
        // Play sound
    }

}

If you notice the code, you will see if an audio instance does not initialize yet, it will initialize from Main class. Which now we can ensure that both Main and Audio class are instantiated. The question is why we need to do like this. The reason is in debugging mode, we may doesn’t come from main scene, and Main instance didn’t instantiate which it holds some significant information. So this way will help you initialize Main instance properly when Manager’s function have been called.
Template class make you develop game quickly
If you have been developed Unity game with a ton of scene and some scene there are some little slightly different. For example, there are three similar scenes which game actions are the same but the different is just minor content inside each scene. Do we have to write three classes separately in each scene or it can be done by one class with configurable variable? As we know that Unity has inspector which show every serialize fields. We can use this feature to minimize our work. It is better when you encounter with some bugs in one of those three scene, we can solve the problem directly at one place and other two scenes will be solved too. You need not to fix every scripts.

Comments