Unity中调试Scroll View,一个Scroll View可以加载不同的图片

1.所有的图片宽度要相同(最好)

2.锚点设置

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class ScrollImageScale : MonoBehaviour
{

    public Image image;
    public GameObject content;
    private float startX;
    private void Start() {
        startX = image.transform.localPosition.x;
        print(startX);
    }
    private void OnEnable()
    {
        Invoke("InvokeFun", 0.01f);
    }
    void InvokeFun() {

        image.SetNativeSize();
        content.transform.localPosition = new Vector3(0, 0, 0);
        float imageHight = image.GetComponent<RectTransform>().sizeDelta.y;
          image.transform.localPosition = new Vector3(startX, -imageHight/2-5, 0);



        RectTransform rectFransF = content.transform.GetComponent<RectTransform>();
        rectFransF.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, (imageHight+10f));

    }

}

 

posted on 2024-08-01 09:35  zqiang0803  阅读(9)  评论(0编辑  收藏  举报

导航