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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

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

Discussion in 'Entity Component System' started by Mr-Mechanical, Dec 25, 2019.

  1. Mr-Mechanical

    Mr-Mechanical

    Joined:
    May 31, 2015
    Posts:
    507
    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!
     
    tlskillman and tgrotte like this.
  2. Kichang-Kim

    Kichang-Kim

    Joined:
    Oct 19, 2010
    Posts:
    979
    Copy package folder to your project's Packages folder.
     
    Mr-Mechanical likes this.
  3. Mr-Mechanical

    Mr-Mechanical

    Joined:
    May 31, 2015
    Posts:
    507
    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.
     
    romya0010 likes this.
  4. Kichang-Kim

    Kichang-Kim

    Joined:
    Oct 19, 2010
    Posts:
    979
    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)
     
    romya0010 and Mr-Mechanical like this.
  5. starikcetin

    starikcetin

    Joined:
    Dec 7, 2017
    Posts:
    335
    Mr-Mechanical likes this.
  6. Mr-Mechanical

    Mr-Mechanical

    Joined:
    May 31, 2015
    Posts:
    507
  7. Mr-Mechanical

    Mr-Mechanical

    Joined:
    May 31, 2015
    Posts:
    507
    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.
     
  8. justaddice83

    justaddice83

    Joined:
    Sep 19, 2012
    Posts:
    44
    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.
     
    Eristen likes this.
  9. R2-RT

    R2-RT

    Joined:
    May 8, 2019
    Posts:
    38
    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 <Project>/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.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using Unity.Collections.LowLevel.Unsafe;
    4. using UnityEngine;
    5.  
    6. namespace Unity.Collections
    7. {
    8.     public static class NativeListExtensions
    9.     {
    10.         public static unsafe UnsafeList* StealUnsafeList(this ref NativeList<int> list)
    11.         {
    12.             return list.m_ListData;
    13.         }
    14.     }
    15. }
     

    Attached Files:

  10. romya0010

    romya0010

    Joined:
    Dec 5, 2019
    Posts:
    2

    Worked for me... Thanks!
     
  11. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,594
    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.
     
    MNNoxMortem and Anthiese like this.
  12. Kironde-Namusanga

    Kironde-Namusanga

    Joined:
    Dec 11, 2014
    Posts:
    12