- Home /
Is there an event/hook/trigger to sub to when a texture is applied to a gameObject in scene in editor mode?
Basically the title is pretty much self-describing, I want to make changes to the gameObject that the texture is applied to :
I want to change some values within a monobehaviour script, which is attached to this gameObject - for example setting an enum or something, based on the dark/light ratios of an image.
Does Unity currently supports such a thing, or is there a workaround for that?
Currently the only way I figured out is that, I can follow the material/texture changes if the gameObject is currently highlighted (so it is visible in the Inpsector window / selected in hierarchy). But if I deselected, click on elsewhere and then apply the new texture I can no longer follow these changes. The only way to do this if I re-select the object and then compare the texture with the one it had before. But that also goes down to hell, and a complicated lifecycle, because:
Let's say I have an enum currently saying that it is half-half, half of the pixels are below and half of them above the X threshold. If I apply the texture with my gameObject unselected, this enum won't be readjusted, so it'll show a bad solution run-time, unless I select the gameObject after the changes.
Or there's another problem with this which is : building. If I build the game without selecting every one of these objects, the enum will show a false value, so I'll have to make a hook in the build system as well, and re-check every item. Which is a pain in the.
So do we have a very good solution for this problem?
Answer by ptc-gkanai · Oct 14, 2019 at 12:00 PM
I'm kinda answering my question. There's a less complicated way to do it. So basically what I've found is that you can use [ExecuteAlways] tag on a MonoBehaviour, which will triggers some part of the script like update or onGUI or others https://docs.unity3d.com/ScriptReference/ExecuteAlways.html. Using the !Application.IsPlaying you can actually narrow down the triggers to those that are only happening in the editor, and from that point you can use member variables and other things in Update to follow if your gameObject's material has changed or not.