csharp

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 – Enum

CSharp – Enum

Quando si definisce una variabile di tipo enumerativo, ad essa viene associato un insieme di costanti intere chiamato insieme dell’enumerazione. La variabile può contenere una qualsiasi delle costanti definite.

Syntax:


enum <enum_name> 
{ 
    enumeration list 
};

Example:


using System;
namespace EnumApplication
{
   class EnumProgram
   {
      enum Days { Sun, Mon, tue, Wed, thu, Fri, Sat };

      static void Main(string[] args)
      {
         int WeekdayStart = (int)Days.Mon;
         int WeekdayEnd = (int)Days.Fri;
         Console.WriteLine("Monday: {0}", WeekdayStart);
         Console.WriteLine("Friday: {0}", WeekdayEnd);
         Console.ReadKey();
      }
   }
}

The rEsult is

Monday: 1
Friday: 5

By |CSharp|Commenti disabilitati su CSharp – Enum

CSharp – Conversions

CSharp – Conversions

Type conversion is basically converting one type of data to another type.

Example:


using System;
using System.Collections;

namespace ConsoleApplication1
{
    class StringConversion
    {
        static void Main(string[] args)
        {
            int i = 75;
            float f = 53.005f;
            double d = 2345.7652;
            bool b = true;

            Console.WriteLine(i.ToString());
            Console.WriteLine(f.ToString());
            Console.WriteLine(d.ToString());
            Console.WriteLine(b.ToString());
            Console.ReadKey();

        }
    }
}

The result is:

75
53.005
2345.7652
True

Conversion Methods:

ToBoolean()
Converts a type to a Boolean value, where possible.

ToByte()
Converts a type to a byte.

ToChar()
Converts a type to a single Unicode character, where possible.

ToDateTime()
Converts a type (integer or string type) to date-time structures.

ToDecimal()
Converts a floating point or integer type to a decimal type.

ToDouble()
Converts a type to a double type.

ToInt16()
Converts a type to a 16-bit integer.

ToInt32()
Converts a type to a 32-bit integer.

ToInt64()
Converts a type to a 64-bit integer.

ToSbyte()
Converts a type to a signed byte type.

ToSingle()
Converts a type to a small floating point number.

ToString()
Converts a type to a string.

ToType()
Converts a type to a specified type.

ToUInt16()
Converts a type to an unsigned int type.

ToUInt32()
Converts a type to an unsigned long type.

ToUInt64()
Converts a type to an unsigned big integer.

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

Ref: http://www.tutorialspoint.com/csharp/csharp_type_conversion.htm

By |CSharp|Commenti disabilitati su CSharp – Conversions

CSharp – Strings

CSharp – Strings

In C#, you can use strings as:
– string keyword to declare a string variable (alias for the System.String class)
– array of characters

How you can use string


using System;
using System.Collections;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            //from string literal and string concatenation
            string fname, lname;
            fname = "Rowan";
            lname = "Atkinson";

            string fullname = fname + lname;
            Console.WriteLine("Full Name: {0}", fullname);

            //by using string constructor
            char[] letters = { 'H', 'e', 'l', 'l', 'o' };
            string greetings = new string(letters);
            Console.WriteLine("Greetings: {0}", greetings);

            //methods returning string
            string[] sarray = { "Hello", "From", "Tutorials", "Point" };
            string message = String.Join(" ", sarray);
            Console.WriteLine("Message: {0}", message);

            //formatting method to convert a value 
            DateTime waiting = new DateTime(2012, 10, 10, 17, 58, 1);
            string chat = String.Format("Message sent at {0:t} on {0:D}",
            waiting);
            Console.WriteLine("Message: {0}", chat);
            Console.ReadKey();
        }
    }
}

The result is:

Full Name: Rowan Atkinson
Greetings: Hello
Message: Hello From Tutorials Point
Message: Message sent at 5:58 PM on Wednesday, October 10, 2012

Comparing Strings


using System;
using System.Collections;

namespace ConsoleApplication1
{
    class StringProg
    {
        static void Main(string[] args)
        {
            string str1 = "This is test";
            string str2 = "This is text";

            if (String.Compare(str1, str2) == 0)
            {
                Console.WriteLine(str1 + " and " + str2 + " are equal.");
            }
            else
            {
                Console.WriteLine(str1 + " and " + str2 + " are not equal.");
            }
            Console.ReadKey();
        }
    }
}

The result is:

This is test and This is text are not equal.

Detect a string


using System;

namespace ConsoleApplication1
{
    class StringProg
    {
        static void Main(string[] args)
        {
            string str = "This is test";
            if (str.Contains("test"))
            {
                Console.WriteLine("The sequence 'test' was found.");
            }
            Console.ReadKey();
        }
    }
}

The result is:

The sequence ‘test’ was found.

Get a substring


using System;

namespace StringApplication
{
   class StringProg
   {
      static void Main(string[] args)
      {
         string str = "Last night I dreamt of San Pedro";
         Console.WriteLine(str);
         string substr = str.Substring(23); // from 24th character to the end
         Console.WriteLine(substr);
      }
      Console.ReadKey() ;
   }
}

The result is:

San Pedro

Joining Strings


using System;

namespace StringApplication
{
   class StringProg
   {
      static void Main(string[] args)
      {
         string[] starray = new string[]{"Down the way nights are dark",
         "And the sun shines daily on the mountain top",
         "I took a trip on a sailing ship",
         "And when I reached Jamaica",
         "I made a stop"};

         string str = String.Join("\n", starray);
         Console.WriteLine(str);
      }
      Console.ReadKey() ;
   }
}

The result is:

Down the way nights are dark
And the sun shines daily on the mountain top
I took a trip on a sailing ship
And when I reached Jamaica
I made a stop

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

Ref: http://www.tutorialspoint.com/csharp/csharp_strings.htm

By |CSharp|Commenti disabilitati su CSharp – Strings