Presentation is loading. Please wait.

Presentation is loading. Please wait.

Programming and Debugging

Similar presentations


Presentation on theme: "Programming and Debugging"— Presentation transcript:

1 Programming and Debugging
in Unity Yingcai Xiao Yingcai Xiao

2 Programming in Unity with C#

3 Scripting in Unity Unity supports: C# for heavy duty programing
JavaScripts for simple interactions Boo: “compiled Python”, CLI, .NET, Mono compatible

4 C# Allow users to define events.
The de facto programming language for .NET OS platform independent Needs CLR (Common Language Runtime): .NET, Mono. Unity uses Mono. Supports: Class, Struct, Interface, Enum, Delegates Allow users to define events.

5 C# Classes Class: a group of code and data to be instantiated to form objects. Four categories of class members: Fields: member variables Methods: member functions Properties: fields exposed using accessor (get and set) methods Events: notifications a class is capable of firing

6 Example: How to define a class in C#
class Rectangle { // Fields protected int width = 1; protected int height = 1; // Methods public Rectangle () { } public Rectangle (int cx, int cy) width = cx; height = cy; }

7 Example: How to define a class (user-defined data type)
// Accessor Methods public void setWidth(int w) { width = w; } public int getWidth() { return width; } public void setHeight(int h) { height = h; } public int getHeight() { return height; } } // End of Rectangle class // No “;” at the end of class definition.

8 Example: How to use a class in C#
Rectangle rect = new Rectangle(2,4); rect.setHeight(8); rect.setWidth(rect.getWidth() * 2); double darea = (double) (rect.getWidth() * rect.getHeight() ); Note: (1) “Rectangle rect” creats a reference, not object. (2) Treat “Rectangle rect = new Rectangle();” in C# as “Rectangle rect;” in C++. (3) Use “rect.” to dereference in C# instead of “rect->” as in C++.

9 Garbage Collection CLR controls garbage collection.
No need to free memory dynamically allocated with “new” on the heap in C#, hence, no destructors in C#. CLR uses a reference counter to keep track the number of references pointing the memory. CLR will free the memory when the reference counter becomes 0. The Finalize() method (inherited from Object class) is called during garbage collection. CLR decides when to perform garbage collection. Force a garbage collection by calling GC.Collect (). (expensive) Implement a Dispose method and call it when needed to cleanup when an object is not needed any more.

10 Example of Exception Handling
CLR’s Exception Handling Mechanism: try, catch, finally, and throw File file = null; // Do not define it in the try block. Why? try { file = new File ("Readme.txt"); if(file != null) { /* Do the work here. */} } catch (FileNotFoundException e) { Console.WriteLine (e.Message); } catch ( … ) { … } finally { // Always come here even if there were exceptions. if (file != null) file.Close ();

11 C# vs. C++ C# C++ class myClass { } class myClass { } ;
Rectangle rect = new Rectangle(); Rectangle rect; “rect.” to dereference “rect->” to dereference No destructors ~className(); Garbage Collection free using #include Single inheritance Multiple inheritance

12 Differences between C++ & C#
Ending a block with a semicolon Class myClass{ }; } Object instance creation myClass myObject; //myObject is an instance myClass *myPointer = new myClass(); //myPointer is a pointer to an instance myClass myReference //myReference is a reference (an internal pointer) to an instance Dereferencing myPointer-> myReference. Class members Fields: member variables Methods: member functions Properties: fields exposed using accessor (get and set) methods Events: notifications a class is capable of firing Freeing heap memory free(myPointer); Automatically by garbage collection when myReference is out of extent.

13 C# Books and Tutorials

14 Debugging in Unity3D (Contributed by Sai Goud Durgappagari)

15 Two ways of debugging: Print to a console window.
void SetCountText() { count ++; countText.text = “Count:” + count.ToString(); // to score board print(“countText.text = ” + countText.text); // to console } (2) Use the debugger in the IDE. Unity3D uses debugger from Visual Studio.

16 Debugging in Unity 1. Debugging needs Visual Studio installed on your System. Visual Studio is available DreamSpark 2. After opening Unity 5 project and your script in Visual Studio, insert Breakpoints at respective lines of code to Debug 3. To insert Breakpoints, move your mouse to grey strip towards left side of editor and right click. Create as many Breakpoints needed and save the script.

17

18

19

20 Debugging in Unity 4. Debug->Debugging. If there is only one unity IDE is running, it will attach the process of the running unity IDE. Otherwise, it will ask you to select a process to attach if there are more than one unity programs are running. 5. Then go to Unity IDE and play the game. 6. The game pauses when it comes across one of your Breakpoints and Visual Studio gets highlighted.

21

22

23

24

25

26 Debugging in Unity 7. Go to Visual Studio and hover on variables to read their values. 8. Click on Continue to continue Debugging the game.

27

28


Download ppt "Programming and Debugging"

Similar presentations


Ads by Google