- Home /
Custom Handle, affect multiple objects
Hey, hope you can help me with this one.
I create a custom handle slider to affect a variable of my script.
This works perfect for every gameobject with my script attached as long as it is a single selection (Just one instance selected).
Now I wanted to add the functionality to edit multiple objects at once.
Currently I do this:
OnSceneGUI()
draw a slider at my transform.position, and calculate the delta movement of the slider as delta
edit variable in my script via ((ScriptXY)target).variable += delta
pretty straight forward
Now for multi-objected editing
I added CanEditMultipleObjects as it says in the docs.
I calculate the new position for the handle as the center of all targets in a callback and assign it in EditorApplication.update
But still one handle is drawn for each instance of myscript in targets in the OnSceneGUI-function. They overlap and only one object is affected by dragging the custom handle. Note that targets must not be used in OnSceneGUI
So how would you solve this problem? Do you know any best practises? Any sample code. Anything working, or just the idea of how to draw only one handle for all objects and affecting them at once would help.
Having a similar problem right now. It appears that one Editor instance gets every time the selection is changed. I believe multi-object editing was designed primarily for editing in the inspector, not within the scene view. Note that the DrawGizmo attribute sits on a static function, so you can't really utilize any meta information you might have in your Editor instance here; e.g. in my case, a set of selected vertices.
The only solution I see is to have a singleton object that all editor instances have a reference to. Its class would have to:
$$anonymous$$now a list of all current targets, and when targets are added or removed.
Receive events from within the OnSceneGUI call and do all the logic.
Store everything the gizmo rendering function calls need to render as wanted.
There are, of course, plenty of caveats to this. I'll report back as I work something out.
Answer by Zyl · Feb 07, 2016 at 06:30 PM
Using the following class I wrote to derive from instead of Editor and then overriding OnMultiEdit() might work for you. It's a bit hacky but works. Basically with [CanEditMultipleObjects] set on your class, a new instance will be made every time the selection changes and it will receive all the OnSceneGUI()-calls for all targets for every event. Any previous state before the selection is changed is lost unless you manage it in static members or otherwise. In below example I build the target list myself to avoid Unity from spamming me with error messages which should really be suppressible warnings instead.
using UnityEngine;
using System.Collections;
using UnityEditor;
public class MultiEditor : Editor {
private System.Collections.Generic.List<Object> targetList;
public MultiEditor() {
targetList = new System.Collections.Generic.List<Object>(1);
}
public virtual void OnMultiEdit(System.Collections.Generic.List<Object> targetList) {
}
public void OnSceneGUI() {
if (targetList.Contains (target)) {
// We got all targets.
if (target == targetList [0]) {
// First call of OnSceneGUI(). Time to relay.
OnMultiEdit (targetList);
}
} else {
// Build the target list first, skipping the first event.
// First event usually is a Layout event which is generally not interesting anyway.
targetList.Add (target);
}
}
}
Your answer
Follow this Question
Related Questions
Custom Handle. Mutliple Objects 0 Answers
Custom Editor loosing data on play 1 Answer
Handles.matrix seems strange? 2 Answers
Editor Handles Vertex Snap not working on script's object. 0 Answers
Draw ellipse via Handles class 0 Answers