swich的封装方法
2018-03-30 14:01 陈徐永 阅读(268) 评论(0) 编辑 收藏 举报using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class RandomCube : MonoBehaviour
{
public GameObject[] prefabs;
List<GameObject> objs = new List<GameObject>();
public Material redmat;
public Material bluemat;
public Material greenmat;
private Dictionary<KeyCode, int> dic;
void Start ()
{
dic = new Dictionary<KeyCode, int>()
{
{ KeyCode.U,LayerMask.GetMask("Red")},
{ KeyCode.M,LayerMask.GetMask("Blue")},
{ KeyCode.N,LayerMask.GetMask("Green")}
};
int n = Random.Range(5, 10);
for (int i = 0; i < n; i++)
{
GameObject prefab = prefabs[i % prefabs.Length];
Vector3 pos = Random.insideUnitSphere * 10;
GameObject obj = Instantiate(prefab,pos,Quaternion.identity);
objs.Add(obj);
}
}
void Update () {
ShowRed();
ShowBlue();
ShowGreen();
}
private void ShowGreen()
{
Key2Color(KeyCode.N, greenmat);
}
private void ShowBlue()
{
Key2Color(KeyCode.M, bluemat);
}
private void ShowRed()
{
Key2Color(KeyCode.U, redmat);
}
public void Key2Color(KeyCode key,Material mat){
if (Input.GetKeyDown(key))
{
//foreach (GameObject o in objs)
//{
// Material omat = o.GetComponent<MeshRenderer>().material;
// o.SetActive(omat.color == mat.color);
//}
if(dic.ContainsKey(key))
Camera.main.cullingMask = dic[key];
}
}
}