GL.LINES 线

static var LINES : int

Description描述

Mode for Begin: draw lines.

Begin模式开始后,然后绘制线。

If you want to draw something 2D in screen space then probably you will want to use GL.LoadOrtho or GL.LoadPixelMatrix.

如果在场景中画2D对象,你可能要用到GL.LoadOrtho 或 GL.LoadPixelMatrix函数。

If you want something in "3D", then probably you should consider using GL.LoadIdentity followed by GL.MultMatrix with the actual 3D transform you want your stuff to be in.

如果在场景中画3D对象,你可能要用到GL.LoadIdentity 或 GL.MultMatrix函数进行3D变换。

参见: GL.Begin, GL.End.

using UnityEngine;
using System.Collections;

public class example : MonoBehaviour {
	public Material mat;
	private Vector3 startVertex;
	private Vector3 mousePos;
	void Update() {
	mousePos = Input.mousePosition;
		if (Input.GetKeyDown(KeyCode.Space))
			startVertex = new Vector3(mousePos.x / Screen.width, mousePos.y / Screen.height, 0);

	}
	void OnPostRender() {
		if (!mat) {
			Debug.LogError("Please Assign a material on the inspector");
			return;
		}
		GL.PushMatrix();
		mat.SetPass(0);
		GL.LoadOrtho();
		GL.Begin(GL.LINES);
		GL.Color(Color.red);
		GL.Vertex(startVertex);
		GL.Vertex(new Vector3(mousePos.x / Screen.width, mousePos.y / Screen.height, 0));
		GL.End();
		GL.PopMatrix();
	}
	void Awake() {
		startVertex = new Vector3(0, 0, 0);
	}
}
// Draws a line from "startVertex" var to the curent mouse position.
//从startVertex绘制一条线到当前的鼠标位置
var mat : Material;
private var startVertex : Vector3;
private var mousePos : Vector3;
startVertex = Vector3(0,0,0);

function Update() {
	mousePos = Input.mousePosition;
	// Press space to update startVertex
	//按下空格更新startVertex
	if(Input.GetKeyDown(KeyCode.Space)){
		startVertex = Vector3(mousePos.x/Screen.width, mousePos.y/Screen.height, 0);
	}
}

function OnPostRender() {
	if (!mat) {
		Debug.LogError("Please Assign a material on the inspector");
		return;
	}
	GL.PushMatrix();
	mat.SetPass(0);
	GL.LoadOrtho();
	GL.Begin(GL.LINES);
	GL.Color(Color.red);
	GL.Vertex(startVertex);
	GL.Vertex(Vector3(mousePos.x/Screen.width, mousePos.y/Screen.height, 0));
	GL.End();
	GL.PopMatrix();
}
最后修改:2011年3月21日 Monday 10:31

本脚本参考基于Unity 3.4.1f5

英文部分版权属©Unity公司所有,中文部分© Unity圣典 版权所有,未经许可,严禁转载 。