123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267 |
- using UnityEngine;
- using System.Collections;
- using System.Collections.Generic;
- using System.IO;
- using UnityEngine.UI;
- public class TextureExam : MonoBehaviour
- {
- public int CurIndex;
- public Image CurImage;
- public SpriteRenderer CurSR;
- public InputField InputField;
- public InputField InputFieldPadding;
- public Camera Camera;
- public Transform ExamParent;
- public Sprite OriginSprite;
- public Texture2D OriginTexture;
- public Vector3 OriginSize;
- public void Start()
- {
- CurImage = ExamParent.GetChild(0).GetComponent<Image>();
- CurSR = ExamParent.GetChild(0).GetComponent<SpriteRenderer>();
- if (CurImage != null)
- {
- OriginSprite = CurImage.sprite;
- OriginTexture = CurImage.sprite.texture;
- CurImage.SetActive(true);
- }
- if (CurSR != null)
- {
- OriginSize = CurSR.transform.localScale;
- OriginSprite = CurSR.sprite;
- OriginTexture = CurSR.sprite.texture;
- CurSR.SetActive(true);
- }
- }
- public void SetReadable()
- {
- //for (int i = 0; i < ExamParent.childCount; i++)
- //{
- // ExamParent.GetChild(i).GetComponent<Image>.
- //}
- }
- public void ZoomIn()
- {
- Camera.orthographicSize = 1.5f;
- }
- public void ZoomIn2()
- {
- Camera.orthographicSize = 3.5f;
- }
- public void ZoomOut()
- {
- Camera.orthographicSize = 5f;
- }
- public void MoveUp()
- {
- Camera.transform.position += new Vector3(0, 0.25f, 0);
- }
- public void MoveDown()
- {
- Camera.transform.position += new Vector3(0, -0.25f, 0);
- }
- public void MoveLeft()
- {
- Camera.transform.position += new Vector3(-0.25f, 0, 0);
- }
- public void MoveRight()
- {
- Camera.transform.position += new Vector3(0.25f,0,0);
- }
- public void MoveToCenter()
- {
- Camera.transform.position = new Vector3(0, 0, -30);
- }
- public void Next()
- {
- if (CurIndex < ExamParent.childCount - 1)
- {
- if (CurImage != null)
- {
- CurImage.SetActive(false);
- }
- if (CurSR != null)
- {
- CurSR.SetActive(false);
- }
- CurIndex++;
- CurImage = ExamParent.GetChild(CurIndex).GetComponent<Image>();
- CurSR = ExamParent.GetChild(CurIndex).GetComponent<SpriteRenderer>();
- if (CurImage != null)
- {
- OriginSprite = CurImage.sprite;
- OriginTexture = CurImage.sprite.texture;
- CurImage.SetActive(true);
- }
- if (CurSR != null)
- {
- OriginSize = CurSR.transform.localScale;
- OriginSprite = CurSR.sprite;
- OriginTexture = CurSR.sprite.texture;
- CurSR.SetActive(true);
- }
- }
- }
- public void Previous()
- {
- if (CurIndex > 0)
- {
- if (CurImage != null)
- {
- CurImage.SetActive(false);
- }
- if (CurSR != null)
- {
- CurSR.SetActive(false);
- }
- CurIndex--;
- CurImage = ExamParent.GetChild(CurIndex).GetComponent<Image>();
- CurSR = ExamParent.GetChild(CurIndex).GetComponent<SpriteRenderer>();
- if (CurImage != null)
- {
- OriginSprite = CurImage.sprite;
- OriginTexture = CurImage.sprite.texture;
- CurImage.SetActive(true);
- }
- if (CurSR != null)
- {
- OriginSize = CurSR.transform.localScale;
- OriginSprite = CurSR.sprite;
- OriginTexture = CurSR.sprite.texture;
- CurSR.SetActive(true);
- }
- }
- }
- public void Scale()
- {
- float factor = float.Parse(InputField.text);
- int padding = int.Parse(InputFieldPadding.text);
- Texture2D newTexture = ScaleTextureCore(padding, factor, OriginTexture);
- newTexture.Apply();
-
- Vector2 pivot = new Vector2(OriginSprite.pivot.x/OriginSprite.rect.width, OriginSprite.pivot.y / OriginSprite.rect.height);
- Sprite newSprite = Sprite.Create(newTexture, new Rect(0, 0, newTexture.width, newTexture.height), pivot, OriginSprite.pixelsPerUnit, 1, SpriteMeshType.Tight, OriginSprite.border);
- if (CurSR != null)
- {
- CurSR.sprite = newSprite;
- CurSR.transform.localScale = OriginSize/factor;
- }
- if (CurImage != null)
- {
- CurImage.sprite = newSprite;
- }
- }
- public void Record()
- {
- StreamWriter streamWriter = new StreamWriter(Application.persistentDataPath + "/TextureExam.txt", true);
- streamWriter.WriteLine(OriginSprite.name + " " + InputField.text + " " + InputFieldPadding.text);
- streamWriter.Close();
- }
- protected Texture2D ScaleTextureCore(int padding, float factor, Texture2D sourceTexture)
- {
- if (factor.Equal(1))
- {
- Texture2D texture2D = new Texture2D(sourceTexture.width, sourceTexture.height);
- texture2D.SetPixels(sourceTexture.GetPixels());
- return texture2D;
- }
- Texture2D texture = AddPaddingToTexture(padding, sourceTexture);
- int originWidth = texture.width;
- int originHeight = texture.height;
- int newWidth = (int)(originWidth * factor);
- int newHeight = (int)(originHeight * factor);
- Texture2D newTexture = new Texture2D(newWidth, newHeight);
- Color[] newColors = new Color[newWidth * newHeight];
- for (int i = 0; i < newHeight; i++)
- {
- for (int j = 0; j < newWidth; j++)
- {
- newColors[i*newWidth + j] = texture.GetPixelBilinear((j + 0.5f)/newWidth, (i + 0.5f)/newHeight);
- }
- }
- newTexture.SetPixels(newColors);
- return newTexture;
- }
- protected Texture2D AddPaddingToTexture(int padding, Texture2D texture)
- {
- if (padding == 0)
- {
- return texture;
- }
- int originWidth = texture.width;
- int originHeight = texture.height;
- int newWidth = originWidth + 2 * padding;
- int newHeight = originHeight + 2 * padding;
- Texture2D newTexture = new Texture2D(newWidth, newHeight);
- Color[] newColors = new Color[newWidth * newHeight];
- Color[] originColors = texture.GetPixels();
- for (int i = 0; i < originHeight; i++)
- {
- for (int j = 0; j < originWidth; j++)
- {
- newColors[(i + padding) * newWidth + (j + padding)] = originColors[i * originWidth + j];
- }
- }
- newTexture.SetPixels(newColors);
- return newTexture;
- }
- }
|