123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202 |
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.UIElements;
- // 这个controller 是用于控制 Status UI菜单的
- public class StatusController : MonoBehaviour
- {
- public Button backButton;
- public Label nameLabel, genderLabel, breedLabel, hostLabel, hostnameLabel, ageLabel, ageValueLabel, statusLabel, statusValueLabel;
- DogProperty puppy;
- public VisualElement selectElement;
- // Start is called before the first frame update
- void Start()
- {
- var root = GetComponent<UIDocument>().rootVisualElement;
- backButton = root.Q<Button>("back");
- nameLabel = root.Q<Label>("name");
- genderLabel = root.Q<Label>("gender");
- breedLabel = root.Q<Label>("breed");
- hostLabel = root.Q<Label>("host");
- hostnameLabel = root.Q<Label>("hostname");
- ageLabel = root.Q<Label>("age");
- ageValueLabel = root.Q<Label>("ageValue");
- statusLabel = root.Q<Label>("status");
- statusValueLabel = root.Q<Label>("statusValue");
- selectElement = root.Q("selectElement");
- // todo 目前只有一个狗,以后会添加这里要改
- puppy = EnviromentSetting.puppies[0];
- StatusPageUpdate();
- LabelLanguageSetting();
- StatusSummary();
- }
- // Update is called once per frame
- //void Update()
- //{
- //}
- void BackPressed()
- {
- if (backButton != null)
- {
- var root = GetComponent<UIDocument>();
- Destroy(GameObject.Find(root.GetComponentInParent<Canvas>().name));
- }
- }
- void StatusPageUpdate()
- {
- backButton.clicked += BackPressed;
- nameLabel.text = puppy.name;
- if (puppy.gender == 1) {
- genderLabel.text = "♂";
- }
- else
- {
- genderLabel.text = "♀";
- }
- breedLabel.text = puppy.breed;
- hostnameLabel.text = UserProperty.name;
- // 计算狗生日和现在时间差
- TimeSpan ts = System.DateTime.Now - puppy.brithday;
- ageValueLabel.text = ts.Days.ToString();
- //根据狗的数量添加选择球
- int puppyQty = EnviromentSetting.puppies.Count;
- VisualTreeAsset selectBall = Resources.Load<VisualTreeAsset>("Status/SelectBall");
- // 这里将VisualTreeAsset转换成VisualElement类型
- VisualElement uiDocument = selectBall.CloneTree();
- VisualElement[] selectBalls = new VisualElement[puppyQty];
- for (int i = 0; i<puppyQty; i++)
- {
- selectBalls[i] = uiDocument;
- selectElement.Add(selectBalls[i]);
- }
- // todo 设置selectBall颜色对应选择
- }
- void LabelLanguageSetting()
- {
- // 设置status UI界面里面label和按键的语言显示
- string textValue = EnviromentSetting.GetValueAtPath(EnviromentSetting.languageData, new string[] { "statusUI", "button", "back", EnviromentSetting.languageCode });
- backButton.text = textValue;
- textValue = EnviromentSetting.GetValueAtPath(EnviromentSetting.languageData, new string[] { "statusUI", "label", "host", EnviromentSetting.languageCode });
- hostLabel.text = textValue;
- textValue = EnviromentSetting.GetValueAtPath(EnviromentSetting.languageData, new string[] { "statusUI", "label", "days", EnviromentSetting.languageCode });
- ageLabel.text = textValue;
- textValue = EnviromentSetting.GetValueAtPath(EnviromentSetting.languageData, new string[] { "statusUI", "label", "status", EnviromentSetting.languageCode });
- statusLabel.text = textValue;
- }
- void StatusSummary()
- {
- // 先清空状态文字,应对切换多只狗
- statusValueLabel.text = null;
- // 汇总宠物状态
- string summary = "";
- if (puppy.satiety < 30)
- {
- if (puppy.satiety < 10)
- {
- // 小于10,达到L2警告
- summary += EnviromentSetting.GetValueAtPath(EnviromentSetting.languageData, new string[] { "statusUI", "status", "satiety_2", EnviromentSetting.languageCode });
- }
- else
- {
- // 不小于10,触发L1警告
- summary += EnviromentSetting.GetValueAtPath(EnviromentSetting.languageData, new string[] { "statusUI", "status", "satiety_1", EnviromentSetting.languageCode });
- }
- summary += "<br>";
- }
- if (puppy.stamina < 30)
- {
- if (puppy.stamina < 10)
- {
- // 小于10,达到L2警告
- summary += EnviromentSetting.GetValueAtPath(EnviromentSetting.languageData, new string[] { "statusUI", "status", "stamina_2", EnviromentSetting.languageCode });
- }
- else
- {
- // 不小于10,触发L1警告
- summary += EnviromentSetting.GetValueAtPath(EnviromentSetting.languageData, new string[] { "statusUI", "status", "stamina_1", EnviromentSetting.languageCode });
- }
- summary += "<br>";
- }
- if (puppy.thirsty < 30)
- {
- if (puppy.thirsty < 10)
- {
- // 小于10,达到L2警告
- summary += EnviromentSetting.GetValueAtPath(EnviromentSetting.languageData, new string[] { "statusUI", "status", "thirsty_2", EnviromentSetting.languageCode });
- }
- else
- {
- // 不小于10,触发L1警告
- summary += EnviromentSetting.GetValueAtPath(EnviromentSetting.languageData, new string[] { "statusUI", "status", "thirsty_1", EnviromentSetting.languageCode });
- }
- summary += "<br>";
- }
- if (puppy.healthy < 30)
- {
- if (puppy.healthy < 10)
- {
- // 小于10,达到L2警告
- summary += EnviromentSetting.GetValueAtPath(EnviromentSetting.languageData, new string[] { "statusUI", "status", "healthy_2", EnviromentSetting.languageCode });
- }
- else
- {
- // 不小于10,触发L1警告
- summary += EnviromentSetting.GetValueAtPath(EnviromentSetting.languageData, new string[] { "statusUI", "status", "healthy_1", EnviromentSetting.languageCode });
- }
- summary += "<br>";
- }
- if (puppy.clean < 30)
- {
- if (puppy.clean < 10)
- {
- // 小于10,达到L2警告
- summary += EnviromentSetting.GetValueAtPath(EnviromentSetting.languageData, new string[] { "statusUI", "status", "clean_2", EnviromentSetting.languageCode });
- }
- else
- {
- // 不小于10,触发L1警告
- summary += EnviromentSetting.GetValueAtPath(EnviromentSetting.languageData, new string[] { "statusUI", "status", "clean_1", EnviromentSetting.languageCode });
- }
- summary += "<br>";
- }
- if (puppy.obesity > 70)
- {
- if (puppy.obesity >90)
- {
- // 小于10,达到L2警告
- summary += EnviromentSetting.GetValueAtPath(EnviromentSetting.languageData, new string[] { "statusUI", "status", "obesity_2", EnviromentSetting.languageCode });
- }
- else
- {
- // 不小于10,触发L1警告
- summary += EnviromentSetting.GetValueAtPath(EnviromentSetting.languageData, new string[] { "statusUI", "status", "obesity_1", EnviromentSetting.languageCode });
- }
- summary += "<br>";
- }
-
- // 如果没有任何异常,返回正常的状态
- if (summary.Length == 0)
- {
- summary = EnviromentSetting.GetValueAtPath(EnviromentSetting.languageData, new string[] { "statusUI", "status", "normal", EnviromentSetting.languageCode });
- }
- statusValueLabel.text = summary;
- }
- }
|