using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class UI_demo : MonoBehaviour
{
//ui button
public Button[] _arr;
//字符串 动态
string str_model;
// Start is called before the first frame update
void Start()
{
//添加点击事件
for (int i = 0; i < _arr.Length; i++)
{
int temp=i;
_arr[temp].onClick.AddListener(delegate() { Out_model(_arr[temp].name); } );
}
}
// Update is called once per frame
void Update()
{
if (Input.GetMouseButton(1))
{
str_model = null;
}
Ray raydemo = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if(Physics.Raycast(raydemo,out hit, Mathf.Infinity, LayerMask.GetMask("building")))
{
if (Input.GetMouseButtonDown(0))
{
if (str_model != null)
{
//生成模型并赋予位置
Gement_demo(str_model).transform.position = hit.point;
print("模型");
str_model = null;
}
}
}
}
//索取生成字符
void Out_model(string str)
{
switch (str)
{
case "Cube":
str_model = str;
break;
case "Sphere":
str_model = str;
break;
case "Capsule":
str_model = str;
break;
case "Cylinder":
str_model = str;
break;
}
}
//生成obj
public GameObject Gement_demo(string str)
{
GameObject obj;
switch (str)
{
case "Cube":
obj = GameObject.CreatePrimitive(PrimitiveType.Cube);
break;
case "Sphere":
obj = GameObject.CreatePrimitive(PrimitiveType.Sphere);
break;
case "Capsule":
obj = GameObject.CreatePrimitive(PrimitiveType.Capsule);
break;
case "Cylinder":
obj = GameObject.CreatePrimitive(PrimitiveType.Cylinder);
break;
default:
print("null");
obj = null;
break;
}
return obj;
}
}