Dzianis Yafimau
Dzianis Yafimau

Reputation: 2016

How can I move common code into external assembly to be used in Unity 3d projects?

I have to share some code between multiple projects in Unity. This code is under constant changes during work on projects. So I need my code to be shared as separate assembly and be included in each Unity solution in Visual Studio 2015.

What is the way to make changes in common assembly so that it automatically updates for other projects and for Unity editor?

Upvotes: 0

Views: 876

Answers (2)

Everts
Everts

Reputation: 10721

Your solution is in submodules with version control. You have one repo for the main project. Then you have a folder within that is another repo. This one is a submodule. It appears grey on your main repo and does not go into commit.

https://git-scm.com/book/en/v2/Git-Tools-Submodules

It does work with other version control systems.

The point of that pattern is that you work on project A with utility-submodule. utility is a folder inside Assets folder. Then you modify Utility.cs and push it on Utility repo.

Project B is using utility-submodule and make a pull, your modification are there without altering the rest of Project B. Obviously, this includes all the hassle offered by version control, that is, conflicts if Project B has worked on utility, probable breaks on other projects if you change the implementation of utility and so on (nothin unusual though).

On the other hand, it is an easy way to pass common code over independent projects.

Upvotes: 1

Dzianis Yafimau
Dzianis Yafimau

Reputation: 2016

Let's say I have my shared project here: c:\UnityProjects\DesignPatterns\ and need to include in my Unity project here: c:\UnityProjects\Game\

Solution:

  1. Move all shared code into separate project (c:\UnityProjects\DesignPatterns)
  2. Include this project in solution for all your Unity projects. This allows you to make changes once you need them without reopening shared project separately.
  3. Everything works fine at this moment except Unity editor can't see your external assembly. You have to copy it into any assets folder in Unity. Unity will automatically detect it and create .meta file. Let's create folder for this: c:\UnityProjects\Game\Assets\ExternalDLLs\
  4. We don't want to copy recently built assembly into this directory, we want make it automatically. And Visual Studio post-build event command line is here to help with that. Right click on CSharp project and select properties, then go to Build Events tab and add the following line into post-build event command line:

    xcopy $(ProjectDir)..\DesignPatterns\bin\$(ConfigurationName)\DesignPatterns.dll" "$(ProjectDir)\Assets\ExternalDLLs\DesignPatterns.dll" /Y

Now each time we make solution build this command copies our dll from output folder of shared project into our project's asset folder.

Please note: shared project must be built before your unity code assembly is built. It is the case when you always make solution build. In other cases consider copiing assembly from Unity temp directory (you have macros for this folder to select).

Upvotes: 0

Related Questions