在加载图片又是需要去动态获取图片的一部分,或者要识别一张图片的二维码,但是二维码比较小一整张识别容易失败,就可以对图片进行分区识别
public void TailorTexture()
{
Texture2D tx = Resources.Load<Texture2D>("Test");
Texture2D tx2d = new Texture2D(tx.width,tx.height / 2,TextureFormat.RGBA32,false);
tx2d.SetPixels(tx.GetPixels(0,0, tx.width, (int)(tx.height * 0.5f)));
tx2d.Apply();
raw.texture = tx2d;
}
上面一种是对图片的 texture进行裁剪,下面是只获取图片的裁剪的图片,而不修改整张图片的 texture数据
public void TestMethod()
{
raw.texture = Resources.Load("Test") as Texture2D;
image_h = raw.texture.height / 2;
image_w = raw.texture.width ;
Texture2D tx = raw.texture as Texture2D;
iam.sprite = Sprite.Create(tx, new Rect(new Vector2(0,0), new Vector2(raw.texture.width, image_h)),new Vector2(0,1));
}