using UnityEngine;
using UnityEngine.UI;
public class RawImagesExtend : MonoBehaviour
{
[Header(“资源加载方式”)]
public SourceMode sourceMode;
public enum SourceMode
{
Resources_loading,
StreamAssets_loading,
Absolute_loading
}
[Header("写入图片名称")]
public string rawImageSource;
[Header("控制图片显示样式")]
public DisplayMode displayMode;
public static RawImagesExtend instance;
public enum DisplayMode
{
//原始大小
OriginalSize,
//图片拉伸铺满
Bespread,
//按宽比例
ProportionWidth,
//按高比例
ProportionHeight
}
void Style()
{
//原始大小
if (displayMode == DisplayMode.OriginalSize)
{
this.GetComponent<RawImage>().SetNativeSize();
}
else if (displayMode == DisplayMode.Bespread)
{
//平铺
gameObject.GetComponent<RawImage>().rectTransform.sizeDelta = new Vector2(gameObject.GetComponent<RectTransform>().rect.width, gameObject.GetComponent<RectTransform>().rect.height);
}
else if (displayMode == DisplayMode.ProportionWidth)
{
//按宽比例
gameObject.GetComponent<RawImage>().rectTransform.sizeDelta = new Vector2(gameObject.GetComponent<RectTransform>().rect.width, gameObject.GetComponent<RectTransform>().rect.width / gameObject.GetComponent<RawImage>().texture.width * gameObject.GetComponent<RawImage>().texture.height);
}
else if (displayMode == DisplayMode.ProportionHeight)
{
//按高比例
gameObject.GetComponent<RawImage>().rectTransform.sizeDelta = new Vector2(gameObject.GetComponent<RectTransform>().rect.height / gameObject.GetComponent<RawImage>().texture.height * gameObject.GetComponent<RawImage>().texture.width, gameObject.GetComponent<RectTransform>().rect.height);
}
}
void Awake()
{
instance = this;
}
public void RawImgExtend()
{
//Resources文件读取
if (sourceMode == SourceMode.Resources_loading)
{
GetComponent<RawImage>().texture = Resources.Load(rawImageSource) as Texture;
Style();
}
//StreamAssets文件读取
else if (sourceMode == SourceMode.StreamAssets_loading)
{
ResourceMgrImg.instance.LoadImageData(rawImageSource, _textre =>
{
Texture ass = _textre as Texture;
this.GetComponent<RawImage>().texture = ass;
Style();
});
}
//绝对路径读取
else if (sourceMode == SourceMode.Absolute_loading)
{
StartCoroutine(ResourceMgrImg.instance.LoadWWWTexture(rawImageSource, _textre =>
{
GetComponent<RawImage>().texture = _textre;
Style();
}));
}
}
}