I searched for hours on google and Unity Documentation & forums for information or tutorials on how to do this, but I can't find anything.
I'm trying to write an editor script who'll allow me to edit a zone in the scene view.
I found a way to do it and it works, BUT I really find this solution not aesthetic at all, here's how it looked :
And that's my code to achieve that :
[CustomEditor(typeof(CameraZone))]
public class CameraZoneEditor : Editor
{
void OnSceneGUI()
{
var zone = (CameraZone)target;
Handles.DrawSolidRectangleWithOutline(new Rect(zone.transform.position, zone.size), zone.editorColor, Color.black);
Handles.color = Color.white;
var center = zone.transform.position + ((Vector3)zone.size / 2);
var handlesPos = new [] {
new Vector3(center.x + zone.size.x / 2, center.y, center.z),
new Vector3(center.x - zone.size.x / 2, center.y, center.z),
new Vector3(center.x, center.y + zone.size.y / 2, center.z),
new Vector3(center.x, center.y - zone.size.y / 2, center.z)
};
var posRight = Handles.Slider(handlesPos[0], Vector2.right);
var posLeft = Handles.Slider(handlesPos[1], Vector2.left);
var posUp = Handles.Slider(handlesPos[2], Vector2.up);
var posDown = Handles.Slider(handlesPos[3], Vector2.down);
var rect = new Rect(posLeft.x, posDown.y, posRight.x, posUp.y);
zone.size = rect.size;
}
}
This is working just how I want it to (except that the bottom and left arrows are not resizing the zone but that's not a problem for now).
Now to make it more beautiful, I wanted to replace the arrows with simple cubes, I've seen that I can do that with Handles.CubeHandleCap
delegates so I made some to test it. It looks exactly how I want them to but I'm unable to resize the zone now, when I try to drag it it just unselect the GameObject.
Here's how it looks now :
And here's the code I wrote to do that :
[CustomEditor(typeof(CameraZone))]
public class CameraZoneEditor : Editor
{
void OnSceneGUI()
{
var zone = (CameraZone)target;
Handles.DrawSolidRectangleWithOutline(new Rect(zone.transform.position, zone.size), zone.editorColor, Color.black);
Handles.color = Color.white;
var center = zone.transform.position + ((Vector3)zone.size / 2);
var handlesPos = new [] {
new Vector3(center.x + zone.size.x / 2, center.y, center.z),
new Vector3(center.x - zone.size.x / 2, center.y, center.z),
new Vector3(center.x, center.y + zone.size.y / 2, center.z),
new Vector3(center.x, center.y - zone.size.y / 2, center.z)
};
Handles.CapFunction rightFunc = (id, position, rotation, size, type) => Handles.CubeHandleCap(0, handlesPos[0], Quaternion.identity, 0.2f, EventType.Repaint);
Handles.CapFunction leftFunc = (id, position, rotation, size, type) => Handles.CubeHandleCap(0, handlesPos[1], Quaternion.identity, 0.2f, EventType.Repaint);
Handles.CapFunction upFunc = (id, position, rotation, size, type) => Handles.CubeHandleCap(0, handlesPos[2], Quaternion.identity, 0.2f, EventType.Repaint);
Handles.CapFunction downFunc = (id, position, rotation, size, type) => Handles.CubeHandleCap(0, handlesPos[3], Quaternion.identity, 0.2f, EventType.Repaint);
var posRight = Handles.Slider(handlesPos[0], Vector2.right, 0.2f, rightFunc, 0.1f);
var posLeft = Handles.Slider(handlesPos[1], Vector2.left, 0.2f, leftFunc, 0.1f);
var posUp = Handles.Slider(handlesPos[2], Vector2.up, 0.2f, upFunc, 0.1f);
var posDown = Handles.Slider(handlesPos[3], Vector2.down, 0.2f, downFunc, 0.1f);
var rect = new Rect(posLeft.x, posDown.y, posRight.x, posUp.y);
zone.size = rect.size;
}
}