新案例位置分享,代码老的,别看了,直接下载案例吧。
链接:https://pan.baidu.com/s/19h0TYLmkcuRGuxwjzx8FpQ
提取码:y1sh
1.主控制代码
自己设置好行数,大小,和初始位置。我一般用下放的组件设置好位置,在取消这个组件。
using DG.Tweening;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.UI;
public class PictrueManager : MonoBehaviour
{
public static PictrueManager Instance;
private Queue<Texture2D> texture2d;
/// <summary>
/// 放大的图片
/// </summary>
public Image Da;
/// <summary>
/// 放大物体整体
/// </summary>
public RawImage fangda;
/// <summary>
/// 图片行数
/// </summary>
private int hang;
/// <summary>
/// 图片列数
/// </summary>
private int lie;
/// <summary>
/// 实例化图片索引
/// </summary>
public static int ImageNumber = 0;
public bool isMove = true;
public static int CountNumber = 0;
/// <summary>
/// 保底图片,库里没有文件的时候,作为显示的图片
/// </summary>
public Texture2D baodi;
/// <summary>
/// 图片存储位置,项目下的文件夹
/// </summary>
private string path = Directory.GetCurrentDirectory() + @"/Picture";
public static string[] dirs;
public static bool isBaodi;
private void Awake()
{
dirs = Directory.GetFiles(path, "*.png");
}
void Start()
{
Instance = this;
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
hang = transform.GetChild(0).childCount;
lie = transform.childCount;
CountNumber = hang * lie;
Da.gameObject.SetActive(false);
AddImage();
}
/// <summary>
/// 找到文件里面的图片位置
/// </summary>
public void FindPictrue()
{
string[] dirs = Directory.GetFiles(path, "*.png");
}
/// <summary>
/// 初始加载图片
/// </summary>
public void AddImage()
{
Debug.Log(path + "图片数量:" + dirs.Length);
for (int i = 0; i < hang; i++)
{
for (int j = 0; j < lie; j++)
{
AddSuoShow();
if (dirs.Length != 0)
{
isBaodi = false;
StartCoroutine(GetTexture(dirs[ImageNumber], i, j));
}
else
{
isBaodi = true;
transform.GetChild(j).GetChild(i).GetComponent<ClickButton>().xian(baodi);
}
}
}
}
/// <summary>
/// 图片数量索引循环
/// </summary>
public void AddSuoShow()
{
ImageNumber++;
if (ImageNumber >= dirs.Length)
{
ImageNumber = 0;
}
}
/// <summary>
/// 接收到的点击按钮信息
/// </summary>
/// <param name="name">按钮参数</param>
public void ReciveName(Texture2D tx)
{
Debug.Log("接收到的信息:");
Da.gameObject.SetActive(true);
Da.color = new Color(0, 0, 0, 0);
fangda.transform.localScale = new Vector3(0, 0, 0);
fangda.transform.DOScale(new Vector3(1, 1, 1), 1f).SetEase(Ease.InOutQuart);
Da.DOColor(new Color(0, 0, 0, 0.4f), 1f);
fangda.texture = tx;
rawImage显示
//RawImage spr = fangda.transform.GetChild(0).GetComponent<RawImage>();
//spr.texture = tx;
300为本实例的大小
//spr.GetComponent<RectTransform>().sizeDelta = new Vector2(imageDaWidth, imageDaWidth * (1.0f * tx.height / tx.width));
}
/// <summary>
/// 关闭放大图片
/// </summary>
public void DaQuit()
{
fangda.transform.DOScale(new Vector3(0, 0, 0), 1f).SetEase(Ease.InExpo).OnComplete(() =>
{
Da.gameObject.SetActive(false);
PictrueAnim.isStop = false;
});
Da.DOColor(new Color(0, 0, 0, 0f), 1f).SetEase(Ease.InExpo);
}
/// <summary>
/// 异步加载图片
/// </summary>
/// <param name="file">地址</param>
/// <returns></returns>
IEnumerator GetTexture(string file,int hang ,int lie)
{
//这里的地址可以填本地文件地址 file://[文件路径]
using (UnityWebRequest www = UnityWebRequestTexture.GetTexture(file))
{
yield return www.SendWebRequest();
if (www.isNetworkError)
{
Debug.Log(www.error);
}
else
{
/*这里把贴图转换的过程放到了后台线程来完成,
并且相比于直接脚本加载图片做了优化,减少了内存分配*/
Texture2D tx = DownloadHandlerTexture.GetContent(www);
transform.GetChild(lie).GetChild(hang).GetComponent<ClickButton>().xian(tx);
}
}
}
}
2.每一行的代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.UI;
public class PictrueAnim : MonoBehaviour
{
//数值存储
private List<float> gamePosY = new List<float> { };
/// <summary>
/// 图片之间的距离
/// </summary>
private float distanceX;
private float lastPosX;
/// <summary>
/// 暂停滚动
/// </summary>
public static bool isStop = true;
public GameObject picture;
public float speedTime = 1f;
private float distanceSpeed;
/// <summary>
/// 每秒计时数据
/// </summary>
private float timer = 0;
/// <summary>
/// 是否更换图片
/// </summary>
public bool isHuan = true;
public void OnEnable()
{
isStop = false;
}
public void Awake()
{
RemberPosY();
OndistanceSpeed();
}
void Update()
{
if (isStop == false)
{
timer += 0.02f;
//每秒钟输出一次
if (timer >= speedTime)
{
timer = 0;
Destroy(transform.GetChild(0).gameObject);
AnimPicture();
}
for (int i = 0; i < transform.childCount; i++)
{
float xx = transform.GetChild(i).localPosition.x + distanceSpeed;
transform.GetChild(i).localPosition = new Vector3(xx, transform.GetChild(0).localPosition.y, 0);
}
}
}
/// <summary>
/// 记录坐标位置
/// </summary>
private void RemberPosY()
{
lastPosX = transform.GetChild(transform.childCount - 1).localPosition.x;
for (int i = 0; i < transform.childCount; i++)
{
gamePosY.Add(transform.GetChild(i).localPosition.x);
}
}
private void OndistanceSpeed()
{
distanceX = transform.GetChild(0).localPosition.x - transform.GetChild(1).localPosition.x;
distanceSpeed = distanceX / (speedTime / 0.02f);
Debug.Log("速度" + this.name + "距离:" + distanceSpeed);
}
/// <summary>
/// 图片滚动动画
/// </summary>
private void AnimPicture()
{
RectTransform xin = (Instantiate(picture) as GameObject).GetComponent<RectTransform>();
xin.transform.SetParent(transform);
float xx = lastPosX;
xin.transform.localScale = new Vector3(1, 1, 1);
xin.transform.localPosition = new Vector3(xx, transform.GetChild(0).localPosition.y, 0);
xin.name = PictrueManager.ImageNumber.ToString();
//显示已存储的数据
if (PictrueManager.dirs.Length == 0)
{
xin.transform.GetComponent<ClickButton>().xian(PictrueManager.Instance.baodi);
}
else
{
StartCoroutine(GetTexture(PictrueManager.dirs[PictrueManager.ImageNumber], xin.gameObject));
}
SendMessageUpwards("AddSuoShow");
}
/// <summary>
/// 接受信息
/// </summary>
public void ReceiveClick(Texture2D tx)
{
Debug.Log("点击图片");
isStop = true;
SendMessageUpwards("ReciveName", tx);
}
/// <summary>
/// 异步加载图片
/// </summary>
/// <param name="file">地址</param>
/// <returns></returns>
IEnumerator GetTexture(string file, GameObject go)
{
//这里的地址可以填本地文件地址 file://[文件路径]
using (UnityWebRequest www = UnityWebRequestTexture.GetTexture(file))
{
yield return www.SendWebRequest();
if (www.isNetworkError)
{
Debug.Log(www.error);
}
else
{
/*这里把贴图转换的过程放到了后台线程来完成,
并且相比于直接脚本加载图片做了优化,减少了内存分配*/
Texture2D tx = DownloadHandlerTexture.GetContent(www);
go.transform.GetComponent<ClickButton>().xian(tx);
}
}
}
}
3.每一个预制体的内容,存储图片数据
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class ClickButton : MonoBehaviour
{
private Texture2D texture2d;
public void Click()
{
SendMessageUpwards("ReceiveClick", texture2d);
}
/// <summary>
/// 数据记录
/// </summary>
public void xian(Texture2D tx)
{
RawImage spr = transform.GetChild(0).GetComponent<RawImage>();
spr.texture = tx;
//300为本实例的大小
//spr.GetComponent<RectTransform>().sizeDelta = new Vector2(300* (1.0f * tx.width / tx.height), 300);
texture2d = tx;
}
private void OnDestroy()
{
if (PictrueManager.isBaodi!=true)
{
Destroy(texture2d);
}
}
}