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 ?
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 ?
No but you can use this: http://docs.unity3d.com/ScriptReference/Application.RegisterLogCallback.html
Thanks ! Is it the only class i can do that with ? Or can it be done with all kind of classes ?
I don’t understand what you mean.
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 ? :
void someLogLikeDelegate(string aaa,string bbb, LogType ccc)
{
for(int i=0;i<10;i++)Debug.Log("sdfasdfasdf");
}
void Start()
{
Application.RegisterLogCallbackThreaded(someLogLikeDelegate);
Application.RegisterLogCallback(someLogLikeDelegate); // Or this one, nether works
}
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;
An example of what was done by library injection in the Editor; Jailbreaking Unity Editor - Unity Engine - Unity Discussions
Thanks !
The “better” way would be to write your own extension method that wraps Unity’s.
That doesn’t allow you to hook on existing methods.
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.
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;
Both being horribly tricky.
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”.
And just to be cheeky - you could do something like this and execute different code in each call
public static void CustomSetActive(this GameObject gameObject, bool active, Action<GameObject> action)
{
gameObject.SetActive(active);
if (action != null)
action(gameObject);
}