Search Unity

Is there a way to automatically extract an existing Assets folder structure into a UPM package?

Discussion in 'Package Manager' started by RecursiveFrog, Oct 27, 2019.

  1. RecursiveFrog

    RecursiveFrog

    Joined:
    Mar 7, 2011
    Posts:
    350
    Let's presume that you have a project in which you want to extract a folder structure that adheres to the UPM conventions, and has Assembly Definitions. Let's presume it also contains prefabs that you already have in your scenes. Is there a way to automagically convert this folder structure into a UPM package?

    Currently, when I try to extract such a folder manually and re-add it using the package manifest.json, the meta files don't match up correctly.
     
  2. IsopodInAnEscapePod_unity

    IsopodInAnEscapePod_unity

    Unity Technologies

    Joined:
    Sep 20, 2019
    Posts:
    18
    Hi,

    Could you provide which version of Unity you are working in?

    It sounds as though there is no editor functionality to automatically extract an Assets folder into a UPM package, but manually extracting what you want into a package should work- with some exceptions. Namely that you shouldn't add scenes to packages.

    It sounds like that wouldn't have caused problems with your .meta files though. Could you elaborate a bit more on how the .meta files 'don't match up correctly'? Did they get changed, or were some of them deleted?
     
  3. RecursiveFrog

    RecursiveFrog

    Joined:
    Mar 7, 2011
    Posts:
    350
    2018.4.11f1

    The main problem is when your package contains MonoBehaviour or ScriptableObject code, and you have already assigned these scripts to objects either in a scene or to a Prefab, or have an existing ScriptableObject.

    1. Create an empty Unity project
    2. In the Assets folder, create a folder called UPMPackage.
    3. In the UPMPackage folder, create a Runtime folder
    4. In the Runtime folder, create a MonoBehaviour subclass named MyCode with any namespace you like.
    5. In the Runtime folder, create your asmdef
    6. In UPMPackage folder, create your package.json.
    7. Add MyCode to a GameObject in your scene.

    Observe: you have a game object with the desired script attached.

    Next:

    1. Export UPMPackage as a Unity package file
    2. Create another new Unity project
    3. Import your Unitypackage’s contents Into the new project. Verify that the code complies and is intact.

    Next

    1. In your original project, add the location of your package folder to manifest.json

    Observe : your new package has conflicting guids and conflicting code definitions Unity makes new guids for the imported package.

    Next

    1. Delete the original UPMPackage folder from the first project
    2. Click the GameObject to which you’d attached the original MyCode script

    Observe : Missing MonoScript. This is because the GameObject is referencing the guid from the deleted version of the package.



    This problem also happens if you delete the original package first and then later import the UPM package. Even though it doesn’t seem to need to generate new guids for the UPM package, there is still a mismatch.


    This makes it impossible to externalize code that we would like to reuse, because we break all the gameobjects, prefabs, and ScriptableObjects that we’re created and depend on those original files with those original guids.
     
  4. RecursiveFrog

    RecursiveFrog

    Joined:
    Mar 7, 2011
    Posts:
    350
    Note : I did manage to resolve that sample situation by re-re-importing the unitypackage file after adding the UPM package to the manifest. However, I’m uncertain if this is a viable solution for a larger project or a more complicated library.

    This is why I feel like we need a tool that allows us to externalize an existing folder structure and ensures consistency of the guids inside the meta files, so that we don’t lose script references in the process.
     
  5. M_R

    M_R

    Joined:
    Apr 15, 2015
    Posts:
    559
    don't do this.
    instead:

    1. setup your first project as before (code, asmdef, prefab etc...)
    2. create an empty folder on disk, add a package.json and reference that in the manifest (Unity will see an empty package)
      • you can also create the folder inside Packages/ and you don't need to edit the manifest
    3. from inside Unity, move the contents from Assets/ to Packages/your.package/
    4. done
     
    RecursiveFrog likes this.
  6. RecursiveFrog

    RecursiveFrog

    Joined:
    Mar 7, 2011
    Posts:
    350
    I’ll try that out later, as it does solve the immediate problem.

    However, what that doesn’t do is give you a test project to ensure your package is complete and that you haven’t missed any hidden dependencies (eg: an asset store tool has littered the assets folder and you didn’t track down quite everything).

    But your approach does sound promising, and I think I’ll try it out with some asset store packages.
     
  7. M_R

    M_R

    Joined:
    Apr 15, 2015
    Posts:
    559
    after my steps, you can create a new empty project and add the package to the manifest.

    the key part is that you never move asset files from outside unity (i.e. a single project), otherwise it can mess with the import

    e.g. in your previous step, when you added the package from the new project to inside the main project, all of the assets are present twice in the project, with the same GUIDs. this will cause Unity to assign new GUIDs to those assets, breaking all the references.

    if you need to do it (e.g. when updating an AS version of a package to it's newer, existing UPM version) you delete the assets first, then you add the package to the manifest. this way you don't have duplicate GUIDs at any given time, and Unity will not mess with them.
    just remember to NOT save the project between those 2 steps, as Unity (or some custom inspector/editor code) can set missing references to null. restart Unity to be safer
     
    RecursiveFrog likes this.
  8. RecursiveFrog

    RecursiveFrog

    Joined:
    Mar 7, 2011
    Posts:
    350
    Just wanted to report back : This works perfectly. Still wish Unity could create a tool to ease this process, but it's a start.
     
  9. manu73

    manu73

    Unity Technologies

    Joined:
    Aug 22, 2014
    Posts:
    68
    Hi @RecursiveFrog

    We are working on a new package "Package Developer Tools" that will ease this development process.

    Regards,
    EmmanuelH
     
    RecursiveFrog likes this.