Skip to content

Instantly share code, notes, and snippets.

@s2kw
Created June 15, 2016 11:38
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/34c516bf155fa7f650c049248aedc3e5 to your computer and use it in GitHub Desktop.
Save s2kw/34c516bf155fa7f650c049248aedc3e5 to your computer and use it in GitHub Desktop.
毎日出題の3日目第三問の答え
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