Search Unity

Cropping textures at runtime

Discussion in 'Scripting' started by Tripwire, Jan 31, 2016.

  1. Tripwire

    Tripwire

    Joined:
    Oct 12, 2010
    Posts:
    442
    Hi,

    I'm trying to figure out how to slice images at runtime for a project i'm doing on iOS. Here's a screenshot of what i'm trying to achieve:




    Since i'm using Mobilepaint to draw on a canvastexure, i'm using it's function to return the canvas i'm drawing on to a Texture2D. Here's my code so far:


    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using UnityEngine.UI;
    4. using System.Collections;
    5. using unitycoder_MobilePaint;
    6.  
    7. public class SliceDrawing : MonoBehaviour {
    8.  
    9.     public MobilePaint mobilePaint;
    10.     public Image testImage;
    11.  
    12.     public void startSlicing() {
    13.         Texture2D drawingTex = mobilePaint.GetCanvasAsTexture ();
    14.         print ("Tex height: " + drawingTex.height + " Screen height: " + Screen.height);
    15.         print ("Tex width: " + drawingTex.width + " Screen width: " + Screen.width);
    16.         Rect testRect = new Rect (0, 0, 455, Screen.height);
    17.         Sprite sprite = Sprite.Create (drawingTex, testRect, Vector2.zero);
    18.         testImage.sprite = sprite;
    19.         testImage.SetNativeSize ();
    20.     }
    21. }
    22.  
    So far it does seem to work but the image is smaller then my TestImage (which is the same size as the size of place number 3). All the sizes seem to be correct (Tex height & width are the same as Screen height and width). Also whenever I try to set the correct size of the testRect or try to set the sprite to the testImage size, Unity3D throws an error saying it can't create a textures bigger than the screensize.
    Anyone have an idea how to get this working?
     
  2. Tripwire

    Tripwire

    Joined:
    Oct 12, 2010
    Posts:
    442
    anyone?
     
  3. Tripwire

    Tripwire

    Joined:
    Oct 12, 2010
    Posts:
    442
    This thread can be closed I think I posted in the wrong section...
     
  4. Tripwire

    Tripwire

    Joined:
    Oct 12, 2010
    Posts:
    442
    So since it's now posted in the correct section (I think) can anyone help me out here?
     
  5. Tripwire

    Tripwire

    Joined:
    Oct 12, 2010
    Posts:
    442
    So i've played around with some coding an read alot online about the issue I'm having. It seems that cropping is the right name for my problem :)

    So here's my code which keeps throwing an error on creating a sprite: !hasError

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. using System.Collections;
    4. using unitycoder_MobilePaint;
    5.  
    6. public class SliceDrawing : MonoBehaviour {
    7.  
    8.     //The plugin which paints on a canvas
    9.     public MobilePaint mobilePaint;
    10.  
    11.     //crop parts (reference images to get the size and position where the cropping has to be done)
    12.     public Image topCropPart;
    13.     public Image bottomCropPart;
    14.     public Image leftCropPart;
    15.     public Image rightCropPart;
    16.  
    17.     public void startSlicing() {
    18.         Texture2D drawingTex = mobilePaint.GetCanvasAsTexture ();
    19.    
    20.         print(leftCropPart.GetComponent<CanvasRenderer> ().transform.position);
    21.         Transform trans = leftCropPart.GetComponent<CanvasRenderer> ().transform;
    22.  
    23.         int x = (int)trans.position.x;
    24.         int y = (int)trans.position.y;
    25.         int width = (int)trans.localScale.x;
    26.         int height = (int)trans.localScale.y;
    27.  
    28.         Color[] pixels = drawingTex.GetPixels (x, y, width, height, 0);
    29.        
    30.         print("Pos: x: " + x + " y: " + y + " Width: " + width + " Height: " + height);
    31.         Texture2D cropped = new Texture2D (width, height, TextureFormat.ARGB32, false);
    32.         cropped.SetPixels (0, 0, cropped.width, cropped.height, pixels, 0);
    33.         cropped.Apply ();
    34.  
    35.         // Place the left crop result in the right image to see the result
    36.         Sprite sprite = Sprite.Create (drawingTex, leftCropPart.rectTransform.rect, Vector2.zero);
    37.         rightCropPart.sprite = sprite;
    38.         rightCropPart.SetNativeSize ();
    39.     }
    40. }
     
    matthiaskruis likes this.