|
@@ -9,8 +9,12 @@ public class TextureExam : MonoBehaviour
|
|
|
public int CurIndex;
|
|
|
public Image CurImage;
|
|
|
public SpriteRenderer CurSR;
|
|
|
+ public InputField InputField;
|
|
|
public Camera Camera;
|
|
|
public Transform ExamParent;
|
|
|
+ public Sprite OriginSprite;
|
|
|
+ public Texture2D OriginTexture;
|
|
|
+ public Vector3 OriginSize;
|
|
|
|
|
|
public void ZoomIn()
|
|
|
{
|
|
@@ -67,11 +71,18 @@ public class TextureExam : MonoBehaviour
|
|
|
|
|
|
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);
|
|
|
}
|
|
|
}
|
|
@@ -97,13 +108,111 @@ public class TextureExam : MonoBehaviour
|
|
|
|
|
|
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);
|
|
|
+
|
|
|
+ Texture2D newTexture = ScaleTextureCore(1, 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;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ 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;
|
|
|
+ }
|
|
|
}
|