- Home /
onMaterialChange or SceneView.onDropMaterial
Hi, I have a game object with many generated children. The parent does not have any visual or physical presence in the scene, only some code to maintain the children.
Our graphics artists are tired of having to assign a material on a field in the inspector, and would much rather be able to just use the regular drag and drop solution for setting materials. My problem is that I don't know how to detect this, without cycling through all children every frame, to detect changes.
Is there any onMaterialChange or SceneView.onDropMaterial? or just any drop event, so I can limit the check function to only run on those occasions.
/Jannek
@VildNinja Could you please clarify the 2nd paragraph of your question. I mean, materials can be assigned in a field in the Inspector by drag-n-drop anyway, so it's not apparent (to me anyway) what exactly the problem is.
It does look like you want to detect a change of material, and I believe that you could setup a custom event to do that, but a lot of context is missing in your question.
So, please provide as much details as you can.
Sorry. The artists wants drag and drop in scene view. Dragging one material to one child should update the materials for all children. Currently the parent maintains a ref to the material, and has functionality to update all children. So I wanted any material drop events on the children to call that function on the parent.
I found a solution, so will post an answer now.
I see, so if I understand well, you want to have the flexibility to update the material, by drag-n-drop, either on the parent, or any of the children.
I don't know about you answer, but here's a suggestion: You could add an OnDrop$$anonymous$$aterial event to the child script, which I assume all children would have it assigned on them. Then, when any child gets a new material, it could raise the event. Since the parent has a reference to all the children, it could subscribe to the OnDrop$$anonymous$$aterial event of each child instance, and handle it appropriately.
The child script should be marked with the [ExecuteInEdit$$anonymous$$ode] attribute, and then in Update() check when the current material changes, in order to raise the OnDrop$$anonymous$$aterial event if it has.
Answer by VildNinja · Nov 07, 2017 at 03:22 PM
Hooking into the SceneView.onSceneGUIDelegate allows me to check for EventType.DragPerform and EventType.DragUpdated. Combining those with var mat = DragAndDrop.objectReferences[0] as Material; plus shooting a ray Physics.Raycast(HandleUtility.GUIPointToWorldRay(Event.current.mousePosition), out hit) allows me to steal the event and calling my custom function instead.
Your answer