How to modify Unity packages using custom code and files? *[and also export custom package?]*

Hello!

I am modifying the Unity Physics package; I am adding some custom functions in ConvexConvexDistanceQueries as well as referencing some code in from outside the package in the Assets folder. However, I get errors referencing code outside the package and I can’t drag stuff in the package folder (though I can modify existing scripts). How do I solve this? What is the best strategy to modify a package?

Thank you guys. Happy 2020!

3 Likes

Copy package folder to your project’s Packages folder.

1 Like

Thank you for your response! How do you copy it (from what source)? Can this be done in the finder? Can I just drag and drop into Unity? Thanks a lot for your help.

1 Like

You can find original readonly package from YourProject/Library/PackageCache. Simply copy it to YourProject/Packages. Then re-open unity. After that, you can modify pacakge’s source code. (don’t forget change file write permission)

3 Likes

There is this: GitHub - liortal53/upm-embed: Unity editor extension for easily embedding UPM packages in your project
It embeds the package in your project, so you can change it however you please.

3 Likes

Thanks!

This works great! I am trying to figure out how to export my modified custom package to be able to be used in other Unity projects.

Thanks a lot.

However, this is probably a bad idea, as the idea of packages is that they can be updated independently of your project.

A better or more SOLID way might be to inherit the particular class you want to modify, and then override or wrap the methods you are interacting with.

2 Likes

There is also a bit hacky way to extend any package (even Unity-provided) with access to its internals without modifying the package itself.

  1. Create your own package as usual (e.g. create new directory in /Packages folder with package.json file)
  2. Add .asmref file that references existing package (e.g. Unity.Collections)
  3. Extend any class package using internal access:

In my example, access NativeList<int>.m_ListData even though it is marked with internal keyword.

using System.Collections;
using System.Collections.Generic;
using Unity.Collections.LowLevel.Unsafe;
using UnityEngine;

namespace Unity.Collections
{
    public static class NativeListExtensions
    {
        public static unsafe UnsafeList* StealUnsafeList(this ref NativeList<int> list)
        {
            return list.m_ListData;
        }
    }
}

6560554–743233–com.hacks.collections.zip (2.18 KB)

12 Likes

Worked for me… Thanks!

We do appreciate when some post are helpful to others. But please do not necro three years old threads in future just like that, not adding anything meaningful to the conversation. You can use like button, to say thank you to the poster instead.

2 Likes

How do i use it?

I found the answer in this thread, but unfortunately, the package doesn’t seem to work in unity 2023.