how to read portion or are of one texture and apply to another?

hello
how to read texture part and set it to another texture ?
i tried this.

but did not get exact o/p:

using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour {

public bool grab;
public Renderer display;
public GameObject PIXOBj;
private Texture2D tex;
public Texture2D TextureTobRead;

public Texture2D sourceTex;
public Rect sourceRect;
IEnumerator readPix1()
{
	yield return new WaitForEndOfFrame();

	TextureTobRead =  (Texture2D)PIXOBj.GetComponent<Renderer> ().material.mainTexture;

// int x = Mathf.FloorToInt(sourceRect.x);
// int y = Mathf.FloorToInt(sourceRect.x);

	int width = Mathf.FloorToInt(TextureTobRead.width);
	int height = Mathf.FloorToInt(TextureTobRead.height);

	Color[] pix = TextureTobRead.GetPixels(214,201, width,height );
	//Color[] pix = TextureTobRead.GetPixels(220, 135, width, height);
	Texture2D destTex = new Texture2D(width, height);
	destTex.SetPixels(pix);
	destTex.Apply();
	display.material.mainTexture = destTex;
}
public void Click()
{
	StartCoroutine(readPix1());
}

}

Make sure the texture is actually set to read/write enabled in the import settings. (You can change that by choosing the advanced option). You might also encounter some format issues, if so, make sure it’s imported correctly.

Besides that, the width and height values are already integer values, there’s no need to use Mathf.FloorToInt for them.
Also, GetPixels requires you to provide the blockWidth and blockHeight, not the full width and height unless you start at 0,0. In your case, that is, with the index values x=214 and y= 201, those can only be width-214 and height-201, if you pass the full width and length you’ll be trying to read non-existing pixels.

okey thank you bro

this is the code that i am using for image crop.
i am successfully cropping image using this code.
Can you tell me how to save this generated “sprite” to the persistent data path ??

i can save texture but cant save sprite ?
is there any way to do this

thank you

using UnityEngine;
using System.Collections;

public class CropSprite : MonoBehaviour
{
public GameObject spriteToCrop;

private Vector3 startPoint, endPoint;
private bool isMousePressed;
private LineRenderer leftLine, rightLine, topLine, bottomLine;

void Start () 
{
	isMousePressed = false;
	leftLine = createAndGetLine("LeftLine");
	rightLine = createAndGetLine("RightLine");
	topLine = createAndGetLine("TopLine");
	bottomLine = createAndGetLine("BottomLine");
}
private LineRenderer createAndGetLine (string lineName)
{
	GameObject lineObject = new GameObject(lineName);
	LineRenderer line = lineObject.AddComponent<LineRenderer>();
	line.SetWidth(0.03f,0.03f);
	line.SetVertexCount(2);
	return line;
}
void Update () 
{
	if(Input.GetMouseButtonDown(0) && isSpriteTouched(spriteToCrop))
	{
		isMousePressed = true;
		startPoint = Camera.main.ScreenToWorldPoint(Input.mousePosition);
	}
	else if(Input.GetMouseButtonUp(0))
	{
		if(isMousePressed)
			cropSprite();	
		isMousePressed = false;
	}
	if(isMousePressed && isSpriteTouched(spriteToCrop))
	{
		endPoint = Camera.main.ScreenToWorldPoint(Input.mousePosition);
		drawRectangle();
	}
}
private void drawRectangle()
{
	leftLine.SetPosition(0, new Vector3(startPoint.x, endPoint.y, 0));
	leftLine.SetPosition(1, new Vector3(startPoint.x, startPoint.y, 0));

	rightLine.SetPosition(0, new Vector3(endPoint.x, endPoint.y, 0));
	rightLine.SetPosition(1, new Vector3(endPoint.x, startPoint.y, 0));

	topLine.SetPosition(0, new Vector3(startPoint.x, startPoint.y, 0));
	topLine.SetPosition(1, new Vector3(endPoint.x, startPoint.y, 0));

	bottomLine.SetPosition(0, new Vector3(startPoint.x, endPoint.y, 0));
	bottomLine.SetPosition(1, new Vector3(endPoint.x, endPoint.y, 0));
}
private void cropSprite()
{
	Vector3 topLeftPoint = startPoint, bottomRightPoint=endPoint;
	if((startPoint.x > endPoint.x))
	{
		topLeftPoint = endPoint;
		bottomRightPoint = startPoint;
	}
	if(bottomRightPoint.y > topLeftPoint.y)
	{
		float y = topLeftPoint.y;
		topLeftPoint.y = bottomRightPoint.y;
		bottomRightPoint.y = y;
	}

	SpriteRenderer spriteRenderer = spriteToCrop.GetComponent<SpriteRenderer>();
	Sprite spriteToCropSprite = spriteRenderer.sprite;
	Texture2D spriteTexture = spriteToCropSprite.texture;
	Rect spriteRect = spriteToCrop.GetComponent<SpriteRenderer>().sprite.textureRect;

	int pixelsToUnits = 100; // It's PixelsToUnits of sprite which would be cropped

	GameObject croppedSpriteObj = new GameObject("CroppedSprite");
	Rect croppedSpriteRect = spriteRect;
	croppedSpriteRect.width = (Mathf.Abs(bottomRightPoint.x - topLeftPoint.x)*pixelsToUnits)* (1/spriteToCrop.transform.localScale.x);
	croppedSpriteRect.x = (Mathf.Abs(topLeftPoint.x - (spriteRenderer.bounds.center.x-spriteRenderer.bounds.size.x/2)) *pixelsToUnits)* (1/spriteToCrop.transform.localScale.x);
	croppedSpriteRect.height = (Mathf.Abs(bottomRightPoint.y - topLeftPoint.y)*pixelsToUnits)* (1/spriteToCrop.transform.localScale.y);
	croppedSpriteRect.y = ((topLeftPoint.y - (spriteRenderer.bounds.center.y - spriteRenderer.bounds.size.y/2))*(1/spriteToCrop.transform.localScale.y))* pixelsToUnits - croppedSpriteRect.height;//*(spriteToCrop.transform.localScale.y);
	Sprite croppedSprite = Sprite.Create(spriteTexture, croppedSpriteRect, new Vector2(0,1), pixelsToUnits);
	SpriteRenderer cropSpriteRenderer = croppedSpriteObj.AddComponent<SpriteRenderer>();	
	cropSpriteRenderer.sprite = croppedSprite;
	topLeftPoint.z = -1;
	croppedSpriteObj.transform.position = topLeftPoint;
	croppedSpriteObj.transform.parent = spriteToCrop.transform.parent;
	croppedSpriteObj.transform.localScale = spriteToCrop.transform.localScale;
	Destroy(spriteToCrop);
}

private bool isSpriteTouched(GameObject sprite)
{
	Vector3 posFor2D = Camera.main.ScreenToWorldPoint(Input.mousePosition);
	RaycastHit2D hit2D = Physics2D.Raycast(posFor2D, Vector2.zero);
	if (hit2D != null && hit2D.collider != null)
	{
		if(hit2D.collider.name.Equals(sprite.name))
			return true;
	}
	return false;
}

}