自动循环滚动,图片尺寸自适应
节点结构如下
ScrollView脚本
using System.Collections; using System.Collections.Generic; using UnityEngine; using DG.Tweening; public class ScrollViewScript : MonoBehaviour { public Transform Content; public List<Transform> Ctts = new List<Transform>(); public float MaxTime_ChangeImage; public float time; public Transform ReferencePoint; //public SerialNumber serial; [SerializeField] private int index; public int Index { get { return index; } set { if (value>= Content.childCount) { //print("value>= Content.childCount"); value = 0; } index = value; ChangeImage(index); } } private void Awake() { Content = transform.Find("Viewport/Content"); ReferencePoint = transform.Find("ReferencePoint"); //serial = transform.parent.Find("SerialNumber").GetComponent(); ResetTime(); InitCttSelfIndex(); } private void OnEnable() { ResetAll(); } private void Start() { MaxTime_ChangeImage = Page0Script.Instance.MaxTime_C; ResetTime(); print(Page0Script.Instance.MaxTime_C); } public void InitCttSelfIndex() { foreach (Transform ctt in Content) { Ctts.Add(ctt); } } void FixedUpdate() { if (time > 0) { time -= Time.deltaTime; } else { //切换画面 Index++; ResetTime(); } } public void ResetTime() { time = MaxTime_ChangeImage; } //初始化 private void ResetAll() { //计数器计时器图片定位 index = 0; ResetTime(); StartCoroutine(ResetIndex002()); } //图片切换 public void ChangeImage(int i) { StartCoroutine(ResetIndex(0.2f)); } /// /// 1.把子物体列表中的第一个移至末尾 2.同时当前显示图片会前移一个单位我们需要让content闪现把画面复原 3.然后再进行向左移动的操作 /// /// /// IEnumerator ResetIndex(float t) { if (Ctts[index].GetSiblingIndex() == Ctts.Count-1) { //闪现距离 float D_idx3 = ReferencePoint.position.x - Content.GetChild(Content.childCount-3).position.x; //刷新顺序 Content.GetChild(0).SetSiblingIndex(Ctts.Count - 1); //闪现 float TPx = Content.position.x + D_idx3; Content.position = new Vector3(TPx, Content.position.y, Content.position.z); } yield return new WaitForEndOfFrame(); //用索引找对应的图片,计算距离并位移 float d = ReferencePoint.position.x - Ctts[index].position.x; float TragetPoint = Content.position.x + d; Content.DOMoveX(TragetPoint, t); } //初始化专用 IEnumerator ResetIndex002() { if (Ctts[index].GetSiblingIndex() == Ctts.Count - 1) { //闪现距离 float D_idx3 = ReferencePoint.position.x - Content.GetChild(1).position.x; //刷新顺序 Content.GetChild(0).SetSiblingIndex( Ctts.Count - 1); //闪现 float TPx = Content.position.x + D_idx3; Content.position = new Vector3(TPx, Content.position.y, Content.position.z); } yield return new WaitForEndOfFrame(); //用索引找对应的图片,计算距离并位移 float d = ReferencePoint.position.x - Ctts[index].position.x; float TragetPoint = Content.position.x + d; Content.DOMoveX(TragetPoint, 0F); } }
using System.Collections; using System.Collections.Generic; using UnityEngine; Ctt脚本 public class CttScript : MonoBehaviour public int SelfIndex;
MTexture脚本
using UnityEngine; using System.Collections; using UnityEngine.UI; /// /// 图片参考父物体大小等比例缩放 /// public class X_RectAutoSize : MonoBehaviour { //原始尺寸 private Vector2 olSize; //缩放后的尺寸 private Vector2 size; //原始尺寸宽高比 private float al; private RectTransform self; //是否锁定位置 public bool lockPos = true; internal float ReferHeight; internal float ReferWidth; //父物体尺寸 private Vector2 parentSize; //边框厚度 public float FrameThickness; void Update() { SetWidthHight(); } //设置宽高 public void SetWidthHight() { self = GetComponent<RectTransform>(); //父物体尺寸 parentSize = self.parent.GetComponent<RectTransform>().rect.size; ReferHeight = parentSize.y - FrameThickness; ReferWidth = parentSize.x - FrameThickness; self.GetComponent<Image>().SetNativeSize(); olSize = self.sizeDelta; al = olSize.x / olSize.y; //决定以宽或高为标准 if (olSize.x < olSize.y) size = new Vector2(ReferHeight * al, ReferHeight); else size = new Vector2(ReferWidth, ReferWidth / al); self.sizeDelta = size; if (lockPos) self.anchoredPosition = Vector2.zero; } }
生成图片元素方式
//生成图片,并适应大小 public void InsImage(Sprite sprite) { GameObject TextureBG = Instantiate(Ctt_prefab, TragetParent.transform); GameObject MTexture = Instantiate(Texture_Prefab, TextureBG.transform); MTexture.GetComponent<Image>().sprite = sprite; MTexture.GetComponent<X_RectAutoSize>().SetWidthHight(); }