Home > Back-end >  How to add a CS project into a VS solution containing a Unity project?
How to add a CS project into a VS solution containing a Unity project?

Time:09-27

I am discovering Unity (still totally noob at Unity platform / editor). I wonder what are the best practises to organize my projects in order to make them available for different Unity projects.

I have C# project MyIaProject that does not know anything about Unity. It just contains some NON-Unity IA code. I have created a Unity 2D project MyUnityProject. I want MyUnityProject to use MyIaProject classes.

It looks that you cannot add a project reference to a unity project as you would do for a non Unity project! The UI just does not allow it.

I just do not want to move all my MyIaProject classes into the MyUnityProject project as I will want to use the same classes in different Unity projects.

Also, I would like to add into the MyIaProject project the needed reference so I can use a vector3d class (= unity 3d position class). Unity does not seem to be a framework reference that you can add from the "add reference" interface. I did not see any Unity related package in NuGet either.

How am I supposed to solve this riddle?

CodePudding user response:

You should look into the Unity Package Manager. It allows you to reference a package containing code and assets from another location on your disk (and even better for sharing code, from a Git repository). For a piece of code to be usable in the package manager, it would need a package.json file describing it. More info here.

CodePudding user response:

You should use git for both the MyIaProject and your Unity project, then you can define MyIaProject as a git submodule in the Unity project Assets folder.

It's all git related (no Unity specifics).

cd MyUnitProject/Assets
git submodule add https://mygitrepo/MyIaProject.git MyIaProject
git submodule update --init

Then MyIaProject will be located in any MyUnitProject/Assets/MyIAProject. Any change is this folder will be commited and pushed to the MyIAProject only if you use the git commands in the MyUnitProject/Assets/MyIAProject folder (or a git submodule compatible UI tool like SourceTree).

PS: in the unity project root folder you should add a .gitignore like this one https://github.com/github/gitignore/blob/main/Unity.gitignore

  • Related