Source Image:显示的图片,需要设置成精灵sprite。导入的图片格式必须设置成Sprite(2D and UI)
Color:颜色
Material:材质
Raycast Target:是否接受射线检测,当该项为false时,消息会透传
Raycast Padding:射线检测的偏移值
Maskable:受不受Mask的影响
ImageType
Simple:默认的显示方式,会按照设置的空间大小缩放图片。
Use Sprite Mesh:勾选会自动生成网格。
Preserve Aspect:保持长宽比,取长宽较小的值缩放。
Sliced:已切片,9宫格切片
Fill Center:填充中心
Pixels Per Unit Multiplier:控制图片的缩放,受精灵,Canvas Scaler和Image这三个值共同影响,公式:Pixels Per Unit=Pixels Per Unit(精灵上设置的值)× Pixels Per Unit Multiplier(Image设置的值)÷Reference Pixels Per Unit(Canvas Scaler设置的值)影响大小
Tiled:当宽度和高度大于图片宽度时,会用多张图片平铺。如果是9宫格的会用中间的图片重叠,中间可以掏空
Filled:已填充
Fill Method:填充方法,和Simple一样用一张图片显示,宽和高大于图片时会拉伸,可以按照不同的方式填充内容
Horizontal:水平
Vertical:垂直
Radial 90:放射状90度
Radial 180:放射状180度
Radial 360:放射状360度
Fill Origin:填充原点
Left:左
Right:右
Bottom:底
Top:顶
Fill Amount:填充量
Clockwise:顺时针
Preserve Aspect:保持长宽比
代码加载图片,分开在不在Resources目录下
不在Resources下:两种方式,路径必须写全路径,后缀不能省略
1:www
IEnumerator WWWLoadImage(Image img, string path)
{
WWW www = new WWW(path);
if (www.isDone)
yield return www;
Texture2D texture = www.texture;
Sprite sp = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), new Vector2(0.5f, 0.5f));
img.sprite = sp;
}
2:io
void IoLoadImage(Image img, string path, int width, int height)
{
using (FileStream fileStream = new FileStream(path, FileMode.Open, FileAccess.Read))
{
fileStream.Seek(0, SeekOrigin.Begin);
byte[] byteArr = new byte[fileStream.Length];
fileStream.Read(byteArr, 0, byteArr.Length);
Texture2D texture = new Texture2D(width, height);
texture.LoadImage(byteArr);
Sprite sp = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), new Vector2(0.5f, 0.5f));
img.sprite = sp;
}
}
在Resources下:路径从Resources开始,不能带后缀
例如“F:\Unitytest\Assets\Resources\Image\123.jpg”,只需要用“Image\123”就可以
如果不是图集可以
void ResouceLoadImage(Image img,string path)
{
Texture2D loadTexture = (Texture2D)Resources.Load(path) as Texture2D;
Sprite sp = Sprite.Create(loadTexture, new Rect(0, 0, loadTexture.width, loadTexture.height), new Vector2(0.5f, 0.5f));
img.sprite = sp;
}
是图集可以
void ResouceLoadImage(Image img,string path)
{
img.sprite = Resources.Load<Sprite>(path);
}
面板上看到的其他属性直接可以修改例如
img.type = Image.Type.Filled;
img.fillAmount = 0.6f;