Unity Docker



Visual Studio Code can be a great companion to Unity for editing and debugging C# files. All of the C# features are supported and more. In the screen below, you can see code colorization, bracket matching, IntelliSense, CodeLens and that's just the start.

  1. Unity Docker Gitlab
  2. Unity Docker Container
  3. Unity Docker
  4. Unity Dockery

Unity Development with VS Code. Visual Studio Code can be a great companion to Unity for editing and debugging C# files. All of the C# features are supported and more. In the screen below, you can see code colorization, bracket matching, IntelliSense, CodeLens and that's just the start. We and third parties use cookies or similar technologies ('Cookies') as described below to collect and process personal data, such as your IP address or browser information. Use docker run -it -name MyUnityDockerContainer -p 8080:5900 my-unity-image:tagname to create the Docker container and enter it with your shell. The -p switch exposes a port from the Docker container to the host machine so we can communicate over the network interface. Find this GUI tool & more on the Unity Asset Store. Use the Dock Panel from Vadim Andriyanov on your next project. Sprint into Spring Sale is on: Get 50% off top assets and score extra savings with coupon code SPRING2021.

Read on to find out how to configure Unity and your project to get the best possible experience.

Prerequisites

From Using .NET Core in Visual Studio Code:

  1. Install the .NET Core SDK, which includes the Runtime and the dotnet command.

  2. [Windows only] Logout or restart Windows to allow changes to %PATH% to take effect.

  3. [macOS only] To avoid seeing 'Some projects have trouble loading. Please review the output for more details', make sure to install the latest stable Mono release.

    Note: This version of Mono, which is installed into your system, will not interfere with the version of MonoDevelop that is installed by Unity.

  4. Install the C# extension from the VS Code Marketplace.

Setup VS Code as Unity Script Editor

Open up Unity Preferences, External Tools, then browse for the Visual Studio Code executable as External Script Editor.

The Visual Studio Code executable can be found at /Applications/Visual Studio Code.app on macOS, %localappdata%ProgramsMicrosoft VS CodeCode.exe on Windows by default.

Unity has built-in support for opening scripts in Visual Studio Code as an external script editor on Windows and macOS. Unity will detect when Visual Studio Code is selected as an external script editor and pass the correct arguments to it when opening scripts from Unity. Unity will also set up a default .vscode/settings.json with file excludes, if it does not already exist (from Unity 5.5 Release notes).

Unity version 2019.2 or above

Since 2019.2, it is required to use the Visual Studio Code editor package. The built-in support for opening scripts from Unity and getting csproj and sln files generated has been removed.

Editing Evolved

With the solution file selected, you are now ready to start editing with VS Code. Here is a list of some of the things you can expect: Update mac os x 10.5 8 to 10.6 free.

  • Syntax Highlighting
  • Bracket matching
  • IntelliSense
  • Snippets
  • CodeLens
  • Peek
  • Go-to Definition
  • Code Actions/Lightbulbs
  • Go to symbol
  • Hover

Two topics that will help you are Basic Editing and C#. In the image below, you can see VS Code showing hover context, peeking references and more.

Unity Extensions

The community is continually developing more and more valuable extensions for Unity. Here are some popular extensions that you might find useful. You can search for more extensions in the VS Code Extension Marketplace.

The extensions shown above are dynamically queried. Select an extension tile above to read the description and reviews to decide which extension is best for you. See more in the Marketplace.

Enabling code completion (For recent versions of Unity)

If you are installing VS Code for the first time, you might be missing targeting packs required for Unity's code-completion (IntelliSense) in VS Code.

Targeting pack download links:

Steps:

  1. Stop VS Code or Unity running.
  2. Download and install the targeting pack for your targeted framework version / preferred version from one of the above links.
  3. Start Unity.
  4. Create and/or open an existing script in VS Code, through Unity, and you should now see code completions.

Enabling Unity warnings

Unity has a set of custom C# warnings, called analyzers, that check for common issues with your source code. These analyzers ship out of the box with Visual Studio but need to be set up manually in Visual Studio Code.

Due to how Unity handles its .csproj files, it does not seem possible to install packages automatically. You will need to download the analyzers from the NuGet website manually. When you're done, open the package file using a tool such as 7zip and extract Microsoft.Unity.Analyzers.dll onto your project's root folder. You can place it inside a folder named NuGet, for example. Do not place it inside Assets or Packages, as that will cause Unity to try to process the .dll, which will make it output an error in the console.

Next, create an omnisharp.json file at the root folder of your project, as explained here. Analyzer support in OmniSharp is experimental at the moment, so we need to enable it explicitly. We also need to point it to the .dll file we just extracted.

Your omnisharp.json file should end up looking like this:

where './NuGet/microsoft.unity.analyzers.1.9.0' is a relative path pointing to the folder containing the .dll file. Depending on where you placed it, your path may look different.

The Unity analyzers should now be working in your project. You can test them by creating an empty FixedUpdate() method inside one of your MonoBehavior classes, which should trigger a The Unity message 'FixedUpdate' is empty warning (UNT0001).

Docker

Note that while it is possible to activate these analyzers, the suppressors they ship with the package (that turn off other C# warnings that may conflict with these custom ones) may not be picked up by OmniSharp at the moment, according to this thread. You can still turn off specific rules manually by following these steps:

  1. Create a .editorconfig file in your project's root folder (next to Unity's .csproj files).
  2. Add the following contents to the file:

root=true tells OmniSharp that this is your project root and it should stop looking for parent .editorconfig files outside of this folder.

dotnet_diagnostic.IDE0051.severity = none is an example of turning off the analyzer with ID IDE0051 by setting its severity level to none. You can read more about these settings in the Analyzer overview. You can add as many of these rules as you wish to this file.

[*.cs] indicates that our custom rules should apply to all C# scripts (files with the .cs extension).

You are now ready to code in Visual Studio Code, while getting the same warnings as you would when using Visual Studio!

Unity Docker

Next steps

Unity Docker Gitlab

Read on to learn more about:

  • Basic Editing - Learn about the powerful VS Code editor.
  • Code Navigation - Move quickly through your source code.
  • Debugging - how to use the debugger with your project
  • C# - learn about the C# support in VS Code

Common questions

I don't have IntelliSense

Unity Docker Container

You need to ensure that your solution is open in VS Code (not just a single file). Open the folder with your solution and you usually will not need to do anything else. If for some reason VS Code has not selected the right solution context, you can change the selected project by clicking on the OmniSharp flame icon on the status bar.

Choose the -CSharp version of the solution file and VS Code will light up.

How can I change the file exclusions?

Unity creates a number of additional files that can clutter your workspace in VS Code. You can easily hide these so that you can focus on the files you actually want to edit.

To do this, add the following JSON to your workspace settings.

As you can see below this will clean things up a lot..

Unity docker containerUnity Docker
BeforeAfter

How can I debug Unity?

Install the Debugger for Unity extension. And check out Debugging with VS Code to learn more about VS Code debugging support.

5/2/2017

TL;DR: For in-depth info skip to next paragraph. Use the ready baked images available on DockerHub or follow the steps beneath to create your own activated Unity Linux on a Docker image.

Intro

I have quite a few Unity based code repositories and naturally I wanted to automate most tasks. Mainly running tests and building .unitypackage files for distribution and easy integration. Normally I use Submodules to integrate my code in other projects. But sometimes on Gamejams or Prototypes it is nice to just drag in a .unitypackage file. Of course you can run Unity on a Windows Gitlab Runner if you host your own. But I wanted to use shared runners and asked myself if it is possible with Unity Linux and Docker.So I started out to run Unity on Linux inside a Docker container. The main problem was the activation of the Unity personal edition. As of this time you can only activate Unity via command-line when using a serial number (You can check the options HERE). You only have a serial number when you have a version above Unity Personal. I’m using Unity Personal so I could not use the easy command-line activation. To log in to your Unity Personal edition you have to have a GUI and there is where the problem starts. Here is my guide on how to activate Unity Personal inside a Docker container:

Steps

Unity

Unity Docker

  1. You need a Dockerfile to create an image with Unity and all needed dependencies. You can use the one I am using if you want a quick start, but you can also look through the needed dependencies and create your own Dockerfile. The Dockerfile uses a bash script to download Unity from a certain link. You can aquire the links from HERE. The GitHub repository that is linked above uses branches for the different Unity versions. If you switch to a branch you can see the link used in the get-unity.sh file.Please note that the other steps in the Dockerfile (e.g. copying the .ulf and .pem file) are the result of the following steps. If you want to follow this post with a fresh slate then comment out the ADD steps that copy the .pem and .ulf file to the image. If you want to you can use the ready built images but keep in mind that the image is activated on the account I used when creating the license files.

  2. Install Docker by following the instructions HERE

  3. Create your Docker image by running cd <DIRECTORY_WITH_DOCKERFILE> and docker image build --tag my-unity-image:tagname .. If you are on Windows make sure your Dockerfile and any other file used by Docker uses Unix-Style line endings. This step can take some time. Especially the “Starting Unity3D installer download…” step. It took 30 min on my machine.

  4. Get inside that container. The following commands we only want to run once and then extract the results. That’s why we will continue to run commands inside the Docker container instead of adding them to the Dockerfile. Use docker run -it --name MyUnityDockerContainer -p 8080:5900 my-unity-image:tagname to create the Docker container and enter it with your shell. The -p switch exposes a port from the Docker container to the host machine so we can communicate over the network interface. We need this later.

  5. To use Unity’s launcher GUI to activate this version of Unity we need a VNC server and client. We can use XVFB to create a ‘fake’ window server. Run the following bash commands:

  6. Commit changes from installing VNC server docker commit MyUnityDockerContainer my-unity-image:tagname. This steps creates a revision of our changes to the image. It will be helpful in the next steps.

  7. Remove the docker container docker container rm MyUnityDockerContainer. This seems weird but we want to create a new container based on our newly created image revision. The image a container is using cannot be updated. We can only remove and recreate the container.

  8. Start the container in attach mode docker run -it --name MyUnityDockerContainer -p 8080:5900 my-unity-image:tagname. Your shell should now run inside the Docker container and not on your host machine. You can now start the VNC server inside the Docker container using x11vnc -forever -usepw -create.

  9. Install and connect VNC client on your host machine. I use RealVNC as my VNC client but any modern VNC client should work. Connect to your running docker container using localhost:8080 and the password (in this example: ‘1234’) used when starting x11vnc.

  10. Mac os 10.12 download dmg. Start Unity inside the docker container VNC session by executing /opt/Unity/Editor/Unity. There will be some prompts from Unity, just hit OK on those.

  11. Activate Unity using the normal launcher GUI. I would recommend to not use your private/main Unity account and instead create a new Account that is then used in your build environment. Continue the on screen steps until the launcher lets you create a new project. Yes, you have to do the survey! :)

    Unity inside VNC client Looks interesting but it works.

  12. Now Unity is activated in our Docker container but what now? We want to be able to create a Docker image with an already registered Unity. For this we have to find out what Unity did to our OS when we activated it. Luckily Docker does feature a diff command. Yes, you can diff a complete OS! Open a new shell window/tab and lets run the diff by using the command docker diff MyUnityDockerContainer on our host machine. Now we can look through the changes and after some searching we can see two files which are suspiciously placed: /root/.local/share/unity3d/Unity/Unity_v5.x.ulf and /root/.local/share/unity3d/Certificates/CACerts.pem.

  13. Copy the files out of the container using docker cp MyUnityDockerContainer:/root/.local/share/unity3d/Unity/Unity_v5.x.ulf Unity_v5.x.ulf and docker cp MyUnityDockerContainer:/root/.local/share/unity3d/Certificates/CACerts.pem CACerts.pem. Calibri mac download free. The files are now placed in the current directory where your shell is placed.

  14. With the license files we can now enhance the Dockerfile to add the files to the image. Look at the finished Dockerfile linked above to see how to add the files in the image creation process. The Dockerfile can be used on DockerHub to create images on your account. You can also use it locally with docker image build <PATH_TO_DOCKERFILE> to create an image.

  15. With these added license files Unity should start correctly and see valid license files which are required for any kind of Unity operation. Hooray!

Conclusion

This concludes the steps I used to activate Unity Personal inside a Docker container. You can of course build your own Unity images or you can use my DockerHub Repository where I maintain most Unity Linux versions as Docker images. You can also create a Docker image that is based on one of my images in case you have to add something to the image.

Unity Dockery

I hope you enjoyed this little guide and all steps work on your machine.