using System.Collections; using System.Collections.Generic; using UnityEngine; #region 当前脚本的引用路径 /***************************************************************** "Canvas/BodyAndOrgans "Canvas/BrainRegions *****************************************************************/ #endregion public class AddMaterialToBrainAndOrgans : MonoBehaviour { public ModelType modelType; // 声明为静态变量,便于在别的类中调用,因为大脑的材质需要随着BIS值的变化而变化 public Material brainMat; public void Update() { float bisValue = 0; brainMat.color = Color.green; // if (!MySqlDataSource.curFeatureTableDataDict.ContainsKey("BIS")) // { // return; // } // bool isBisValueParsed = float.TryParse(MySqlDataSource.curFeatureTableDataDict["BIS"], out bisValue); bool isBisValueParsed = true; if (!isBisValueParsed) { // 如果无法解析 BIS 值,您可能需要处理这种异常情况 return; } if (bisValue <= 0) { brainMat.color = Color.green; } if (bisValue >=40 && bisValue <= 60) { brainMat.color = Color.green; } else { brainMat.color = Color.red; } } private void Awake() { InitModelsList(); } private void Start() { AddMatToModel1(); } /// /// 因为每个脑区的颜色不同,需要为不同脑区模型添加对应的材质,器官模型也一样 /// void AddMatToModel() { List models = new List(); Dictionary tempMatDict = new Dictionary(); if (modelType == ModelType.brain) { models = DataSource.regions; tempMatDict = DataSource.regionMatDict; } else { models = DataSource.organs; tempMatDict = DataSource.organMatDict; } foreach (Transform obj in models) { string objName = obj.gameObject.name; if (tempMatDict.ContainsKey(objName)) { obj.GetComponent().material = tempMatDict[objName]; } else { Debug.Log(objName + " add Mat failed, model name is not matching with ModelMaterialDict!"); } } } /// /// 让所有脑区模型使用一个材质(材质的颜色随着麻醉深度的变化而变化-->在其他函数中实现) /// 人体器官模型还是不同器官使用不同材质 /// void AddMatToModel1() { List models = new List(); if (modelType == ModelType.brain) { models = DataSource.regions; brainMat = Resources.Load("Models/Mat/BrainMat"); if(brainMat != null) { foreach (Transform obj in models) { if(obj != null) { obj.GetComponent().material = brainMat; } } } } else { Dictionary tempMatDict = new Dictionary(); models = DataSource.organs; tempMatDict = DataSource.organMatDict; foreach (Transform obj in models) { if (obj != null) { string objName = obj.gameObject.name; if (tempMatDict.ContainsKey(objName)) obj.GetComponent().material = tempMatDict[objName]; else Debug.Log(objName + " add Mat failed, model name is not matching with ModelMaterialDict!"); } } } } #region 初始化脑区模型和器官模型的静态列表 void InitModelsList() { if (modelType == ModelType.brain) { int regionCount = transform.childCount; //遍历脑区模型,添加到列表中 for (int idx = 0; idx < regionCount; idx++) { Transform region = transform.GetChild(idx); // 获取脑区模型 DataSource.regions.Add(region); } } else { // 人体器官模型有两层目录,第一层目录是大的器官种类,如消化系统,神经系统等 // 第二层目录是具体的器官,如盲肠,尿管等,所以需要两层for循环来提取所有模型 int chdCount = transform.childCount; //遍历第一层目录 for (int i = 0; i < chdCount; i++) { Transform curChild = transform.GetChild(i); if (curChild.name != "Location") { int chdCount2 = curChild.childCount; // 如果当前目录中包含子物体,则继续遍历目录下的子物体 if (chdCount2 > 0) { for (int j = 0; j < chdCount2; j++) { // 把提取到的模型添加到列表中 Transform curChild2 = curChild.GetChild(j); DataSource.organs.Add(curChild2); } } else { DataSource.organs.Add(curChild); } } } } } #endregion }