Skip to content

Instantly share code, notes, and snippets.

@wandermyz
Created May 23, 2017 07:11
Show Gist options
  • Save wandermyz/84fd5d903f698375f6ac21abcc6d75d1 to your computer and use it in GitHub Desktop.
Save wandermyz/84fd5d903f698375f6ac21abcc6d75d1 to your computer and use it in GitHub Desktop.
How to draw a curve in a Unity Editor Window
using UnityEngine;
using UnityEditor;
public class CurveDrawingTest : EditorWindow {
[MenuItem("Window/CurveDrawingTest")]
public static void ShowWindow() {
EditorWindow.GetWindow<CurveDrawingTest>().Show();
}
void OnGUI() {
EditorGUILayout.BeginVertical();
var rect = new Rect(0, 0, position.width, position.height);
EditorGUI.DrawRect(rect, Color.black);
float yMin = -1;
float yMax = 1;
float step = 1 / position.width;
Vector3 prevPos = new Vector3(0, curveFunc(0), 0);
for (float t = step; t < 1; t += step) {
Vector3 pos = new Vector3(t, curveFunc(t), 0);
UnityEditor.Handles.DrawLine(
new Vector3(rect.xMin + prevPos.x * rect.width, rect.yMax - ((prevPos.y - yMin) / (yMax - yMin)) * rect.height, 0),
new Vector3(rect.xMin + pos.x * rect.width, rect.yMax - ((pos.y - yMin) / (yMax - yMin)) * rect.height, 0));
prevPos = pos;
}
EditorGUILayout.EndVertical();
}
float curveFunc(float t) {
return Mathf.Sin(t * 2 * Mathf.PI);
}
}
@wandermyz
Copy link
Author

screen shot 2017-05-23 at 12 12 01 am

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment