• 240 likes • 259 Views
Yingcai Xiao. Programming and Debugging in Unity Yingcai Xiao. Programming in Unity with C#. Scripting in Unity. Unity supports: C# for heavy duty programing JavaScripts for simple interactions Boo: “compiled Python”, CLI, .NET, Mono compatible. C#.
E N D
Yingcai Xiao Programming and Debugging in Unity Yingcai Xiao
Scripting in Unity • Unity supports: • C# for heavy duty programing • JavaScripts for simple interactions • Boo: “compiled Python”, CLI, .NET, Mono compatible
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.
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
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; }
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.
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++.
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. Garbage Collection
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 ( … ) { … } catch ( … ) { … } } finally { // Always come here even if there were exceptions. if (file != null) file.Close (); } Example of Exception Handling
http://www.tutorialspoint.com/csharp/ http://www.cs.uakron.edu/~xiao/windows/syllabus.html C# Books and Tutorials
Two ways of debugging: • Print to a console window (EDP-Unity5.zip). • void SetCountText() { • count ++; • countText.text = “Count:” + count.ToString(); // to score board • print(“countText.text = ” + countText.text); // to console • } • (2) Use a debugger that works with Unity3D.
Debugging in Unity with a Debugger • Unity works with Mono and Visual Studio debugger. • Mono debugger comes with Unity. • Visual Studio is available free for CS students: http://www.cs.uakron.edu/~xiao/msdnaa.html • 2. Select a editor/debugger to use: • Unity->Edit->Preferences->External Script Editor • Select an editor of your choice from a list which Unity detected on your computer. • (We will use Mono for the demonstration.)
Debugging in Unity with a Debugger • Activate the external editor: • Unity->Project Tab->Assets->Script • Double click on “C# Player”, the selected debugger will start to run in a separate window, showing the Player.cs code. • 4. Now you can debug your code in the debugger window. • 5. To insert a breakpoints, move your mouse to the grey strip to the left side of the code and click. Create as many breakpoints as needed and save the script.
Debugging in Unity 6. If there is only one Unity IDE is running, the debugger will attach the process of the running unity IDE. Otherwise, it will ask you to select which Unity process to attach. 7. Go to Unity IDE and play the game. 8. The game pauses when it comes across one of your reakpoints and the debugger window becomes the active window. All input will go to the debugger which controls the game from this point on.
Visual Studio window showing “Attach to Unity” menu for selecting a Unity process to attach when more than one Unity is running.
Debugging in Unity 9. In the debugger window, hover on variables to read their values. 10. Click on Continue to run the game to the next breakpoint. Unity IDE will take over the input before the breakpoint. 11. Explore other capabilities of the debugger: Watch, Call Stack, Run Over, Run Into, …