2017-04-16 02:12:44 +08:00
|
|
|
|
///Credit Jason Horsburgh
|
|
|
|
|
///Sourced from - https://bitbucket.org/UnityUIExtensions/unity-ui-extensions/issues/127/uilinerenderer-mesh-not-updating-in-editor
|
|
|
|
|
|
2017-05-05 21:25:48 +08:00
|
|
|
|
using UnityEditor;
|
2017-04-16 02:12:44 +08:00
|
|
|
|
|
|
|
|
|
namespace UnityEngine.UI.Extensions
|
|
|
|
|
{
|
|
|
|
|
[CustomEditor(typeof(UILineRenderer))]
|
|
|
|
|
public class BezierLineRendererEditor : Editor
|
|
|
|
|
{
|
|
|
|
|
void OnSceneGUI()
|
|
|
|
|
{
|
|
|
|
|
UILineRenderer curveRenderer = target as UILineRenderer;
|
|
|
|
|
|
2017-05-08 01:39:53 +08:00
|
|
|
|
if (!curveRenderer || curveRenderer.Points == null || curveRenderer.Points.Length < 2)
|
2017-04-16 02:12:44 +08:00
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var oldMatrix = Handles.matrix;
|
|
|
|
|
var transform = curveRenderer.GetComponent<RectTransform>();
|
|
|
|
|
//Pivot must be 0,0 to edit
|
2017-05-08 07:18:06 +08:00
|
|
|
|
//transform.pivot = Vector2.zero;
|
2017-04-16 02:12:44 +08:00
|
|
|
|
Handles.matrix = transform.localToWorldMatrix;
|
|
|
|
|
|
2017-06-22 06:44:10 +08:00
|
|
|
|
var sizeX = curveRenderer.rectTransform.rect.width;
|
|
|
|
|
var sizeY = curveRenderer.rectTransform.rect.height;
|
|
|
|
|
var offsetX = -curveRenderer.rectTransform.pivot.x * sizeX;
|
|
|
|
|
var offsetY = -curveRenderer.rectTransform.pivot.y * sizeY;
|
|
|
|
|
|
|
|
|
|
Vector2[] points = new Vector2[curveRenderer.Points.Length];
|
|
|
|
|
for (int i = 0; i < curveRenderer.Points.Length; i++)
|
|
|
|
|
{
|
|
|
|
|
points[i] = curveRenderer.Points[i];
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-08 07:18:06 +08:00
|
|
|
|
//Need to transform points to worldspace! when set to Relative
|
2017-06-22 06:44:10 +08:00
|
|
|
|
if (curveRenderer.relativeSize)
|
|
|
|
|
{
|
|
|
|
|
for (int i = 0; i < points.Length; i++)
|
|
|
|
|
{
|
|
|
|
|
points[i] = new Vector2(points[i].x * sizeX + offsetX, points[i].y * sizeY + offsetY);
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-04-16 02:12:44 +08:00
|
|
|
|
|
|
|
|
|
for (int i = 0; i < points.Length - 1; i += 2)
|
|
|
|
|
{
|
|
|
|
|
Handles.DrawLine(points[i], points[i + 1]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < points.Length; ++i)
|
|
|
|
|
{
|
|
|
|
|
using (var check = new EditorGUI.ChangeCheckScope())
|
|
|
|
|
{
|
|
|
|
|
var p = Handles.PositionHandle(points[i], Quaternion.identity);
|
|
|
|
|
|
|
|
|
|
if (check.changed)
|
|
|
|
|
{
|
|
|
|
|
Undo.RecordObject(curveRenderer, "Changed Curve Position");
|
2017-06-22 06:44:10 +08:00
|
|
|
|
if (curveRenderer.relativeSize)
|
|
|
|
|
{
|
|
|
|
|
curveRenderer.Points[i] = new Vector2((p.x - offsetX) / sizeX, (p.y - offsetY) / sizeY);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
curveRenderer.Points[i] = p;
|
|
|
|
|
}
|
2017-04-16 02:12:44 +08:00
|
|
|
|
curveRenderer.transform.gameObject.SetActive(false);
|
|
|
|
|
curveRenderer.transform.gameObject.SetActive(true);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Handles.matrix = oldMatrix;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|