当前位置:   article > 正文

Unity 动态更换Image图片 && UI自适应_unityimage动态更改图片

unityimage动态更改图片

一、动态更换Image图片

前提: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;
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

方式二:泛型
这里的icon_{数值:00}实际上是图片的名称,只不过用的是正则的方式替换了

Sprite sprite = Resources.Load<Sprite>($"Images/icon_{数值:00}");
  • 1

二、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;
                }
        }
    }
}


  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
  • 221
  • 222
  • 223
  • 224
  • 225
  • 226
  • 227
  • 228
  • 229
  • 230
  • 231
  • 232
  • 233
  • 234
  • 235
  • 236
  • 237
  • 238
  • 239
  • 240
  • 241
  • 242
  • 243
  • 244
  • 245
  • 246
  • 247
  • 248
  • 249
  • 250
  • 251
  • 252
  • 253
  • 254
  • 255
  • 256
  • 257
  • 258
  • 259
  • 260
  • 261
  • 262
  • 263
  • 264
  • 265
  • 266
  • 267
  • 268
  • 269
  • 270
  • 271
  • 272
  • 273
  • 274
  • 275
  • 276
  • 277
  • 278
  • 279
  • 280
  • 281
  • 282
  • 283
  • 284
  • 285
  • 286
  • 287
  • 288
  • 289
  • 290
  • 291
  • 292
  • 293
  • 294
  • 295
  • 296
  • 297
  • 298
  • 299
  • 300
  • 301
  • 302
  • 303
  • 304
  • 305
  • 306
  • 307
  • 308
  • 309
  • 310
  • 311
  • 312
  • 313
  • 314
  • 315
  • 316
  • 317
  • 318
  • 319
  • 320
  • 321
  • 322
  • 323
  • 324
  • 325
  • 326
  • 327
  • 328
  • 329
  • 330
  • 331

UI自适应使用方式

public class UIDemo : MonoBehaviour
{
    public RectTransform UIParent;
    public Image UIImage;
    public Text UIText;
    void Start()
    {
        UIParent.AdaptiveUI ();
        UIImage.AdaptiveUIImage ();
        UIText.AdaptiveUIText ();
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/羊村懒王/article/detail/566299
推荐阅读
相关标签
  

闽ICP备14008679号