前提:Image源文件必须存放在自行创建的文件夹[Resources]中
方式一:typeof 类型 Resources.Load()动态更换Image
Image img;
//image路径
string path = "Images/Item/img";
//参数为资源路径和资源类型
Sprite sprite = Resources.Load(path,typeof(Sprite)) as Sprite;
//动态更换image
Img.sprite = sprite;
方式二:泛型
这里的icon_{数值:00}实际上是图片的名称,只不过用的是正则的方式替换了
Sprite sprite = Resources.Load<Sprite>($"Images/icon_{数值:00}");
using UnityEngine;
using UnityEngine.UI;
public enum AnchorPresets
{
TopLeft,
TopCenter,
TopRight,
MiddleLeft,
MiddleCenter,
MiddleRight,
BottomLeft,
BottonCenter,
BottomRight,
BottomStretch,
VertStretchLeft,
VertStretchRight,
VertStretchCenter,
HorStretchTop,
HorStretchMiddle,
HorStretchBottom,
StretchAll
}
public enum PivotPresets
{
TopLeft,
TopCenter,
TopRight,
MiddleLeft,
MiddleCenter,
MiddleRight,
BottomLeft,
BottomCenter,
BottomRight,
}
public enum UILocation
{
Left,
Center,
Right
}
public static class UIUtil
{
///
/// 自适应Image,不考虑高度,如果图片比父物体要宽,图片将会自适应
///
///
/// 是否进行高度自适应,有滑动条可忽略
/// 缩放倍数
public static void AdaptiveUIImage( this Image mImage, bool mIsAdaptHeight = false, float mSize = 0.95f)
{
if (mImage.type != Image.Type.Simple)
return;
mImage.SetNativeSize ();
if (mImage.rectTransform.sizeDelta.x > mImage.rectTransform.parent.GetComponent<RectTransform> ().sizeDelta.x)
{
float w = mImage.rectTransform.parent.GetComponent<RectTransform> ().sizeDelta.x / mImage.rectTransform.sizeDelta.x;
//Debug.Log ("图片宽:" + f + "%");
mImage.rectTransform.sizeDelta = new Vector2 (mImage.rectTransform.sizeDelta.x * w, mImage.rectTransform.sizeDelta.y * w) * mSize;
}
if (mIsAdaptHeight)
{
if (mImage.rectTransform.sizeDelta.y > mImage.rectTransform.parent.GetComponent<RectTransform> ().sizeDelta.y)
{
float h = mImage.rectTransform.parent.GetComponent<RectTransform> ().sizeDelta.y / mImage.rectTransform.sizeDelta.y;
//Debug.Log ("图片宽:" + f + "%");
mImage.rectTransform.sizeDelta = new Vector2 (mImage.rectTransform.sizeDelta.x * h, mImage.rectTransform.sizeDelta.y * h) * mSize;
}
}
}
///
/// 自适应Text,不考虑宽度(宽度可自定义),如果txt数量过多导致显示不出来下一行,可自适应高度
///
/// text
public static void AdaptiveUIText( this Text mText, bool mIsAdaptWide = false, float mSize = 0.95f )
{
if (mIsAdaptWide)
{
mText.rectTransform.sizeDelta = new Vector2 (mText.transform.parent.GetComponent<RectTransform>().sizeDelta.x* mSize, mText.rectTransform.sizeDelta.y);
}
mText.rectTransform.sizeDelta = new Vector2 (mText.rectTransform.sizeDelta.x, mText.preferredHeight);
}
///
/// UI自适应(子物体锚点不允许为Stretch模式)
///
/// 父物体
/// 自适应位置
/// 间隔距离
/// 父物体是否跟随子物体调整大小
public static void AdaptiveUI( this RectTransform mUIParent, UILocation mUILocation = UILocation.Center, float mGap = 10f, bool mAlterUIParent = true )
{
#region 锚点固定左上角
foreach (RectTransform item in mUIParent)
{
item.SetAnchor (AnchorPresets.TopLeft);
}
#endregion
RectTransform UIItem = null;
foreach (Transform item in mUIParent.transform)
{
if (!item.gameObject.activeSelf)
continue;
#region 自适应Text与Image
Image image = item.GetComponent<Image> ();
Text text = item.GetComponent<Text> ();
if (image)
AdaptiveUIImage (image);
if (text)
AdaptiveUIText (text,true);
#endregion
if (UIItem == null)
{
UIItem = item.GetComponent<RectTransform> ();
UIItem.anchoredPosition = new Vector2 (UIItem.anchoredPosition.x, ( UIItem.sizeDelta.y / -2 ));
}
else
{
float itemx = item.GetComponent<RectTransform> ().anchoredPosition.x;
float itemy = UIItem.anchoredPosition.y - ( item.GetComponent<RectTransform> ().sizeDelta.y / 2 ) - ( UIItem.sizeDelta.y / 2 );
item.GetComponent<RectTransform> ().anchoredPosition = new Vector2 (itemx, itemy);
UIItem = item.GetComponent<RectTransform> ();
}
SetUILocation (mUIParent, item.GetComponent<RectTransform> (), mUILocation, mGap);
}
//设置父物体的长度
if (mAlterUIParent)
{
float mUIParent_Y = ( UIItem.anchoredPosition.y * -1 ) + ( UIItem.sizeDelta.y / 2 );
mUIParent.sizeDelta = new Vector2 (mUIParent.sizeDelta.x, mUIParent_Y + mGap);
//mUIParent.SetPivot (PivotPresets.TopCenter);
//mUIParent.SetAnchor ( AnchorPresets.TopLeft);
}
}
static void SetUILocation( RectTransform mUIParent, RectTransform mTaggetUI, UILocation mUILocation, float mGap )
{
float mTaggetUI_X = 0;
float mTaggetUI_Y = mTaggetUI.anchoredPosition.y - mGap;
switch (mUILocation)
{
case UILocation.Left:
mTaggetUI_X = mTaggetUI.sizeDelta.x / 2 + mGap;
break;
case UILocation.Center:
mTaggetUI_X = mUIParent.sizeDelta.x / 2;
break;
case UILocation.Right:
mTaggetUI_X = mUIParent.sizeDelta.x - ( mTaggetUI.sizeDelta.x / 2 ) - mGap;
break;
}
mTaggetUI.anchoredPosition = new Vector2 (mTaggetUI_X, mTaggetUI_Y);
}
public static void SetAnchor( this RectTransform source, AnchorPresets allign, int offsetX = 0, int offsetY = 0 )
{
source.anchoredPosition = new Vector3 (offsetX, offsetY, 0);
switch (allign)
{
case ( AnchorPresets.TopLeft ):
{
source.anchorMin = new Vector2 (0, 1);
source.anchorMax = new Vector2 (0, 1);
break;
}
case ( AnchorPresets.TopCenter ):
{
source.anchorMin = new Vector2 (0.5f, 1);
source.anchorMax = new Vector2 (0.5f, 1);
break;
}
case ( AnchorPresets.TopRight ):
{
source.anchorMin = new Vector2 (1, 1);
source.anchorMax = new Vector2 (1, 1);
break;
}
case ( AnchorPresets.MiddleLeft ):
{
source.anchorMin = new Vector2 (0, 0.5f);
source.anchorMax = new Vector2 (0, 0.5f);
break;
}
case ( AnchorPresets.MiddleCenter ):
{
source.anchorMin = new Vector2 (0.5f, 0.5f);
source.anchorMax = new Vector2 (0.5f, 0.5f);
break;
}
case ( AnchorPresets.MiddleRight ):
{
source.anchorMin = new Vector2 (1, 0.5f);
source.anchorMax = new Vector2 (1, 0.5f);
break;
}
case ( AnchorPresets.BottomLeft ):
{
source.anchorMin = new Vector2 (0, 0);
source.anchorMax = new Vector2 (0, 0);
break;
}
case ( AnchorPresets.BottonCenter ):
{
source.anchorMin = new Vector2 (0.5f, 0);
source.anchorMax = new Vector2 (0.5f, 0);
break;
}
case ( AnchorPresets.BottomRight ):
{
source.anchorMin = new Vector2 (1, 0);
source.anchorMax = new Vector2 (1, 0);
break;
}
case ( AnchorPresets.HorStretchTop ):
{
source.anchorMin = new Vector2 (0, 1);
source.anchorMax = new Vector2 (1, 1);
break;
}
case ( AnchorPresets.HorStretchMiddle ):
{
source.anchorMin = new Vector2 (0, 0.5f);
source.anchorMax = new Vector2 (1, 0.5f);
break;
}
case ( AnchorPresets.HorStretchBottom ):
{
source.anchorMin = new Vector2 (0, 0);
source.anchorMax = new Vector2 (1, 0);
break;
}
case ( AnchorPresets.VertStretchLeft ):
{
source.anchorMin = new Vector2 (0, 0);
source.anchorMax = new Vector2 (0, 1);
break;
}
case ( AnchorPresets.VertStretchCenter ):
{
source.anchorMin = new Vector2 (0.5f, 0);
source.anchorMax = new Vector2 (0.5f, 1);
break;
}
case ( AnchorPresets.VertStretchRight ):
{
source.anchorMin = new Vector2 (1, 0);
source.anchorMax = new Vector2 (1, 1);
break;
}
case ( AnchorPresets.StretchAll ):
{
source.anchorMin = new Vector2 (0, 0);
source.anchorMax = new Vector2 (1, 1);
break;
}
}
}
public static void SetPivot( this RectTransform source, PivotPresets preset )
{
switch (preset)
{
case ( PivotPresets.TopLeft ):
{
source.pivot = new Vector2 (0, 1);
break;
}
case ( PivotPresets.TopCenter ):
{
source.pivot = new Vector2 (0.5f, 1);
break;
}
case ( PivotPresets.TopRight ):
{
source.pivot = new Vector2 (1, 1);
break;
}
case ( PivotPresets.MiddleLeft ):
{
source.pivot = new Vector2 (0, 0.5f);
break;
}
case ( PivotPresets.MiddleCenter ):
{
source.pivot = new Vector2 (0.5f, 0.5f);
break;
}
case ( PivotPresets.MiddleRight ):
{
source.pivot = new Vector2 (1, 0.5f);
break;
}
case ( PivotPresets.BottomLeft ):
{
source.pivot = new Vector2 (0, 0);
break;
}
case ( PivotPresets.BottomCenter ):
{
source.pivot = new Vector2 (0.5f, 0);
break;
}
case ( PivotPresets.BottomRight ):
{
source.pivot = new Vector2 (1, 0);
break;
}
}
}
}
public class UIDemo : MonoBehaviour
{
public RectTransform UIParent;
public Image UIImage;
public Text UIText;
void Start()
{
UIParent.AdaptiveUI ();
UIImage.AdaptiveUIImage ();
UIText.AdaptiveUIText ();
}
}