赞
踩
mask功能可以参考我这篇文章使用Mask遮罩功能实现头像
在场景中调整位置。
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using DG.Tweening;
- using System.IO;
- using UnityEngine.UI;
-
- public class Test : MonoBehaviour
- {
- public GameObject go;
- public GameObject prefabToInstantiate;
-
- int n = 0;
- int index = 0;
- int num = 0;
- Transform parentTransform;
- GameObject newObject;
-
-
-
- // Start is called before the first frame update
- void Start()
- {
- parentTransform = go.transform; // 替换 go为实际的父级对象
-
- LoadLocalTextures();
- InvokeRepeating("lunbo", 1, 4);
- }
-
-
- public void LoadLocalTextures()
- {
-
- string imgType = "*.BMP|*.JPG|*.PNG"; //需要加载的图片类型后缀
- string[] imgTypes = imgType.Split('|');
- for (int i = 0; i < imgTypes.Length; i++)
- {
- string[] s2 = Directory.GetFiles(Application.dataPath + "/Resources/", imgTypes[i]); //Application.dataPath + "/Resources/"为图片所在路径
- num += s2.Length ;
- for (int ji = 0; ji < s2.Length; ji++)
- {
- if (++n <= num)
- {
- newObject = Instantiate(prefabToInstantiate);
- newObject.transform.SetParent(parentTransform);
- newObject.transform.localPosition = new Vector3((500f*n-250), 0f, 0f);
- }
-
- //文件流读取图片
- FileStream fileStream = new FileStream(s2[ji], FileMode.Open, FileAccess.Read);
-
- byte[] bytes = new byte[fileStream.Length];
-
- fileStream.Read(bytes, 0, (int)fileStream.Length);
-
-
- fileStream.Close();
-
- fileStream.Dispose();
- fileStream = null;
- //设置图片大小
- int width = 500;
- int height = 360;
- //创建texture2D
- Texture2D texture = new Texture2D(width, height);
- //加载图片流
- texture.LoadImage(bytes);
- //创建sprite并赋值
- Sprite sprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), new Vector2(0.5f, 0.5f));
- //ga.GetComponent<Image>().sprite = sprite;
- newObject.GetComponent<Image>().sprite = sprite;
- }
-
- }
- }
-
- void lunbo()
- {
- if (index == (num-1))
- {
- index = 0;
- //go.transform.localPosition = new Vector3(-250, 0, 0);
- go.transform.DOLocalMoveX(-250, 1);
- }
- else
- {
- index++;
- go.transform.DOLocalMoveX(-250 - 500 * index, 1);
- }
- //Resources.Load<Image>
- }
- }
这里轮播的逻辑类似我之前的文章Unity实现图片轮播 ,只不过加入了自动加载的功能。整体逻辑是先加载图片再轮播。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。