自动循环滚动,图片尺寸自适应 节点结构如下
ScrollView脚本
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using DG.Tweening;
public class ScrollViewScript : MonoBehaviour
{
public Transform Content;
public List Ctts = new List();
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();
//父物体尺寸
parentSize = self.parent.GetComponent().rect.size;
ReferHeight = parentSize.y - FrameThickness;
ReferWidth = parentSize.x - FrameThickness;
self.GetComponent().SetNativeSize();
olSize = self.sizeDelta;
al = olSize.x / olSize.y;
//决定以宽或高为标准
if (olSize.x
关注
打赏
最近更新
- 深拷贝和浅拷贝的区别(重点)
- 【Vue】走进Vue框架世界
- 【云服务器】项目部署—搭建网站—vue电商后台管理系统
- 【React介绍】 一文带你深入React
- 【React】React组件实例的三大属性之state,props,refs(你学废了吗)
- 【脚手架VueCLI】从零开始,创建一个VUE项目
- 【React】深入理解React组件生命周期----图文详解(含代码)
- 【React】DOM的Diffing算法是什么?以及DOM中key的作用----经典面试题
- 【React】1_使用React脚手架创建项目步骤--------详解(含项目结构说明)
- 【React】2_如何使用react脚手架写一个简单的页面?