TextureExam.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using UnityEngine.UI;
  6. public class TextureExam : MonoBehaviour
  7. {
  8. public int CurIndex;
  9. public Image CurImage;
  10. public SpriteRenderer CurSR;
  11. public InputField InputField;
  12. public InputField InputFieldPadding;
  13. public Camera Camera;
  14. public Transform ExamParent;
  15. public Sprite OriginSprite;
  16. public Texture2D OriginTexture;
  17. public Vector3 OriginSize;
  18. public void Start()
  19. {
  20. CurImage = ExamParent.GetChild(0).GetComponent<Image>();
  21. CurSR = ExamParent.GetChild(0).GetComponent<SpriteRenderer>();
  22. if (CurImage != null)
  23. {
  24. OriginSprite = CurImage.sprite;
  25. OriginTexture = CurImage.sprite.texture;
  26. CurImage.SetActive(true);
  27. }
  28. if (CurSR != null)
  29. {
  30. OriginSize = CurSR.transform.localScale;
  31. OriginSprite = CurSR.sprite;
  32. OriginTexture = CurSR.sprite.texture;
  33. CurSR.SetActive(true);
  34. }
  35. }
  36. public void SetReadable()
  37. {
  38. //for (int i = 0; i < ExamParent.childCount; i++)
  39. //{
  40. // ExamParent.GetChild(i).GetComponent<Image>.
  41. //}
  42. }
  43. public void ZoomIn()
  44. {
  45. Camera.orthographicSize = 1.5f;
  46. }
  47. public void ZoomIn2()
  48. {
  49. Camera.orthographicSize = 3.5f;
  50. }
  51. public void ZoomOut()
  52. {
  53. Camera.orthographicSize = 5f;
  54. }
  55. public void MoveUp()
  56. {
  57. Camera.transform.position += new Vector3(0, 0.25f, 0);
  58. }
  59. public void MoveDown()
  60. {
  61. Camera.transform.position += new Vector3(0, -0.25f, 0);
  62. }
  63. public void MoveLeft()
  64. {
  65. Camera.transform.position += new Vector3(-0.25f, 0, 0);
  66. }
  67. public void MoveRight()
  68. {
  69. Camera.transform.position += new Vector3(0.25f,0,0);
  70. }
  71. public void MoveToCenter()
  72. {
  73. Camera.transform.position = new Vector3(0, 0, -30);
  74. }
  75. public void Next()
  76. {
  77. if (CurIndex < ExamParent.childCount - 1)
  78. {
  79. if (CurImage != null)
  80. {
  81. CurImage.SetActive(false);
  82. }
  83. if (CurSR != null)
  84. {
  85. CurSR.SetActive(false);
  86. }
  87. CurIndex++;
  88. CurImage = ExamParent.GetChild(CurIndex).GetComponent<Image>();
  89. CurSR = ExamParent.GetChild(CurIndex).GetComponent<SpriteRenderer>();
  90. if (CurImage != null)
  91. {
  92. OriginSprite = CurImage.sprite;
  93. OriginTexture = CurImage.sprite.texture;
  94. CurImage.SetActive(true);
  95. }
  96. if (CurSR != null)
  97. {
  98. OriginSize = CurSR.transform.localScale;
  99. OriginSprite = CurSR.sprite;
  100. OriginTexture = CurSR.sprite.texture;
  101. CurSR.SetActive(true);
  102. }
  103. }
  104. }
  105. public void Previous()
  106. {
  107. if (CurIndex > 0)
  108. {
  109. if (CurImage != null)
  110. {
  111. CurImage.SetActive(false);
  112. }
  113. if (CurSR != null)
  114. {
  115. CurSR.SetActive(false);
  116. }
  117. CurIndex--;
  118. CurImage = ExamParent.GetChild(CurIndex).GetComponent<Image>();
  119. CurSR = ExamParent.GetChild(CurIndex).GetComponent<SpriteRenderer>();
  120. if (CurImage != null)
  121. {
  122. OriginSprite = CurImage.sprite;
  123. OriginTexture = CurImage.sprite.texture;
  124. CurImage.SetActive(true);
  125. }
  126. if (CurSR != null)
  127. {
  128. OriginSize = CurSR.transform.localScale;
  129. OriginSprite = CurSR.sprite;
  130. OriginTexture = CurSR.sprite.texture;
  131. CurSR.SetActive(true);
  132. }
  133. }
  134. }
  135. public void Scale()
  136. {
  137. float factor = float.Parse(InputField.text);
  138. int padding = int.Parse(InputFieldPadding.text);
  139. Texture2D newTexture = ScaleTextureCore(padding, factor, OriginTexture);
  140. newTexture.Apply();
  141. Vector2 pivot = new Vector2(OriginSprite.pivot.x/OriginSprite.rect.width, OriginSprite.pivot.y / OriginSprite.rect.height);
  142. Sprite newSprite = Sprite.Create(newTexture, new Rect(0, 0, newTexture.width, newTexture.height), pivot, OriginSprite.pixelsPerUnit, 1, SpriteMeshType.Tight, OriginSprite.border);
  143. if (CurSR != null)
  144. {
  145. CurSR.sprite = newSprite;
  146. CurSR.transform.localScale = OriginSize/factor;
  147. }
  148. if (CurImage != null)
  149. {
  150. CurImage.sprite = newSprite;
  151. }
  152. }
  153. public void Record()
  154. {
  155. StreamWriter streamWriter = new StreamWriter(Application.persistentDataPath + "/TextureExam.txt", true);
  156. streamWriter.WriteLine(OriginSprite.name + " " + InputField.text + " " + InputFieldPadding.text);
  157. streamWriter.Close();
  158. }
  159. protected Texture2D ScaleTextureCore(int padding, float factor, Texture2D sourceTexture)
  160. {
  161. if (factor.Equal(1))
  162. {
  163. Texture2D texture2D = new Texture2D(sourceTexture.width, sourceTexture.height);
  164. texture2D.SetPixels(sourceTexture.GetPixels());
  165. return texture2D;
  166. }
  167. Texture2D texture = AddPaddingToTexture(padding, sourceTexture);
  168. int originWidth = texture.width;
  169. int originHeight = texture.height;
  170. int newWidth = (int)(originWidth * factor);
  171. int newHeight = (int)(originHeight * factor);
  172. Texture2D newTexture = new Texture2D(newWidth, newHeight);
  173. Color[] newColors = new Color[newWidth * newHeight];
  174. for (int i = 0; i < newHeight; i++)
  175. {
  176. for (int j = 0; j < newWidth; j++)
  177. {
  178. newColors[i*newWidth + j] = texture.GetPixelBilinear((j + 0.5f)/newWidth, (i + 0.5f)/newHeight);
  179. }
  180. }
  181. newTexture.SetPixels(newColors);
  182. return newTexture;
  183. }
  184. protected Texture2D AddPaddingToTexture(int padding, Texture2D texture)
  185. {
  186. if (padding == 0)
  187. {
  188. return texture;
  189. }
  190. int originWidth = texture.width;
  191. int originHeight = texture.height;
  192. int newWidth = originWidth + 2 * padding;
  193. int newHeight = originHeight + 2 * padding;
  194. Texture2D newTexture = new Texture2D(newWidth, newHeight);
  195. Color[] newColors = new Color[newWidth * newHeight];
  196. Color[] originColors = texture.GetPixels();
  197. for (int i = 0; i < originHeight; i++)
  198. {
  199. for (int j = 0; j < originWidth; j++)
  200. {
  201. newColors[(i + padding) * newWidth + (j + padding)] = originColors[i * originWidth + j];
  202. }
  203. }
  204. newTexture.SetPixels(newColors);
  205. return newTexture;
  206. }
  207. }