Friday 21 November 2014

Learning C# by Developing Games with Unity 3D Beginner's Guide

Just completed that book, below is the link for anyone interested to buy the book:

https://www.packtpub.com/game-development/learning-c-developing-games-unity-3d-beginners-guide


The book teaches some simple programming in C#, but does provide in a succinct way how to do C# coding in Unity environment, which is suitable for my case as i know quite a lot about C# but nothing about Unity development. So in a sense the book is also good for programmers who previously know nothing about Unity IDE and wonder how to add codes to a game to modify game behavior (kind of like a teach yourself in 6 hours books on how to start code on Unity).

The first 6 chapters basically a hello-world C# tutorial, with some minor examples of:

1. how to create and attach script to game objects, how to get game object using GameObject.Find("[gameObjectId]"),

2. how to get the MonoBehavior sub-class script object attached to a game object (i.e. the class defined in a script attached in a game object) using thisScriptInstance.GetComponent<classname> (where ClassName refers to the class name of the MonoBehavior sub-class)

3. how to rotate a game object, by calling its transform.Rotate(x, y, z) method

4. How to expose a script object's variable as property in the Unity Inspector panel (by declaring the variable as public)

5. Where to add script behavior during game initialization (i.e. in the Start() method) and game update (i.e. in the Update() method) or for GUI output (i.e. in the OnGUI() method)

Chapter 7 teach about a simple design of a state machine and its implementation (as a script object derived from MonoBehavior)

Chapter 8 delves a bit more into implementation of individual state. Not much interesting stuff except the part where it shows how to use Unity API to interact with Unity engine, such as

1. add codes to pause a game scene (by settings Time.timeScale = 0) and restart again (by settings Time.timeScale=1). The Time.timeScale sets the game speed. Time.realtimeSinceStartup is another interesting property which returns the time in seconds from the start of the game.

2. Some basic Unity properties and method including:

Screen.width: width of the game screen
Screen.Height: height of the game screen.

bool GUI.Button(Rect rect, String text): create a button on the game screen, the return value can be used implement logics run when it return true (i.e. onClick) or false. Note that this method must be called inside the OnGUI() of a MonoBehavior object

bool GUI.Box(Rect rect, String text): a label on the game screen. Note that this method must be called inside the OnGUI() of a MonoBehavior object.

3. How to attach a script object to an empty game object (by selecting GameObject--&gt;Create Empty, then attach a script to it by dragging the script onto the create empty game object in the Unity Hierarchy panel). This is good for, e.g., when building a Unity scene of a GUI panel for game settings and configuration.

4. How to implement logic in the MonoBehavior.Awake() method override to persists an object across multiple scenes (via static member variable and DontDestroyOnload(this.gameObject) and DestroyImmeidate(this.gameObject))

5. How to switch between different scenes using Application.LoadLevel("[sceneName]") (all scenes must be in the "Scenes In Build" accessed via File--&gt;Build Settings)

While the first 8 chapters to most C# programmers is not worth much except for some of the stuffs mentioned above.

By the way, Chapter 9 and Chapter 10 is worth some reading for Unity programming using C#

No comments:

Post a Comment