一、动态更换Image图片
前提:Image源文件必须存放在自行创建的文件夹[Resources]中
方式一:typeof 类型 Resources.Load()动态更换Image文章来源:https://www.toymoban.com/news/detail-509968.html
Image img;
//image路径
string path = "Images/Item/img";
//参数为资源路径和资源类型
Sprite sprite = Resources.Load(path,typeof(Sprite)) as Sprite;
//动态更换image
Img.sprite = sprite;
方式二:泛型
这里的icon_{数值:00}实际上是图片的名称,只不过用的是正则的方式替换了文章来源地址https://www.toymoban.com/news/detail-509968.html
Sprite sprite = Resources.Load<Sprite>($"Images/icon_{数值:00}");
二、UI自适应
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
{
/// <summary>
/// 自适应Image,不考虑高度,如果图片比父物体要宽,图片将会自适应
/// </summary>
/// <param name="mImage"></param>
/// <param name="mIsAdaptHeight">是否进行高度自适应,有滑动条可忽略</param>
/// <param name="mSize">缩放倍数</param>
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;
}
}
}
/// <summary>
/// 自适应Text,不考虑宽度(宽度可自定义),如果txt数量过多导致显示不出来下一行,可自适应高度
/// </summary>
/// <param name="mText">text</param>
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);
}
/// <summary>
/// UI自适应(子物体锚点不允许为Stretch模式)
/// </summary>
/// <param name="mUIParent">父物体</param>
/// <param name="mUILocation">自适应位置</param>
/// <param name="mGap">间隔距离</param>
/// <param name="mAlterUIParent">父物体是否跟随子物体调整大小</param>
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;
}
}
}
}
UI自适应使用方式
public class UIDemo : MonoBehaviour
{
public RectTransform UIParent;
public Image UIImage;
public Text UIText;
void Start()
{
UIParent.AdaptiveUI ();
UIImage.AdaptiveUIImage ();
UIText.AdaptiveUIText ();
}
}
到了这里,关于Unity 动态更换Image图片 && UI自适应的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!