BigThumbnail.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. using System;
  2. using System.IO;
  3. using UnityEngine;
  4. public class BigThumbnail : MonoBehaviour
  5. {
  6. public void Awake()
  7. {
  8. this.isEnabled = true;
  9. this.Init();
  10. }
  11. private void Init()
  12. {
  13. if (this.sprite_ == null)
  14. {
  15. this.CheckCreateFrame();
  16. this.sprite_ = UTY.GetChildObject(base.gameObject, "Image", false).GetComponent<UI2DSprite>();
  17. this.bg_sprite_ = base.gameObject.GetComponent<UI2DSprite>();
  18. this.frame_sprite_ = UTY.GetChildObject(base.gameObject, "Frame", false).GetComponent<UI2DSprite>();
  19. this.alpha = 0f;
  20. }
  21. }
  22. public void OnDestroy()
  23. {
  24. if (this.sprite_.sprite2D != null && this.sprite_.sprite2D.texture != null)
  25. {
  26. UnityEngine.Object.DestroyImmediate(this.sprite_.sprite2D.texture);
  27. }
  28. if (this.bg_sprite_.sprite2D != null && this.bg_sprite_.sprite2D.texture != null)
  29. {
  30. UnityEngine.Object.DestroyImmediate(this.bg_sprite_.sprite2D.texture);
  31. }
  32. if (this.frame_sprite_.sprite2D != null && this.frame_sprite_.sprite2D.texture != null)
  33. {
  34. UnityEngine.Object.DestroyImmediate(this.frame_sprite_.sprite2D.texture);
  35. }
  36. this.sprite_.sprite2D = null;
  37. this.bg_sprite_.sprite2D = null;
  38. this.frame_sprite_.sprite2D = null;
  39. this.draw_tex_ = null;
  40. }
  41. public void SetMaid(Maid maid)
  42. {
  43. if (!this.isEnabled)
  44. {
  45. return;
  46. }
  47. this.Init();
  48. this.alpha = 0f;
  49. if (this.sprite_.sprite2D != null && this.sprite_.sprite2D.texture != null)
  50. {
  51. UnityEngine.Object.DestroyImmediate(this.sprite_.sprite2D.texture);
  52. }
  53. this.sprite_.sprite2D = null;
  54. this.draw_tex_ = null;
  55. if (maid == null)
  56. {
  57. return;
  58. }
  59. this.draw_tex_ = maid.GetThumCard();
  60. if (this.draw_tex_ == null)
  61. {
  62. return;
  63. }
  64. this.LoadFrameAndBG();
  65. Sprite sprite = Sprite.Create(this.draw_tex_, new Rect(0f, 0f, (float)this.draw_tex_.width, (float)this.draw_tex_.height), default(Vector2));
  66. sprite.name = maid.status.lastName + maid.status.firstName;
  67. this.sprite_.sprite2D = sprite;
  68. this.sprite_.SetDimensions(this.draw_tex_.width, this.draw_tex_.height);
  69. this.alpha = 1f;
  70. }
  71. public float alpha
  72. {
  73. get
  74. {
  75. this.Init();
  76. return this.bg_sprite_.alpha;
  77. }
  78. set
  79. {
  80. this.Init();
  81. this.bg_sprite_.alpha = value;
  82. }
  83. }
  84. public bool Visible
  85. {
  86. get
  87. {
  88. return base.gameObject.activeSelf;
  89. }
  90. set
  91. {
  92. base.gameObject.SetActive(value);
  93. }
  94. }
  95. public bool isEnabled { get; set; }
  96. private Texture2D draw_tex
  97. {
  98. get
  99. {
  100. return this.draw_tex_;
  101. }
  102. }
  103. private void CheckCreateFrame()
  104. {
  105. string str = UTY.gameProjectPath + "\\";
  106. string text = str + "Thumb";
  107. if (!Directory.Exists(text))
  108. {
  109. Directory.CreateDirectory(text);
  110. }
  111. string path = text + "/frame_single.png";
  112. if (!File.Exists(path))
  113. {
  114. Texture2D texture2D = Resources.Load<Texture2D>("CharacterSelect/Atlas/DefaultFrame");
  115. File.WriteAllBytes(path, texture2D.EncodeToPNG());
  116. Resources.UnloadAsset(texture2D);
  117. }
  118. path = text + "/frame_back.png";
  119. if (!File.Exists(path))
  120. {
  121. Texture2D texture2D2 = Resources.Load<Texture2D>("CharacterSelect/Atlas/DefaultFrameBack");
  122. File.WriteAllBytes(path, texture2D2.EncodeToPNG());
  123. Resources.UnloadAsset(texture2D2);
  124. }
  125. }
  126. private void LoadFrameAndBG()
  127. {
  128. this.CheckCreateFrame();
  129. string str = UTY.gameProjectPath + "\\Thumb";
  130. string f_strFileName = str + "/frame_back.png";
  131. if (this.bg_sprite_.sprite2D == null)
  132. {
  133. Texture2D texture2D = UTY.LoadTexture(f_strFileName);
  134. if (texture2D != null)
  135. {
  136. int width = this.bg_sprite_.width;
  137. int height = this.bg_sprite_.height;
  138. Sprite sprite = Sprite.Create(texture2D, new Rect(0f, 0f, (float)width, (float)height), default(Vector2));
  139. sprite.name = "frame_back";
  140. this.bg_sprite_.sprite2D = sprite;
  141. this.bg_sprite_.SetDimensions(width, height);
  142. }
  143. }
  144. if (this.frame_sprite_.sprite2D == null)
  145. {
  146. f_strFileName = str + "/frame_single.png";
  147. Texture2D texture2D2 = UTY.LoadTexture(f_strFileName);
  148. if (texture2D2 != null)
  149. {
  150. int width2 = this.frame_sprite_.width;
  151. int height2 = this.frame_sprite_.height;
  152. Sprite sprite2 = Sprite.Create(texture2D2, new Rect(0f, 0f, (float)width2, (float)height2), default(Vector2));
  153. sprite2.name = "frame_back";
  154. this.frame_sprite_.sprite2D = sprite2;
  155. this.frame_sprite_.SetDimensions(width2, height2);
  156. }
  157. }
  158. }
  159. private Texture2D draw_tex_;
  160. private UI2DSprite sprite_;
  161. private UI2DSprite bg_sprite_;
  162. private UI2DSprite frame_sprite_;
  163. }