Last active
June 14, 2016 08:44
-
-
Save s2kw/26d56b51202c359db459ad0e50b72fe5 to your computer and use it in GitHub Desktop.
毎日出題の2日目第四問 description:http://wp.me/p5rxnz-hQ sample:https://vimeo.com/170588450
This file contains hidden or 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
public class LineMaker : MonoBehaviour { | |
// ラインは使い回すのでメンバとして宣言。 | |
LineRenderer line; | |
void Update() | |
{ | |
// GetMouseButtonDownは開始時。 | |
// GetMouseButtonはドラッグ時。 | |
// GetMouseButtonUpは終了時。 | |
if (Input.GetMouseButtonDown(0)) | |
{ | |
bool isHit; | |
var newPos = this.GetClickPosition(out isHit, Input.mousePosition ); | |
// 当たらなかったので終了 | |
if (!isHit) return; | |
// ラインがカラなら作る。 | |
if (this.line == null) | |
{ | |
// 線を引くLineRenderer搭載のゲームオブジェクトお作る。 | |
// 名前あとりあえず "line" にする。 | |
var lg = new GameObject("line"); | |
// ラインレンダラーをくっつけつつ、メンバとして格納しておく。使い回すので。 | |
this.line = lg.AddComponent<LineRenderer>(); | |
// レンダラーにはマテリアルあ必要。これを入れないとピンク表示(表示バグであることを明示するためにUnityが色を付けてくれる)になる。 | |
var material = new Material(Shader.Find("Standard")); | |
// レンダラーにマテリアルを代入する。 | |
this.line.material = material; | |
// vertexは始点と終点のみなので2つ。 | |
this.line.SetVertexCount(2); | |
} | |
// 色をランダムで | |
var color = new Color( | |
UnityEngine.Random.Range(0f, 1f), | |
UnityEngine.Random.Range(0f, 1f), | |
UnityEngine.Random.Range(0f, 1f) | |
); | |
// 色はrenderer.material.colorに代入することで変更できる。ただしシェーダーによる。 | |
// 今回はshaderがstandardなのでこの手法を取る。 | |
this.line.material.color = color; | |
this.line.SetPosition(0, newPos); | |
this.line.SetPosition(1, newPos); | |
} | |
else if (Input.GetMouseButtonUp(0)) | |
{ | |
// ラインがカラということはマウスクリックが開始されていないので終了。 | |
if (this.line == null) return; | |
bool isHit; | |
var newPos = this.GetClickPosition(out isHit, Input.mousePosition); | |
// 当たらなかったので終了 | |
if (!isHit) return; | |
this.line.SetPosition(1, newPos); | |
} | |
else if (Input.GetMouseButton(0)) | |
{ | |
// ラインがカラということはマウスクリックが開始されていないので終了。 | |
if (this.line == null) return; | |
bool isHit = false; | |
var newPos = this.GetClickPosition(out isHit, Input.mousePosition); | |
if (!isHit) return; | |
this.line.SetPosition(1, newPos); | |
} | |
} | |
// 難度も同じことを書く場合あ関数化する | |
// 当たったかどうか、当たった場合はどこか、を知ることができる関数として定義。 | |
Vector3 GetClickPosition( out bool isHit, Vector3 screenMousePos ) | |
{ | |
// スクリーン座標からワールド座標空間で飛ばす 線 を生成 | |
var ray = Camera.main.ScreenPointToRay(screenMousePos); | |
// 当たり判定の情報を以って帰ってくれる変数を宣言。宣言さえしておけばスコープ内で参照可能 | |
RaycastHit hit; | |
// Physics.Raycastで当たり判定お行う。 | |
// out が付くと、関数終了後に変数の中身が変わる可能性ああることを理解すべし | |
if (Physics.Raycast(ray, out hit, float.MaxValue)) | |
{ | |
// この辺はこれの問題と同様に取得する | |
var p = hit.point; | |
p.z = Camera.main.transform.position.z + 10f; | |
isHit = true; | |
return p; | |
} | |
isHit = false; | |
return Vector3.zero; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment