PrintscreenItem.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using UnityEngine.UI;
  6. using XRTool.Util;
  7. public class PrintscreenItem : MonoBehaviour
  8. {
  9. public RawImage mainImage;
  10. public Text nameText;
  11. public RectTransform rectTransform;
  12. public SCButton deleteBtn;
  13. private void Start()
  14. {
  15. deleteBtn.onClick.AddListener(ClickOnDeleteBtn);
  16. }
  17. private void ClickOnDeleteBtn()
  18. {
  19. Destroy(this.gameObject);
  20. }
  21. private BoundingBox boundBox;
  22. public void Init(Texture2D tex)
  23. {
  24. if (tex)
  25. {
  26. Adaption(tex);
  27. mainImage.texture = tex;
  28. }
  29. if (!boundBox)
  30. {
  31. boundBox = GetComponent<BoundingBox>();
  32. if (!boundBox)
  33. {
  34. UnityLog.LogError(gameObject.name + "this is no XBoundingBox");
  35. }
  36. else
  37. {
  38. boundBox.ScaleStopped.AddListener(OnScaleStopped);
  39. }
  40. }
  41. }
  42. private void OnScaleStopped()
  43. {
  44. if (transform.localScale.x < 1f)
  45. {
  46. transform.localScale = Vector3.one;
  47. }
  48. }
  49. public void Adaption(Texture tex)
  50. {
  51. float standard_width = 150f;
  52. float standard_height = 84f;
  53. float video_width = tex.width;
  54. float video_height = tex.height;
  55. //Debug.Log(tex.width + "***" + tex.height);
  56. if (standard_width < video_width && standard_height > video_height)
  57. {
  58. float video_aspect = standard_width / video_width;
  59. rectTransform.sizeDelta = new Vector2(standard_width, video_height * video_aspect);
  60. }
  61. else if (standard_width > video_width && standard_height < video_height)
  62. {
  63. float video_aspect = standard_height / video_height;
  64. rectTransform.sizeDelta = new Vector2(video_width * video_aspect, standard_height);
  65. }
  66. else if (standard_width > video_width && standard_height > video_height)
  67. {
  68. if (standard_width / video_width > standard_height / video_height)
  69. {
  70. float video_aspect = standard_height / video_height;
  71. rectTransform.sizeDelta = new Vector2(video_width * video_aspect, video_height * video_aspect);
  72. }
  73. else
  74. {
  75. float video_aspect = standard_width / video_width;
  76. rectTransform.sizeDelta = new Vector2(video_width * video_aspect, video_height * video_aspect);
  77. }
  78. }
  79. else
  80. {
  81. if (standard_width / video_width > standard_height / video_height)
  82. {
  83. float video_aspect = standard_height / video_height;
  84. rectTransform.sizeDelta = new Vector2(video_width * video_aspect, video_height * video_aspect);
  85. }
  86. else
  87. {
  88. float video_aspect = standard_width / video_width;
  89. rectTransform.sizeDelta = new Vector2(video_width * video_aspect, video_height * video_aspect);
  90. }
  91. }
  92. }
  93. }