鼠标画画,物体轨迹

using System.Collections;

using System.Collections.Generic;

using UnityEngine;


public class draw001 : MonoBehaviour {


public Material material;

private List lineInfo;


public Transform CUBE;

private Vector3 _vec3TargetScreenSpace;

Vector3 coords;


// Use this for initialization

void Start () {

//初始化鼠标线段链表

lineInfo=new List();

}


// Update is called once per frame

void Update () {

//coords = CUBE.position;

if (CUBE.position!=coords) {

//每次鼠标改变的位置存储进链表

//lineInfo.Add (Input.mousePosition);

_vec3TargetScreenSpace = Camera.main.WorldToScreenPoint(CUBE.position);  

lineInfo.Add (_vec3TargetScreenSpace);

}

coords = CUBE.position;

if (Input.GetMouseButtonUp (1)) {

//每次鼠标改变的位置存储进链表

lineInfo.Clear ();

}

}


void OnGUI(){

GUILayout.Label ("当前鼠标x轴的位置:" + _vec3TargetScreenSpace.x);

GUILayout.Label ("当前鼠标y轴的位置:" + _vec3TargetScreenSpace.y);

}


//此绘制方法由系统调用

void OnPostRender(){

if (!material) {

Debug.LogError ("请给材质资源赋值");

return;

}

//设置该材质的通道,0位默认值

material.SetPass(0);

//设置绘制2D图像

GL.LoadOrtho ();

//表示开始绘制,绘制类型为线段

GL.Begin (GL.LINES);

//得到鼠标点信息的总数量

int size = lineInfo.Count;

//遍历鼠标点的链表

for (int i = 0; i < size - 1; i++) {

Vector3 start = lineInfo [i];

Vector3 end = lineInfo [i + 1];

//绘制线段

DrawLine (start.x, start.y, end.x, end.y);

}

//结束绘制

GL.End ();

}


void DrawLine(float x1,float y1,float x2,float y2){

  //绘制线段,某个点的像素坐标除以屏幕宽或者高

GL.Vertex(new Vector3(x1/Screen.width,y1/Screen.height,0));

GL.Vertex(new Vector3(x2/Screen.width,y2/Screen.height,0));

}

}

你可能感兴趣的:(鼠标画画,物体轨迹)