Presentation is loading. Please wait.

Presentation is loading. Please wait.

Yingcai Xiao Programming and Debugging in Unity Yingcai Xiao.

Similar presentations


Presentation on theme: "Yingcai Xiao Programming and Debugging in Unity Yingcai Xiao."— Presentation transcript:

1 Yingcai Xiao Programming and Debugging in Unity 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# 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 class Rectangle { // Fields protected int width = 1; protected int height = 1; // Methods public Rectangle () { } public Rectangle (int cx, int cy) { width = cx; height = cy; } Example: How to define a class in C#

7 // 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. Example: How to define a class (user-defined data type)

8 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++. Example: How to use a class 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 Collectionfree using#include Single inheritanceMultiple inheritance

12 Differences between C++ & C# DifferenceC++C# Ending a block with a semicolon Class myClass{ … }; Class myClass{ … } Object instance creation myClass myObject; //myObject is an instance myClass *myPointer = new myClass(); //myPointer is a pointer to an instance myClass myReference = new myClass(); //myReference is a reference (an internal pointer) to an instance DereferencingmyPointer-> myReference. Class members Fields: member variables Methods: member functions 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 http://www.tutorialspoint.com/csharp/ http://www.cs.uakron.edu/~xiao/windows/syllabus.html 

14 Debugging in Unity3D (Contributed by Sai Goud Durgappagari)

15 Two ways of debugging: (1)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 free @ DreamSpark http://www.cs.uakron.edu/~xiao/msdnaa.html http://www.cs.uakron.edu/~xiao/msdnaa.html 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. Click on "Attach to Unity" available at the top of editor(highlighted Yellow in below picture). Wait for some time to see Stop Button at the same place at the top of the editor. 5. Then go to Unity 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 "Yingcai Xiao Programming and Debugging in Unity Yingcai Xiao."

Similar presentations


Ads by Google