indiegamedev

Unity3D – Game Engine – Script Lifecycle Flowchart

Unity3D – Game Engine – Script Lifecycle Flowchart

monobehaviour_flowchart

My website: http://www.lucedigitale.com

Ref: http://docs.unity3d.com/Manual/ExecutionOrder.html

By |Unity3D, Video Games Development|Commenti disabilitati su Unity3D – Game Engine – Script Lifecycle Flowchart

Unity 3D – CSharp – Event Functions – Inizialization – Update – GUI – Mouse – Physics

Unity 3D – CSharp – Event Functions – Inizialization – Update – GUI – Mouse – Physics

To create videogames Unity3D give us special functions to manage special operations as variable inizialization, updating, physic management.


using UnityEngine;
using System.Collections;

public class MyScript : MonoBehaviour
{

        // Initialization Events
        // ******************************************

	void Awake ()
	{
		// first inizialization
		Debug.Log("Awake called.");
	}
	
	
	void Start ()
	{
		// second inizialization
		Debug.Log("Start called.");
	}

        // Regular Update Events
        // ******************************************

	void FixedUpdate ()
	{
		// frame indipendent update
                // use this function to drive physic
		Debug.Log("FixedUpdate time :" + Time.deltaTime);
	}
	
	void Update ()
	{
		// update at every frame
		Debug.Log("Update time :" + Time.deltaTime);
	}

	void LateUpdate () 
	{
		// update at the end of the frame rendering
		Debug.Log("Late Update");
	}

        // GUI Events
        // ******************************************

        void OnGUI() 
        {
                // draw here labels, buttons and other Graphic User Interface elements
        	GUI.Label(labelRect, "Game Over");
        }

        // Mouse Events
        // ******************************************

        void OnMouseOver() 
        {
                // do something if mouse is over
        }

        void OnMouseDown() {
                // do something if mouse is down
        }

        // Physics Events
        // ******************************************

        void OnCollisionEnter(otherObj: Collision) 
        {
                // do stuff if there is a collision
        }

        void OnTriggerEnter(Collider other) 
        {
                // do stuff if there is an object that overlap the trigger
        }

} // END MyScript

My website: http://www.lucedigitale.com

Ref: http://docs.unity3d.com/Manual/EventFunctions.html

By |CSharp, Unity3D, Video Games Development|Commenti disabilitati su Unity 3D – CSharp – Event Functions – Inizialization – Update – GUI – Mouse – Physics

Unity3D – C# – Basic Syntax

Unity3D – C# – Basic Syntax

First you need call basic namespace and nest all the code inside a class with the name of your script, see the example below.

Basic Structure

Project Window> RMB> Create> C#> name it ‘MyFirstScript’> double click to open ‘MonoDevelop’ editor


// call basic namespace of Unity3D
using UnityEngine;
using System.Collections;

// MonoBehaviour is the base class every script derives from
// class nameofmyscript : derived from MonoBehaviour
public class MyFirstScript : MonoBehaviour {

// Unity3D exclusive function - Use this for initialization
	void Start () {
	
	}// END Start()

	
// Unity3D exclusive function - Update is called once per frame
	void Update () {
	
	}// END Update()

}// END of class MyFirstScript

Variables and Fuctions


// call basic namespace of Unity3D
using UnityEngine;
using System.Collections;

// MonoBehaviour is the base class every script derives from
// scope class nameofmyscript : derived from MonoBehaviour
public class MyFirstScript : MonoBehaviour {
	
	// declare variables
	// scope type nameofvariable = value
	public int myInt = 5;   // you can setup it inside inspector
	private int myInt2 = 6;	// you can't setup it inside inspector
	
	// Unity3D exclusive function - Use this for initialization - void because it has no type
	void Start () {
		myInt = MultiplyByTwo(myInt);
		Debug.Log (myInt);
		
	}// END Start()
	
	// Your custom fuction
	// scope type functionname (type variable)
	public int MultiplyByTwo (int number)
	{
		int ret;
		ret = number * 2;
		return ret;
	}
	
	// Unity3D exclusive function - Update is called once per frame - void because it has no type
	void Update () {
		
	}// END Update()
	
}// END of class MyFirstScript

By |CSharp, Unity3D, Video Games Development|Commenti disabilitati su Unity3D – C# – Basic Syntax

CSharp – for Statement

CSharp – for Statement


using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            int number = 5;

            for(int i = 0; i < number; i++)
                Console.WriteLine(i);

            // the console will close if you press Return on the keyboard
            Console.ReadLine();
        }
    }
}

The result is:
0
1
2
3
4

By |CSharp, Video Games Development|Commenti disabilitati su CSharp – for Statement

CSharp – if Statement

CSharp – if Statement

With if statement you can set up conditional blocks of code.

if – else if – else


using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main()
        {
            Console.Write("Enter a character: ");
            char ch = (char)Console.Read();

            if (Char.IsUpper(ch))
            {
                Console.WriteLine("The character is an uppercase letter.");
            }
            else if (Char.IsLower(ch))
            {
                Console.WriteLine("The character is a lowercase letter.");
            }
            else if (Char.IsDigit(ch))
            {
                Console.WriteLine("The character is a number.");
            }
            else
            {
                Console.WriteLine("The character is not alphanumeric.");
            }
        } // End Main()
    }
}

if – nested


using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main()
        {
            Console.Write("Enter a character: ");
            char c = (char)Console.Read();
            if (Char.IsLetter(c))
            {
                if (Char.IsLower(c))
                {
                    Console.WriteLine("The character is lowercase.");
                }
                else
                {
                    Console.WriteLine("The character is uppercase.");
                }
            }
            else
            {
                Console.WriteLine("The character isn't an alphabetic character.");
            }
        } // End Main()
    }
}

if – boolean


using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main()
        {
            // Change the values of these variables to test the results.
            bool Condition1 = true;
            bool Condition2 = true;
            bool Condition3 = true;
            bool Condition4 = true;

            if (Condition1)
            {
                // Condition1 is true.
            }
            else if (Condition2)
            {
                // Condition1 is false and Condition2 is true.
            }
            else if (Condition3)
            {
                if (Condition4)
                {
                    // Condition1 and Condition2 are false. Condition3 and Condition4 are true.
                }
                else
                {
                    // Condition1, Condition2, and Condition4 are false. Condition3 is true.
                }
            }
            else
            {
                // Condition1, Condition2, and Condition3 are false.
            }
        } // End Main()
    }
}

if – AND NOT


using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main()
        {
            // NOT
            bool result = true;
            if (!result)
            {
                Console.WriteLine("The condition is true (result is false).");
            }
            else
            {
                Console.WriteLine("The condition is false (result is true).");
            }

            // Short-circuit AND
            int m = 9;
            int n = 7;
            int p = 5;
            if (m >= n && m >= p)
            {
                Console.WriteLine("Nothing is larger than m.");
            }

            // AND and NOT
            if (m >= n && !(p > m))
            {
                Console.WriteLine("Nothing is larger than m.");
            }

            // Short-circuit OR
            if (m > n || m > p)
            {
                Console.WriteLine("m isn't the smallest.");
            }

            // NOT and OR
            m = 4;
            if (!(m >= n || m >= p))
            {
                Console.WriteLine("Now m is the smallest.");
            }
            // Output:
            // The condition is false (result is true).
            // Nothing is larger than m.
            // Nothing is larger than m.
            // m isn't the smallest.
            // Now m is the smallest.
        } // End Main()
    }
}

My Website: http://www.lucedigitale.com

References:
http://msdn.microsoft.com/it-it/library/5011f09h.aspx
http://csharp.net-tutorials.com/basics/if-statement/

By |CSharp, Video Games Development|Commenti disabilitati su CSharp – if Statement