Created
June 15, 2016 11:38
-
-
Save s2kw/34c516bf155fa7f650c049248aedc3e5 to your computer and use it in GitHub Desktop.
毎日出題の3日目第三問の答え
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 CreatePrimitiveByMouse2 : MonoBehaviour { | |
GameObject primitive = null; | |
int currentPrimitiveIndex = 0; | |
List<PrimitiveType> primitiveTypeList = new List<PrimitiveType>(); | |
void Start() | |
{ | |
// 必要なPrimitiveTypeを用意 | |
this.primitiveTypeList = new List<PrimitiveType>() | |
{ | |
PrimitiveType.Sphere, | |
PrimitiveType.Capsule, | |
PrimitiveType.Cylinder, | |
PrimitiveType.Cube | |
}; | |
} | |
void Update () { | |
if (Input.GetMouseButton(0)) | |
{ | |
var ray = Camera.main.ScreenPointToRay(Input.mousePosition); | |
RaycastHit hit; | |
if (Physics.Raycast(ray, out hit, float.MaxValue)) | |
{ | |
// Nullなら新規作成。 | |
if( this.primitive == null ) | |
this.primitive = this.CreatePrimitive(hit.point); | |
// nullでないなら追従 | |
else | |
this.primitive.transform.position = hit.point; | |
} | |
} | |
else if (Input.GetMouseButtonUp(0)) | |
{ | |
// ボタンを話したら消滅 | |
if (this.primitive != null) | |
{ | |
Destroy(this.primitive); | |
} | |
} | |
} | |
// 仕様にあったインターフェースで対象おオブジェクトを循環させる | |
GameObject CreatePrimitive(Vector3 position) | |
{ | |
if (this.currentPrimitiveIndex >= this.primitiveTypeList.Count ) | |
{ | |
this.currentPrimitiveIndex = 0; | |
} | |
var p = GameObject.CreatePrimitive( primitiveTypeList[this.currentPrimitiveIndex ++ ] ); | |
Destroy(p.GetComponent<Collider>()); | |
p.transform.position = position; | |
return p; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment