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.

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);
		}
	}
}