BigThumbnail.cs 5.4 KB

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