json - 当有 2 个同名游戏对象时,如何更改 UI/图像?
问题描述
我正在为 hololens 开发一个应用程序,该应用程序使用带有 UI/Image 元素的面板生成多个画布。我有 1 个 JSON 字符串:
{
"PC_Station":[
{
"PLC_0":{
"DB1":{
"test123":0
},
"STOP":false,
"Frap":false,
"START":false,
"Start_1":false,
"Stop_1":false,
"Led1":true,
"Led2":false,
"Led3":true,
"Counter":4002,
"Sliderval":0
}
},
{
"PLC_1":{
"DB1":{
"test123":55
},
"STOP":false,
"START":false,
"Start_1":false,
"Stop_1":false,
"Led1":true,
"Led2":false,
"Led3":true,
"Counter":4002,
"Sliderval":0
}
}
]
}
此 JSON 字符串在名为 PLC_1 和 PLC_0 的 JSON 数组内有 2 个 JSON 对象。PLC_1 具有与 PLC_0 相同的变量。
我制作了以下函数来附加 JSON 并更改 UI/Image 对象的颜色:
IEnumerator updateTags()
{
string json = "{\"PC_Station\": [{\"PLC_1\": {\"DB1\": {\"test123\": 30}, \"STOP\": false, \"START\": true, \"Start_1\": false, \"Stop_1\": true, \"Led1\": false, \"Led2\": false, \"Led3\": false, \"Counter\": 3880, \"Sliderval\": 60}}]}";
string json1 = "{\"PC_Station\": [{\"PLC_0\": {\"DB1\": {\"test123\": 0}, \"STOP\": false,\"Frap\": false, \"START\": false, \"Start_1\": false, \"Stop_1\": false, \"Led1\": true, \"Led2\": false, \"Led3\": true, \"Counter\": 4002, \"Sliderval\": 0}},{\"PLC_1\": {\"DB1\": {\"test123\": 55}, \"STOP\": false, \"START\": false, \"Start_1\": false, \"Stop_1\": false, \"Led1\": true, \"Led2\": false, \"Led3\": true, \"Counter\": 4002, \"Sliderval\": 0}}]}";
var data = JToken.Parse(json1);
while (true)
{
foreach (var value in data)
{
foreach(JArray arr in value)
{
for (int i = 0; i < arr.Count; i++)
{
foreach (var item in arr[i])
{
var itemproperties = item.Parent;
foreach (JToken token in itemproperties)
{
var prop = token as JProperty;
var plc = (JObject)prop.Value;
foreach (KeyValuePair<string, JToken> val in plc)
{
if(val.Value is JObject)
{
JObject nestedobj = (JObject)val.Value;
foreach (JProperty nestedvariables in nestedobj.Properties())
{
string varkey = nestedvariables.Name;
string varvalue = nestedvariables.Value.ToString();
GameObject test = GameObject.Find(varkey+"value");
test.GetComponent<Text>().text = varvalue;
}
}
else
{
for(int v = 0; v < abc.Count; v++)
{
Debug.Log(v);
foreach(KeyValuePair<string, string> variab in abc[v])
{
string varkey = val.Key;
string varvalue = val.Value.ToString();
GameObject test = GameObject.Find(varkey);
if(varvalue == "True")
{
test.GetComponent<Image>().color = Color.green;
}
if(varvalue == "False")
{
test.GetComponent<Image>().color = Color.red;
}
if(varvalue != "True" && varvalue != "False")
{
GameObject text = GameObject.Find(varkey + "value");
text.GetComponent<Text>().text = varvalue;
}
}
}
}
}
}
}
}
}
}
yield return new WaitForSeconds(0.5f);
}
}
当我运行程序时,它看起来像这样:
如您所见,该函数正确地为 UI/图像添加颜色。
现在,对于我的问题:
尽管名称相同,如何才能使两个画布上的 UI/图像都充满颜色?
解决方案
没有明显的解决方案,因为 GameObject 引用不容易序列化,您可以通过它们在树的当前分支中的位置来引用对象 (transform.GetSiblingIndex()),但如果您移动东西,这将不起作用。您也可以只重命名您的对象。
推荐阅读
- python - Pandas:当值为 url 时如何避免重复值?
- curl - kubernetes:使用 externalServices 卷曲外部站点不起作用
- c++ - 在 Linux 上构建的库中的 JNI 缺少符号
- arm-template - ARM 模板部署脚本自定义容器实例文件共享名称
- c# - C# MVC 允许更新重新编辑的字段
- tensorflow - 预测概率是否高于某个值
- swift - 从文件应用程序将 mp3 文件添加到 iOS 应用程序的内部存储
- asp.net-mvc - 生成文档的最佳 ASP.NET MVC 方式
- c++ - 显式类型转换真的有必要吗?
- r - R:为数据框生成动态名称