Search Unity

Is there anyway to edit the files in the "Assembly Browser" ?

Discussion in 'Scripting' started by Sahkan, Jul 29, 2014.

  1. Sahkan

    Sahkan

    Joined:
    Mar 3, 2013
    Posts:
    204
    If let's say i want to edit the Debug class, take the Log function and add there a line that tells it to do an other thing while printing the log.

    Is that possible ?
     
  2. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
  3. Sahkan

    Sahkan

    Joined:
    Mar 3, 2013
    Posts:
    204
    Thanks ! Is it the only class i can do that with ? Or can it be done with all kind of classes ?
     
  4. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    I don't understand what you mean.
     
  5. Sahkan

    Sahkan

    Joined:
    Mar 3, 2013
    Posts:
    204
    I want to inject functionality to, for example : gameObject.SetActive(bool), that this line will do more then just activating the object .

    And btw, I'v tried the log thing and it doesn't do anything, any idea why ? :
    Code (CSharp):
    1. void someLogLikeDelegate(string aaa,string bbb, LogType ccc)
    2.     {
    3.         for(int i=0;i<10;i++)Debug.Log("sdfasdfasdf");
    4.     }
    5.  
    6.     void Start()
    7.     {
    8. Application.RegisterLogCallbackThreaded(someLogLikeDelegate);
    9. Application.RegisterLogCallback(someLogLikeDelegate); // Or this one, nether works
    10.     }
     
    Last edited: Jul 29, 2014
    pjaimin likes this.
  6. LightStriker

    LightStriker

    Joined:
    Aug 3, 2013
    Posts:
    2,717
    No, you cannot... and... yes... you can.

    No, you cannot because there is no direct callback on SetActive (except maybe OnEnable() on a MonoBehaviour).

    Yes, you can by doing library rewriting/injection - which I don't advice you to do. The issue is that;
    1) It's against every conceivable user agreement.
    2) Very easy to break something.
    3) Horribly tricky as you go modify MSIL directly.
    4) It's VERY easy to break something.
    5) Has to be redone anytime you update to a newer version of Unity.

    An example of what was done by library injection in the Editor; http://forum.unity3d.com/threads/jailbreaking-unity-editor.252488/
     
  7. Sahkan

    Sahkan

    Joined:
    Mar 3, 2013
    Posts:
    204
    Thanks !
     
  8. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    The "better" way would be to write your own extension method that wraps Unity's.
     
  9. LightStriker

    LightStriker

    Joined:
    Aug 3, 2013
    Posts:
    2,717
    That doesn't allow you to hook on existing methods.
     
  10. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    Understood. My point was that it would allow you to execute your own stuff alongside Unity' method. Not quite the same pattern but could be used as an alternative.
     
  11. LightStriker

    LightStriker

    Joined:
    Aug 3, 2013
    Posts:
    2,717
    I'm not sure how it is an alternative, as "alongside" means here "with no interaction". Unless I'm mistaken, the point of this thread was "how to hook yourself on existing method". Sometimes there's an event, like you pointed out for the Debug.Log issue, sometimes there isn't.

    When there is no event for an existing method being called, you have two choices left;
    - Runtime hook/injection
    - Library injection

    Both being horribly tricky.
     
  12. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    Sure. One way I took "hook yourself on existing method" was "execute my own code when this other code executes". So - assuming the code you want to execute doesn't change over the lifetime of the application one way you could execute your own code is to wrap Unity's code in another method.

    If you're making runtime changes to what code executes then yes, obviously that method would not work and you'd be forced down one of the paths you mentioned.

    Generally speaking - if something isn't possible (or isn't a good idea) it should be the prerogative of the members of this forum to offer suggestions for other things that could be done. I can't read the OPs mental checklist of requirements so I simply said "maybe something like this would work for you".
     
  13. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    And just to be cheeky - you could do something like this and execute different code in each call

    Code (csharp):
    1.  
    2. public static void CustomSetActive(this GameObject gameObject, bool active, Action<GameObject> action)
    3. {
    4.     gameObject.SetActive(active);
    5.     if (action != null)
    6.         action(gameObject);
    7. }
    8.