Using Symlinks For Unity Game Development
Unity 3D & C#
Unity3D is a powerful suite of tools (Project IDE, Code IDE, run-time) for game development.
As always, RivelloMultimediaConsulting.com/unity/ will be the central location for deep articles and tutorials, Facebook.com/RivelloMultimediaConsulting (like us!) will engage the growing RMC+Unity community, and for the latest opinions and cool links follow me at Twitter.com/srivello.
There is incredible momentum in the Unity3D product and its community. I recently posted “The Best C# Code Naming Conventions” and “Unity Best Practices”. I came across one best-practice that requires a bit of setup, so here it is; using symlinks for game development.
Unity’s Weaknesses
The Unity Editor IDE does not facilitate collaboration between developers nor collaboration between projects.
- 1.Want to use version control? Git? SVN? You can, but it is not easy. While certainly possible I suspect these strategies are purposefully kept difficult to encourage developers to purchase the Unity Collaboration & Unity Asset Server packaged features which are available for sale on the official Unity Asset Store.
- 2. Want to share your code between projects (local or remote)? You can, and it is easy with custom packages (here is how), but it is not great. You are sharing a COPY of your code. Update your code and you have to re-export and re-import it everywhere. Difficult! I suspect its difficult for the same reason as above.
I’m happily surprised how great Unity Indie (free license) is. I really am. Yet, I’m still disappointed at these shortcomings.
Fortunately there are some free workarounds.
Workaround 1. Version Control
You can indeed use any version control to collaborate with other developers (local and remote). Its a bit tricky to setup (here is how, and here is some GIT advice). Enjoy!
Workaround 2. Symlinks
On Window (here1, here2), MacOSX (here), and Linux (here) you can create symlinks. In computing, a symbolic link (also symlink or soft link) is a special type of file that contains a reference to another file or directory. A symlink is SIMILAR to a shortcut (Windows) or alias (Mac). You click the symlink in Finder (Mac) or Windows Explorer (Windows) and it opens the original file. You’ve probably used them often. The added benefit of symlinks is that applications (most) cannot tell the difference between a symlink and the original file or folder. It is this difference we will exploit here.
Here are the instructions for using Symlinks with Unity on MacOSX to share code between three unity projects. The setup would be similar on Windows/Linux. Give it a try on your platform and post below with any suggestions!
Demo
Create Your Code Library
- Open the Unity Editor IDE
- File -> New Project… -> “/Users/srivello/Desktop/MyUnityLibrary”
- Assets -> Create -> Folder -> “MyUnityLibraryScripts”. This folder (and any subfolders) will be the location of 100% of the code you want to share. Let’s add one file to share.
- Assets -> Create -> C# Script -> “MyUnityLibraryScript” with the contents shown below.
- Drag “MyUnityLibraryScript” into “MyUnityLibraryScripts” within the Project window.
- File -> Save Project
- File -> Save Scene As… -> “MyUnityLibraryScene”
[actionscript3]<br />//SAMPLE CONTENTS FOR THE CLASS.<br />//ANY VALID CODE IS A GOOD TEST<br />public class MyUnityLibraryScript<br />{<br />//Test<br />public string sampleVariable = "Hello World";<br />}<br />[/actionscript3]
Create Your Game 1 Project
- Open the Unity Editor IDE
- File -> New Project… -> “/Users/srivello/Desktop/MyUnityGame1”
- Assets -> Create -> Folder -> “MyUnityLibraryScripts” with the contents shown below.
- File -> Save Project
- File -> Save Scene As… -> “MyUnityGame1”
Create Your Game 2 Project
- Repeat the same steps for ‘MyUnityGame2’.
And now here are a few solutions to ‘share’ the code between the projects. Ideally A would work in all cases on all operating systems. I’m still working on that.
A. Create Symlink
UPDATE: A) The following fails on mac. Mac does not allow ‘hard links of folders’ which Unity wants. Mac can do soft links but the Unity Editor IDE does not recognize ‘soft links of folders’. Anyone have the following 3 lines working on PC? Share the lines as a comment below. Anyone have a workaround for MAC?
- Open the native MacOSX application ‘Terminal.app’
- Type “ln -s /Users/srivello/Desktop/MyUnityLibrary/Assets/MyUnityLibraryScripts/ /Users/srivello/Desktop/MyUnityGame1/Assets/MyUnityLibraryScripts” (without the quotes). This creates a symlink from the EXISTING folder in MyUnityLibrary to the NEW (symlink) folder in MyUnityGame1. Use absolute paths as I show here.
- Type “ln -s /Users/srivello/Desktop/MyUnityLibrary/Assets/MyUnityLibraryScripts/ /Users/srivello/Desktop/MyUnityGame2/Assets/MyUnityLibraryScripts” (without the quotes).
B. Use Rsync
- You can download this free Mac App and it works great. (https://www.macupdate.com/app/mac/20983/arrsync) But you have to manually click ‘sync’ whenever you want to refresh the folder across your projects. Not bad. Anyone have a better workaround?
Summary
So what have we done here? Now both MyUnityGame1 project and MyUnityGame2 project have access to the code from MyUnityLibrary. Currently we have just one class in there. ANY of the 3 projects can be opened and you can edit/add new files. They all share a reference to the original MyUnityLibraryScripts folder. No need to COPY scripts! Great. No need to Export packages or Import packages! Great.
Gotchas!
The above workflow solves much of the confusion and problems reported by the community. However there are still some issues with my technique. Here are a few of the issues;
- Symlinks + Unity ON MAC don’t work together!!! Here is the full story – http://forum.unity3d.com/threads/104374-Using-links-%28Sym-amp-Hard%29-to-create-a-common-folder-for-all-Unity-Projects
- Prefabs – There are issues with prefabs via symlinks. Link URL?
- BuildOptions.SymlinkLibraries – This Unity Editor IDE setting (more info) is completely unrelated to my technique above. Don’t be confused.
- GIT Submodules – This technique may replace the need for symlinks. I have not tried it yet.
- Feature Request – The community is passionately requesting Unity Technologies to allow ‘soft links’ to be recognized by Unity Editor IDE
What Do YOU Think?
Have a comment? Please post below. This is a hot topic and I’ll respond promptly over the next month.