Skip to content

Instantly share code, notes, and snippets.

@s2kw
Created June 15, 2016 11:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save s2kw/5e451b76d3f698e0489d73678dd3d5ad to your computer and use it in GitHub Desktop.
Save s2kw/5e451b76d3f698e0489d73678dd3d5ad to your computer and use it in GitHub Desktop.
毎日出題の3日目第二問の答え
public class PrimitiveTypeRotation : MonoBehaviour {
GameObject primitive = null;
PrimitiveType currentPrimitive = PrimitiveType.Sphere;
void Update () {
if (Input.GetMouseButton(0))
{
// マウス位置から発生するRayを作成
var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
// 当たり確認オブジェクト
RaycastHit hit;
if (Physics.Raycast(ray, out hit, float.MaxValue))
{
if (this.primitive == null)
{
this.primitive = GameObject.CreatePrimitive(currentPrimitive);
Destroy(primitive.GetComponent<Collider>());
// 次回生成する primitive type を次の物体にしておく。
this.IncreasePrimitiveType();
}
// hit位置へ移動させることで、Mouseに追従しているかのように見せる
this.primitive.transform.position = hit.point;
}
}
else
if (Input.GetMouseButtonUp(0)) {
if (primitive != null)
{
Destroy(this.primitive);
}
}
}
void IncreasePrimitiveType()
{
bool collect = false;
// 今何番目かを取得
foreach ( PrimitiveType prim in System.Enum.GetValues(typeof(PrimitiveType)) )
{
Debug.Log(prim.ToString());
if ( prim == this.currentPrimitive )
{
collect = true;
// 見つかったら次のループで代入するので以降は何もせず抜ける
continue;
}
// 見つけたフラグが立ってるならば代入して終了
if (collect)
{
this.currentPrimitive = prim;
return;
}
}
// ループが終了しても代入処理がここまで何もされてないということは、primitive type の末尾だったということ。
// なのでPrimitiveTypeの先頭にあるパラメータを代入する。
foreach(PrimitiveType prim in System.Enum.GetValues(typeof(PrimitiveType))){
this.currentPrimitive = prim;
// 最初の1つ目を取ったら解散
return;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment