1 / 24

Yingcai Xiao

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#.

jgoetz
Download Presentation

Yingcai Xiao

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

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. 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. 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

  10. 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

  11. C# vs. C++

  12. Differences between C++ & C#

  13. http://www.tutorialspoint.com/csharp/ http://www.cs.uakron.edu/~xiao/windows/syllabus.html C# Books and Tutorials

  14. Debugging in Unity3D

  15. 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.

  16. 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.)

  17. Debugging in Unity with a Debugger

  18. 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.

  19. 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.

  20. Visual Studio window showing “Attach to Unity” menu for selecting a Unity process to attach when more than one Unity is running.

  21. 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, …

More Related