Created
May 23, 2017 07:11
-
-
Save wandermyz/84fd5d903f698375f6ac21abcc6d75d1 to your computer and use it in GitHub Desktop.
How to draw a curve in a Unity Editor Window
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
Author
wandermyz
commented
May 23, 2017
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment