有关Unity截取图片中心的显示在UI上(Unity 做法)
首先我们需要一张图我这边是在UnityEditor下测试的,移动端测试可用。采用的方式是Sprite的方式
using System.Collections;
using System.Collections.Generic;
using System.Net.NetworkInformation;
using UnityEditor;
using UnityEngine;
using UnityEngine.UI;
public class CodeEditor : MonoBehaviour
{
/// <summary>
/// 取图片的中心区域
/// </summary>
[MenuItem("图片工具/获取中心区域")]
private static void ChangeDrawTexture()
{
var Texture =
AssetDatabase.LoadAssetAtPath<Texture2D>("Assets/Editor/Texture/timg.jpg");
Debug.Log(Texture);
var ShowImage = GameObject.Find("Image").GetComponent<Image>();
ShowImage.sprite = BornSprite(Texture);
}
private static Sprite BornSprite(Texture2D Texture, int Width = 500, int Height = 600)
{
//因为是以左下的00为起点,所以Pos 需要按照原图一半的比例再剪去需要截取大小的一半
Sprite sp = Sprite.Create(Texture,
new Rect(new Vector2(Texture.width / 2 - Width / 2, Texture.height / 2 - Height / 2),
new Vector2(Width, Height)), new Vector2(0.5f, 0.5f));
return sp;
}
}
这一张是原大小的图
我们获取一个500600的图,原图大小是8501134。裁剪过后的图片是这样的
很完美的剔除了边框部分,是以中心点往外扩散的截取方式