Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Custom package vs git submodule

Discussion in 'Package Manager' started by Felix_K, Jan 17, 2022.

  1. Felix_K

    Felix_K

    Joined:
    May 30, 2017
    Posts:
    4
    We work on multiple different projects which all share basic (game) structure (actually, we're not a game company but a neuroscience research lab - so the projects are experiments, but that should not matter).
    Therefore, we try to abstract out the common core (experimental structure, in our case), so that we can maximally recycle and reuse across projects. Naturally, with each project this core develops further and gets better. Some of the improvements shall be updated in old (i.e., running or under-development) projects - others not. Since multiple projects are under active development at the same time, the direction of update is impossible to predict. Their versions of the core go back to the same root but, so they share a lot of code, but each of them also has their particularities. If I now change something in Project A which is in the shared code base with Projects B and C, I want to directly (and selectively) reflect this change in B and C as well. Later it could be that I update B and want to reflect that in A.

    Normally git is the tool of choice here, to my understanding. We would have (for example) a branch per project and keep these as parallel as needed/wanted. I can cherry-pick specific files or commits and leave other stuff untouched. However, since the "common core" between my projects is only a (small) part of each project (while the rest is vastly different between projects), the fact that my entire projects are tracked by git does not help much. (I could, in theory, cherry-pick also here - but that's a nightmare and my colleagues will butcher me for suggesting the required atomic commits on such a level.)
    So I was looking into using the git submodules for the common core and found actually quite nice descriptions of people who've done that:
    https://prime31.github.io/A-Method-for-Working-with-Shared-Code-with-Unity-and-Git/
    and
    https://nagachiang.github.io/share-source-code-between-projects-with-git-submodule-in-unity/#

    However, the 2nd guy writes in an Update:
    | "2020-01-08 Update: It’s recommended to create custom packages instead of this."​
    I looked at his exhaustive tutorial on how to make a custom package (that you can also grab from git). But with this approach, I don't see how you could then once you change something in the code of the package, push these changes back to git(hub) so that other project who also pulled from there could get the update. Am I fundamentally overlooking something?

    This seems such a common problem/setting to me. So I wonder what is the preferred approach by the community these days? Are there solutions that I haven't encountered yet?
    Is anyone of you using a similar workflow and has tips / experiences? I'd be very thankful!
     
  2. Meatloaf4

    Meatloaf4

    Joined:
    Jul 30, 2013
    Posts:
    182
    So to get what you are after you would do the following.

    Essentially the way I do this is through a combination of submodules and Unity packages. Here is what you need below
    * Code sharing Unity package under git version control you want to share. (Example here)
    * Game / project under git version control.

    You then need to add the code sharing unity package as a git submodule to your game version control. Typically I add this to a root folder named Submodule. From there you would add the code-sharing unity package to the unity package manager as a local file package. Seem images attached.

    This would approach allows the following
    * Sharing code between multiple unity projects utilizing a custom shared Unity package.
    * Modification of the custom shared Unity package within whatever game/project is including it using the method above.
    * Pushing and pulling given modifications between projects.

    Sorry if I'm not being as detailed as I could be here, I'm running a bit short on time. Hopefully, though the overall approach comes through.
     

    Attached Files:

    saukki, daneobyrd and us3r72_unity like this.
  3. daneobyrd

    daneobyrd

    Joined:
    Mar 29, 2018
    Posts:
    101
    I wonder if you could leverage the UPM samples feature to only import the shared code/assets you need for any project. An example use-case is only importing a few VFX textures (instead of the entire folder) or shader include files/ShaderGraph subgraphs from your shared package.
     
    Meatloaf4 likes this.
  4. JosephBG

    JosephBG

    Joined:
    Aug 8, 2017
    Posts:
    3
    georges-m and daneobyrd like this.
  5. daneobyrd

    daneobyrd

    Joined:
    Mar 29, 2018
    Posts:
    101