Skip to content

Instantly share code, notes, and snippets.

@makochang
Created May 12, 2015 03:27
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save makochang/0a955736640ae198c7c0 to your computer and use it in GitHub Desktop.
Save makochang/0a955736640ae198c7c0 to your computer and use it in GitHub Desktop.
Unity マウスでドラッグした場所に線を描画(デバッグ)
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class DrawDraggingLine : MonoBehaviour
{
//ドラッグ中に真.
private bool isDragging = false;
//線の座標リスト.
private List<Vector3> linePoints = new List<Vector3> ();
//新しい座標を追加する時のしきい値.
private float threshold = 0.1f;
// Update is called once per frame
void Update ()
{
//左マウスボタンが押されたとき.
if (Input.GetMouseButtonDown (0)) {
//ドラッグ中を真.
isDragging = true;
}
//左マウスボタンが離されたとき.
if (Input.GetMouseButtonUp (0)) {
//ドラッグ中を偽.
isDragging = false;
//座標リストから全ての要素を削除する.
linePoints.Clear ();
}
//ドラッグ中に実行.
if (isDragging) {
//原点からカメラまでの位置をオフセット座標とする.
Vector3 offset = new Vector3 (0.0f, 0.0f, Vector3.Distance (Camera.main.transform.position, Vector3.zero));
//マウス座標をワールド座標に変換する.
Vector3 mousePoint = Camera.main.ScreenToWorldPoint (Input.mousePosition + offset);
//座標リストが空の場合はマウス座標を追加し、空で無い場合はしきい値に応じてマウス座標を追加する.
if (linePoints.Count == 0) {
linePoints.Add (mousePoint);
} else {
if (Vector3.Distance (linePoints [linePoints.Count - 1], mousePoint) > threshold) {
linePoints.Add (mousePoint);
}
//リストに2点以上座標が存在するとき、線を描画する.
if (linePoints.Count >= 2) {
for (int i=1; i<linePoints.Count; i++) {
Debug.DrawLine (linePoints [i - 1], linePoints [i], Color.red);
}
}
}
}
}
}
@makochang
Copy link
Author

これはどのオブジェクトにスクリプトを入れればよろしいですか??

@saka-logu お好みのゲームオブジェクトにアタッチしてください。

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