I’m implementing a custom search that allows the user to find everywhere that a string or number or named object is mentioned in a scene. However, this falls foul of ‘dangerous’ property queries such as renderer.material which leaks materials into the scene.
Is there any way of checking whether a property is editor-safe, or should I go with my current plan of creating a manual exclusion list based on class and property name?
Cheers!
No, there’s no way around a manual exclusion list ^^. That’s why the general advice is that property getter should not have side effects which in case of the material property is not true.
Keep in mind beside just plain side effect issues you also have to be careful with cyclic references as you traverse the object tree. So a “visited” list / dictionary is always required when you want to traverse the whole tree.
1 Like
Cheers. Is there by any chance such a list kicking about that I could refer to? Right now, I’ve had to go for an inclusion list of properties because the consequences of missing one with side-effects are too catastrophic to risk.
I don’t think there is one or at least not a reliable one. The only properties I can think of at the moment is the material property as well as the mesh property of the MeshFilter. Currently I can’t think of anything else. Though, possible that there are others.
The only ones I can remember that have such a disastrous side effect are the material and mesh properties. Those are walking talking memory leak generators.
While pretty much all Unity properties which return an array allocate a copy of the array every time they are accessed, at least those are managed objects which will eventually be collected by the garbage collector, while a Material or Mesh is a native object that will stick around potentially forever if you always load scenes additively and never call Resources.UnloadUnusedAssets().
Yeah; it’s worse than that in edit mode though. “Hey, because you just searched for the word ‘sausage’ in your scene, I’ve now instanced and detatched every mention of every material on every object so that they get rendered as separate draw-calls and no longer update if you change the base material! Good luck manually fixing all that!”