I’m trying to get the Handles.DrawBezier to work. I follow the setup on the Unity Script Reference
but i get an error when i apply the BezierScript.js script to a GameObject.
“Instance of DrawBezierHandle couldn’t be created because there is no script with that name.”
Other Handler’s do this too, like the WiredDisc.
Any help would be Appreciated.
2 Answers
2
You can only use these editor classes to implement extensions for the Unity Editor itself. The classes are not available once you build your games.
Read more about extending the editor here (this will give you some advice to what is supported and how to create editor scripts):
http://unity3d.com/support/documentation/Components/gui-ExtendingEditor.html
Edit:
I see your problem. Handles.DrawBezier seem to only use xy plane.
A workaround could be using the Gizmos to draw the bezier curve. I have tried to implement this in a small C# class (Note this script should not be located in the editor folder):
using UnityEngine;
using System.Collections;
public class Bezier : MonoBehaviour {
public Vector3 startPosition = Vector3.zero;
public Vector3 endPosition = Vector3.left;
public Vector3 startTangent = Vector3.up;
public Vector3 endTangent = Vector3.back;
public int numberOfSubdivisions = 10;
void OnDrawGizmos(){
numberOfSubdivisions = Mathf.Max(1,numberOfSubdivisions);
Vector3[] array = new Vector3[numberOfSubdivisions+1];
// B(t) = (1-t)^3 * startPosition + 3 * (1-t)^2 * t * startTangent + 3 * (1-t) * t^2 * endTangent + t^3 * endPosition, t=[0,1]
for (int i=0;i<=numberOfSubdivisions;i++){
float t = i/(float)numberOfSubdivisions;
float omt = 1.0f-t; // One minus t = omt
array _= startPosition*(omt*omt*omt) +_
_ startTangent*(3omtomtt) +_
_ endTangent(3omttt)+_
_ endPosition(ttt);_
_ Gizmos.DrawLine(array[i-1],array*);_
_ }_
_ }_
_ }_
_}*_
I don’t know how to draw a cubic Bezier curve in the editor i can now draw one in-game, which i needed anyway.
var Marker :Transform;
var startPoint :Transform;
var startTangent :Transform;
var endTangent :Transform;
var endPoint :Transform;
var BCPoint :Vector3 = Vector3(0,0,0);
var i :float;
var idivide :float = 0.0;
var count :int = 10.0;
function Start()
{
for( i = 0; i <= count; i++)
{
idivide = i/count;
if(idivide > 1.0)
{
idivide = 1.0;
}
//http://en.wikipedia.org/wiki/B%C3%A9zier_curve
//Cubic Bézier curves
//The explicit form of the curve is:
// B(t) = (1-t)^3 * Point0 + 3 * (1-t)^2 * t * Point1 + 3 * (1-t) * t^2 * Point2 + t^3 * Point3, t=[0,1]
BCPoint.x = Mathf.Pow((1-idivide),3) * startPoint.position.x + 3 * Mathf.Pow((1-idivide),2) * idivide * startTangent.position.x + 3 * (1-idivide) * Mathf.Pow(idivide,2) * endTangent.position.x + Mathf.Pow(idivide,3) * endPoint.position.x;
BCPoint.y = Mathf.Pow((1-idivide),3) * startPoint.position.y + 3 * Mathf.Pow((1-idivide),2) * idivide * startTangent.position.y + 3 * (1-idivide) * Mathf.Pow(idivide,2) * endTangent.position.y + Mathf.Pow(idivide,3) * endPoint.position.y;
BCPoint.z = Mathf.Pow((1-idivide),3) * startPoint.position.z + 3 * Mathf.Pow((1-idivide),2) * idivide * startTangent.position.z + 3 * (1-idivide) * Mathf.Pow(idivide,2) * endTangent.position.z + Mathf.Pow(idivide,3) * endPoint.position.z;
Instantiate (Marker, BCPoint, Quaternion.identity);
yield WaitForSeconds(Time.deltaTime * 2);
}
}
After following Mortennobel's link i was able to get DrawBezier working. Now my problem is that it's only working on the x,y plane.
– anon95799434