ToolMessageElement.cs 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /****************************************************************************
  2. * 2024.7 LXD
  3. ****************************************************************************/
  4. using System;
  5. using System.Collections.Generic;
  6. using UnityEngine;
  7. using UnityEngine.UI;
  8. using QFramework;
  9. using I2.Loc;
  10. namespace QFramework
  11. {
  12. public partial class ToolMessageElement : UIElement
  13. {
  14. /// <summary>
  15. /// 高度基准
  16. /// </summary>
  17. private float heightBasic = 219.6412f;
  18. /// <summary>
  19. /// 展示工具信息
  20. /// </summary>
  21. public void ShowToolInfo(ToolConfigInfo toolConfigInfo)
  22. {
  23. ToolName.text = toolConfigInfo.toolName;
  24. //"\u3000"首行缩进没效果,改用透明占位符
  25. ToolMessage.text = "<color=#FFFFFF00>占位</color>" + toolConfigInfo.toolDescription.Replace(" ", "\u3000");
  26. #region 多语言
  27. if (LocalizationConfig.localization && LocalizationManager.CurrentLanguage == "English")
  28. {
  29. ToolName.text = toolConfigInfo.en_toolName;
  30. ToolMessage.text = "<color=#FFFFFF00>XXXX</color>" + toolConfigInfo.en_toolDescription.Replace(" ", "\u00A0"); ;
  31. }
  32. #endregion
  33. Content.SetHeight(ToolMessage.preferredHeight);
  34. ToolConfigProxy toolConfigProxy = DAL.Instance.Get<ToolConfigProxy>();
  35. Sprite tmpSprite = toolConfigProxy.GetSpriteByToolName(toolConfigInfo.toolName);
  36. if (tmpSprite != null)
  37. {
  38. ToolIcon.sprite = tmpSprite;
  39. SetInfoOfImage(ToolIcon.sprite);
  40. }
  41. else
  42. {
  43. ToolIcon.sprite = null;
  44. }
  45. }
  46. /// <summary>
  47. /// 设置Image信息
  48. /// </summary>
  49. /// <param name="_sprite"></param>
  50. private void SetInfoOfImage(Sprite _sprite)
  51. {
  52. if (_sprite == null) return;
  53. float width = _sprite.texture.width * CalculateScale(_sprite.texture.height);
  54. float height = _sprite.texture.height * CalculateScale(_sprite.texture.height);
  55. ToolIcon.rectTransform.sizeDelta = new Vector2(width, height);
  56. ToolIcon.sprite = _sprite;
  57. }
  58. /// <summary>
  59. /// 计算比例,以高度为基数计算
  60. /// </summary>
  61. /// <param name="_spriteHeight"></param>
  62. private float CalculateScale(float _spriteHeight)
  63. {
  64. if (_spriteHeight <= heightBasic)
  65. {
  66. return 1;
  67. }
  68. else
  69. {
  70. return heightBasic / _spriteHeight;
  71. }
  72. }
  73. }
  74. }