Unity is a multiplatform game development tool, designed from the start to ease creation. People like very much 3d games. This is a very powerful game editor. Set up your development environment and build your first real-time 3D app with Unity. Time to Complete. 10 minutes + 40 minutes of download/installation time. A multi-platform game that shows a rotating 3D cube.
Unity is a game engine that enables you to develop games in C#. This walkthrough shows how to get started developing and debugging Unity games using Visual Studio for Mac and the Visual Studio for Mac Tools for Unity extension alongside the Unity environment.
Visual Studio for Mac Tools for Unity is a free extension, installed with Visual Studio for Mac. It enables Unity developers to take advantage of the productivity features of Visual Studio for Mac, including excellent IntelliSense support, debugging features, and more.
Objectives
- Learn about Unity development with Visual Studio for Mac
Prerequisites
- Visual Studio for Mac (https://www.visualstudio.com/vs/mac)
- Unity 5.6.1 Personal Edition or higher (https://store.unity.com, requires a unity.com account to run)
Intended Audience
This lab is intended for developers who are familiar with C#, although deep experience is not required.
Task 1: Creating a basic Unity project
Launch Unity. Sign in if requested.
Click New.
Set the Project name to 'UnityLab' and select 3D. Click Create project.
You're now looking at the default Unity interface. It has the scene hierarchy with game objects on the left, a 3D view of the blank scene shown in the middle, a project files pane on the bottom, and inspector and services on the right. Of course, there's a lot more to it than that, but those are few of the more important components.
For developers new to Unity, everything that runs in your app will exist within the context of a scene. A scene file is a single file that contains all sorts of metadata about the resources used in the project for the current scene and its properties. When you package your app for a platform, the resulting app will end up being a collection of one or more scenes, plus any platform-dependent code you add. You can have as many scenes as desired in a project.
The new scene just has a camera and a directional light in it. A scene requires a camera for anything to be visible and an Audio Listener for anything to be audible. These components are attached to a GameObject.
Select the Main Camera object from the Hierarchy pane.
Select the Inspector pane from the right side of the window to review its properties. Camera properties include transform information, background, projection type, field of view, and so on. An Audio Listener component was also added by default, which essentially renders scene audio from a virtual microphone attached to the camera.
Select the Directional Light object. This provides light to the scene so that components like shaders know how to render objects.
Use the Inspector to see that it includes common lighting properties including type, color, intensity, shadow type, and so on.
It is important to point out that projects in Unity are a little different from their Visual Studio for Mac counterparts. In the Project tab on the bottom, right-click the Assets folder and select Reveal in Finder.
Projects contain Assets, Library, ProjectSettings, and Temp folders as you can see. However, the only one that shows up in the interface is the Assets folder. The Library folder is the local cache for imported assets; it holds all metadata for assets. The ProjectSettings folder stores settings you can configure. The Temp folder is used for temporary files from Mono and Unity during the build process. There is also a solution file that you can open in Visual Studio for Mac (UnityLab.sln here).
Close the Finder window and return to Unity.
The Assets folder contains all your assets-art, code, audio, etc. It's empty now, but every single file you bring into your project goes here. This is always the top-level folder in the Unity Editor. But always add and remove files via the Unity interface (or Visual Studio for Mac) and never through the file system directly.
The GameObject is central to development in Unity as almost everything derives from that type, including models, lights, particle systems, and so on. Add a new Cube object to the scene via the GameObject > 3D Object > Cube menu.
Take a quick look at the properties of the new GameObject and see that it has a name, tag, layer, and transform. These properties are common to all GameObjects. In addition, several components were attached to the Cube to provide needed functionality including mesh filter, box collider, and renderer.
Rename the Cube object, which has the name 'Cube' by default, to 'Enemy'. Make sure to press Enter to save the change. This will be the enemy cube in our simple game.
Add another Cube object to the scene using the same process as above, and name this one 'Player'.
Tag the player object 'Player' as well (see Tag drop-down control just under name field). We'll use this in the enemy script to help locate the player game object.
In the Scene view, move the player object away from the enemy object along the Z axis using the mouse. You can move along the Z axis by selecting and dragging the cube by its red panel toward the blue line. Since the cube lives in 3D space, but can only be dragged in 2D each time, the axis on which you drag is especially important.
Move the cube downward and to the right along the axis. This updates the Transform.Position property in the Inspector. Be sure to drag to a location similarly to what's shown here to make later steps easier in the lab.
Now you can add some code to drive the enemy logic so that it pursues the player. Right-click the Assets folder in the Project pad and select Create > C# Script.
Name the new C# script 'EnemyAI'.
To attach scripts to game objects drag the newly created script onto the Enemy object in the Hierarchy pane. Now that object will use behaviors from this script.
Select File > Save Scenes to save the current scene. Name it 'MyScene'.
Task 2: Working with Visual Studio for Mac Tools for Unity
The best way to edit C# code is to use Visual Studio for Mac. You can configure Unity to use Visual Studio for Mac as its default handler. Select Unity > Preferences.
Select the External Tools tab. From the External Script Editor dropdown, select Browse and select Applications/Visual Studio.app. Alternatively, if there's already a Visual Studio option, just select that.
Unity is now configured to use Visual Studio for Mac for script editing. Close the Unity Preferences dialog.
Double-click EnemyAI.cs to open it in Visual Studio for Mac.
The Visual Studio solution is straightforward. It contains an Assets folder (the same one from Finder) and the EnemyAI.cs script created earlier. In more sophisticated projects, the hierarchy will likely look different than what you see in Unity.
EnemyAI.cs is open in the editor. The initial script just contains stubs for the Start and Update methods.
Replace the initial enemy code with the code below.
Take a quick look at the simple enemy behavior that is defined here. In the Start method, we get a reference to the player object (by its tag), as well as its transform. In the Update method, which is called every frame, the enemy will move towards the player object. The keywords and names use color coding to make it easier to understand the codebase in Visual Studio for Mac.
Save the changes to the enemy script in Visual Studio for Mac.
Task 3: Debugging the Unity project
Set a breakpoint on the first line of code in the Start method. You can either click in the editor margin at the target line or place cursor on the line and press F9.
Click the Start Debugging button or press F5. This will build the project and attach it to Unity for debugging.
Return to Unity and click the Run button to start the game.
The breakpoint should be hit and you can now use the Visual Studio for Mac debugging tools.
From the Locals pad, locate the this pointer, which references an EnemyAI object. Expand the reference and see that you can browse the associated members like Speed.
Remove the breakpoint from the Start method the same way it was added-by either clicking it in the margin or selecting the line and press F9.
Press F10 to step over the first line of code that finds the Player game object using a tag as parameter.
Hover the mouse cursor over the player variable within the code editor window to view its associated members. You can even expand the overlay to view child properties.
Press F5 or press the Run button to continue execution. Return to Unity to see the enemy cube repeatedly approach the player cube. You may need to adjust the camera if it's not visible.
Switch back to Visual Studio for Mac and set a breakpoint on the first line of the Update method. It should be hit immediately.
Suppose the speed is too fast and we want to test the impact of the change without restarting the app. Locate the Speed variable within the Autos or Locals window and then change it to '10' and press Enter.
Remove the breakpoint and press F5 to resume execution.
Return to Unity to view the running application. The enemy cube is now moving at a fifth of the original speed.
Stop the Unity app by clicking the Play button again.
Return to Visual Studio for Mac. Stop the debugging session by clicking the Stop button.
Task 4: Exploring Unity features in Visual Studio for Mac
Visual Studio for Mac provides quick access to Unity documentation within the code editor. Place the cursor somewhere on the Vector3 symbol within the Update method and press ⌘ Command + '.
A new browser window opens to the documentation for Vector3. Close the browser window when satisfied.
Visual Studio for Mac also provides some helpers to quickly create Unity behavior classes. From Solution Explorer, right-click Assets and select Add > New MonoBehaviour.
The newly created class provides stubs for the Start and Update methods. After the closing brace of the Update method, start typing 'onmouseup'. As you type, notice that Visual Studio's IntelliSense quickly zeros in on the method you're planning to implement. Select it from the provided autocomplete list. It will fill out a method stub for you, including any parameters.
Inside the OnMouseUp method, type 'base.' to see all of the base methods available to call. You can also explore the different overloads of each function using the paging option in the top-right corner of the IntelliSense flyout.
Visual Studio for Mac also enables you to easily define new shaders. From Solution Explorer, right-click Assets and select Add > New Shader.
The shader file format gets full color and font treatment to make it easier to read and understand.
Return to Unity. You'll see that since Visual Studio for Mac works with the same project system, changes made in either place are automatically synchronized with the other. Now it's easy to always use the best tool for the task.
Summary
In this lab, you've learned how to get started creating a game with Unity and Visual Studio for Mac. See https://unity3d.com/learn to learn more about Unity.
Rider for Unity
Using Rider to write C# makes me happy. I have never seen code refactoring tools that actually work - always without exception. It's amazing when you can rely on it.
Joachim Ante,
Unity CTO & Founder
Powerful cross-platform C# Editor
JetBrains Rider is a fast and powerful
Easy start with Unity
Rider has Unity support built in, and the very first time you open a Unity solution it will automatically configure Unity to use Rider as the default editor for C# scripts and shader files. Once done, double-clicking a C# script or shader asset will open the solution in Rider.
Control Unity editor
Thanks to the integrated two-way communication, you can switch into and out of Play mode, and pause and step a single frame without ever leaving Rider! The toolbar includes game view buttons Play, Pause, and Step, which correspond to the same buttons in Unity Editor and control the same behavior as Unity Editor does. A small Unity icon in the status bar will indicate whether Unity Editor is connected, refreshing, or in Play mode. Any changes applied in Rider in Edit mode will be instantly delivered to Unity Editor.
Coding hints
Rider provides top-notch code analysis for C#, and that includes Unity-specific code inspections and quick-fixes for them.
For example, Rider will warn you against using inefficient string literal comparison with the tag
property, and will provide a quick-fix to rewrite this as a call to CompareTag
.
Similarly, Rider will warn you if you try to use the new
keyword to create a new instance of a class deriving from MonoBehaviour
or ScriptableObject
. Just press AltEnter to have Rider fix the problem for you.
Debug Unity with ease
Rider makes it very easy to debug your C# scripts as they run in the Unity editor. Everything is already configured, and you just need to hit the Debug toolbar button, menu item or keyboard shortcut to attach the debugger to the running Unity editor. You can set breakpoints in the editor, step through your code, look at locals and watch values, and evaluate expressions.
With Rider, you can even debug the code that you don’t have debugging information for. Rider automatically decompiles external libraries on-the-fly, allowing you to debug the decompiled code, step into functions, set breakpoints, view and set locals and variables.
Run Unity tests
Rider allows you to run tests that interact with Unity’s API, and which can step through single frames, all from within Rider. And of course, you can explore the results of your Unity-specific tests, just like you would normal tests – you can filter by result, and click on stack traces to navigate your code.
Unity console logs
The Unity Logs tool window with hyperlinked stack traces lets you explore the same events that are logged in the Unity editor console. It allows filtering by category (error, warning, or message), hides events based on when they were logged (in Play or Edit mode), and lets you easily navigate to any source file, class, method, or property mentioned in the log entry. Unity Logs cleans its output automatically after every switch to Play mode.
Performance highlightings
Rider helps you write better-performing Unity C# code. To do this, it highlights expensive Unity APIs inside methods that get called every frame, like Update and coroutines. Rider even highlights calls to methods that indirectly use expensive Unity APIs!
Besides that, Rider has many Unity-specific performance inspections and appropriate quick-fixes. It is aware of code patterns that are poor performance and can suggest automatic fixes for them, such as using a different API or overload, or caching values.
Find Usages inside Unity files
Find Usages now includes Unity scenes, assets, and prefabs. If you search for a class or method used as an event handler, Rider shows where it’s used in your Unity files, grouped by type, component name, and parent game objects. Even better, double-clicking the item highlights the usage directly in the Unity Editor.
Pulling this information from Unity files also means that Rider highlights event handler methods as being implicitly used. The implicit usage highlights are turned into Code Vision links, so you can see at a glance what classes, methods, and fields are implicitly used by Unity. And clicking the link will find those usages, even inside Unity files.
Shader support
Rider also adds support for .shader files, with syntax highlighting, code folding, brace matching and more. Syntax errors in the ShaderLab sections are highlighted, todo comments are pulled into the To Do Explorer and color properties are highlighted, with a color picker for editing. Simple word completion is available throughout the file, including in Cg/HLSL blocks.
Explore Unity docs
Rider displays external documentation for Unity symbols. You can either click the icon from the Quick Documentation pop-up (CtrlShiftF1 if using the Visual Studio keymap) or use the View External Documentation action (ShiftF1) directly, to navigate to locally installed documentation, or to Unity’s hosted docs if they are not available locally.
See how the Unity support in JetBrains Rider compares to Visual Studio and Visual Studio for Mac.
IDE for Unity development
Free 30-day trial
Features
Code analysis and quick-fixes
Rider boasts 2500+ live code inspections, with over 1000 automated quick-fixes to resolve detected issues individually or in bulk. Solution-wide error analysis will monitor code issues and let you know if anything goes wrong, even in files that are not currently open.
Code navigation
You can jump to any file, type, or member in your code base in no time, as well as find settings and actions. Find usages of any symbol, or navigate from a symbol to base and derived symbols, extension methods or implementations.
Refactorings
Rider provides 50+ global automated refactorings, as well as 450+ smaller context actions for local code transformations. Rename, extract methods, interfaces and classes, move and copy types, use alternative syntax, and a lot more!.
And more
There’s unit testing support, code cleanup, integrated version control, local history to save your code between commits, NuGet support, database tooling and more. Rider can be easily extended with plugins, from Markdown support to VIM keyboard bindings.
Free Photo Editor For Mac
Customers
Far and away the best Unity IDE for the Mac. Unparalleled debugging and refactoring capabilities.
— Erin Keenan, Engineer, N3twork mobile games company
Unity Editor For Mac Windows 7
Join these companies that already use Rider to develop Unity games.
Having the right tool for the job is essential to compete in any market, games especially. At Yakuto each dev has a JetBrains Toolbox subscription, which we’ve found invaluable for productivity. We work mostly in C# on Macs. This used to mean a VM running ReSharper but with Rider, we’re native to one OS, which is awesome. Unity debugging is now seamless and the extensions add vital Unity-aware context. As a veteran of ReSharper, I feel completely at home in Rider: refactoring, solution-wide analysis, test runners, it’s all there. We couldn’t consider C# development without Rider.
Unity Editor For Mac Installer
— James Gratton, CTO, Yakuto