You've successfully subscribed to Hacking with Unity - Learn how to create your own games
Great! Next, complete checkout for full access to Hacking with Unity - Learn how to create your own games
Welcome back! You've successfully signed in.
Success! Your account is fully activated, you now have access to all content.
Success! Your billing info is updated.
Billing info update failed.

CSharp

Date time in C#
Members Public

It is quite common to see Date and Time used in the game, here are some frequently used methods: Get the current date time// Get the current date and time DateTime currentDateTime= DateTime.Now; Console.WriteLine(currentDateTime.ToString()); Get date and time difference between two dates// Get time difference between

Ben Liu
Ben Liu
C#

Ternary Operator in C#
Members Public

Also called One-line if: Syntax condition ? consequent : alternative Example using UnityEngine; using System.Collections; public class TernaryOperator: MonoBehaviour { void Start() { int age = 20; string message; message = health > 20 ? "should buy life insurance" : "Player is young"; } }

Ben Liu
Ben Liu
C#

Namespaces in C#
Members Public

Namespaces are like containers for classes, which can help us organize the code, we see the same concept in other programming languages such as Java, Python and other languages. The purpose of Namespace is to avoid variable or class conflicts and ambiguity. For example both of UnityEngine and System have

Ben Liu
Ben Liu
C#

Properties in C#
Members Public

Like many other programming languages, C# class does have the class properties, also called fields. Example of the class propertypublic class Player { private int experience; public int Experience { get { return experience; } set { experience = value; } } } // Use property public class GameControl: MonoBehaviour { void Start () { Player myPlayer = new Player(); //Properties can be used

Ben Liu
Ben Liu
C#
Use Singleton in Unity

Use Singleton in Unity
Members Public

Singleton is a common design pattern in programming, which keeps only one copy of a variable or object throughout the entire runtime of the program. Create a Singleton is quite simple in Unity:First create an empty game object in the hierarchy named SingletonExample, then create a C# script attached

Ben Liu
Ben Liu
C#

Output formatting in C#
Members Public

See the following examples for the formatting on float and date: using System; using System.Globalization; using System.Threading; namespace CSharpLearning { class Program { static void Main(string[] args) { double f = 0.049; // print out float: 0.049 Console.WriteLine(f); // keep 2 decimals with rounding Console.WriteLine(String.Format("{0:

Ben Liu
Ben Liu
Unity Basic

How to create struct in C#
Members Public

Like Enumerations, struct is value type, which means if you create a struct variable which holds its actual value not the reference. If you try to modify the value and it will not affect the other copy. Features of the Struct: ImmutableValue typesuitable for collection of data (i.e. a

Ben Liu
Ben Liu
C#