栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 游戏开发 > 其他

unity 图片轮播,左右可切换

其他 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

unity 图片轮播,左右可切换

 

using DG.Tweening;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PictrueMove : MonoBehaviour
{
    public static PictrueMove instance;

    ///


    /// 每一个初始的状态
    ///

    private List item = new List { };

    ///


    /// 判断动画运动期间,不允许再点击
    ///

    private bool isPlay = true;

    private float timer = 0.5f;

    ///


    /// 实例化的物体
    ///

    public GameObject prefabs;

    void Start()
    {
        instance = this;

        StartGame();
    }

    ///


    /// 下一页
    ///

    public void NextPos()
    {
        if (isPlay == false) return;
        isPlay = false;
        StartCoroutine(animPlay());
        for (int i = 0; i < item.Count; i++)
        {

            if (i > 0)
            {
                item[i].DOLocalMove(item[i - 1].localPosition, timer);
                item[i].DOScale(item[i - 1].localScale, timer);
            }
            else
            {
                item[i].DOLocalMove(item[item.Count - 1].localPosition, timer).OnComplete(() => { });
                item[i].DOScale(item[item.Count - 1].localScale, timer);

            }

            int number = item[i].GetComponent().index;

            if (number == 0)
            {
                item[i].GetComponent().index = item.Count - 1;

            }
            else
            {
                item[i].GetComponent().index = number - 1;
                if (number == 3)
                {
                    item[i].SetSiblingIndex(4);
                }
            }

            StartCoroutine(waitSecend(item[i]));


        }
    }


    ///


    /// 上一页
    ///

    public void LastPos()
    {
    
        if (isPlay == false) return;
        isPlay = false;
        StartCoroutine(animPlay());
        for (int i = 0; i < item.Count; i++)
        {

            if (i             {
                item[i].DOLocalMove(item[i + 1].localPosition, timer);
                item[i].DOScale(item[i + 1].localScale, timer);
            }
            else
            {
                item[i].DOLocalMove(item[0].localPosition, timer).OnComplete(() => { });
                item[i].DOScale(item[0].localScale, timer);

            }

            int number = item[i].GetComponent().index;

            if (number == item.Count - 1)
            {
                item[i].GetComponent().index = 0;

            }
            else
            {
                item[i].GetComponent().index = number + 1;
                if (number == 1)
                {
                    item[i].SetSiblingIndex(4);
                }
            }

            Cengji(item[i]);

            if (item[i].GetComponent().index == 0)
            {
                FindPictrue.instance.Find(false,item[i]);
            }
        }

    }

    ///


    /// 位置初始化
    ///

    private void StartGame()
    {
        int i = 0;

        foreach (Transform go in transform)
        {
            item.Add(go);
            go.GetComponent().index = i;
            i++;

        }

        for (int j = 0; j < transform.childCount; j++)
        {
            Cengji(item[j]);
        }


    }


    ///


    /// UI显示优先级
    ///

    /// 物体
    private void Cengji(Transform go)
    {
        int number = go.GetComponent().index;

        switch (number)
        {
            case 0:
                go.SetSiblingIndex(0);
                break;
            case 1:
                go.SetSiblingIndex(3);
                break;
            case 2:
                go.SetSiblingIndex(4);
                break;
            case 3:
                go.SetSiblingIndex(2);
                break;
            case 4:
                go.SetSiblingIndex(1);
                break;
        }

    }

    IEnumerator waitSecend(Transform go)
    {
        yield return new WaitForSeconds(timer/2);
        Cengji(go);
        if(go.GetComponent().index== item.Count - 1)
        {
            FindPictrue.instance.Find(true,go);
        }
       

    }

    IEnumerator animPlay()
    {
        yield return new WaitForSeconds(timer);
        isPlay = true;
    }
}

代码挂在他的上面

 找到图片代码

using DG.Tweening;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.UI;

public class FindPictrue : MonoBehaviour
{
    public static FindPictrue instance;

    ///


    /// 保底图片,库里没有文件的时候,作为显示的图片
    ///

    public Texture2D baodi;
    ///
    /// 图片存储位置,项目下的文件夹
    ///

    private string path = Directory.GetCurrentDirectory() + @"/Picture";
    public static string[] dirs;

    ///


    /// 实例化图片索引
    ///

    public static int ImageNumber = 0;
    public bool isMove = true;
    public static int CountNumber = 0;
    ///
    /// 轮播物体
    ///

    public Transform item;

    ///


    /// 放大的图片
    ///

    public Image Da;
    ///
    /// 放大物体整体
    ///

    public RawImage fangda;

    public bool isDa = false;

    private void Awake()
    {
        instance = this;
        dirs = Directory.GetFiles(path, "*.png");
        Debug.Log("长度:"+dirs.Length);
    }


    void Start()
    {
        StartGame();
    }


    void Update()
    {

    }


    private void StartGame()
    {
        for (int i = 0; i < item.childCount; i++)
        {
            if (dirs.Length == 0)
            {
                item.GetChild(i).GetComponent().xian(baodi);
            }
            else
            {
                StartCoroutine(GetTexture(dirs[ImageNumber], item.GetChild(i)));
                AddSuoShow();
            }

        }

    }

    ///


    /// 图片数量索引循环
    ///

    public void AddSuoShow()
    {
        ImageNumber++;
        //Debug.Log("那个:" + ImageNumber);
        if (ImageNumber >= dirs.Length)
        {
            ImageNumber = 0;
        }
    }

    public void JianSuoShow()
    {
        ImageNumber--;
        //Debug.Log("那个:" + ImageNumber);
        if (ImageNumber <= 0)
        {
            ImageNumber = dirs.Length-1;
        }
    }

    ///


    /// 异步加载图片
    ///

    /// 地址
    ///
    IEnumerator GetTexture(string file, Transform 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.GetComponent().xian(tx);
            }
        }

    }

    ///


    /// 接收到的点击按钮信息
    ///

    /// 按钮参数
    public void ReciveName(Texture2D tx)
    {
        isDa = true;
        Da.gameObject.SetActive(true);
        Da.color = new Color(0, 0, 0, 0);
        fangda.transform.localScale = new Vector3(0.5f, 0.5f, 0.5f);
        fangda.transform.DOScale(new Vector3(0.9f, 0.9f, 0.9f), 1f).SetEase(Ease.InOutQuart);
        Da.DOColor(new Color(0, 0, 0, 0.4f), 1f);
        fangda.texture = tx;
        MoveNumber.instance.StopnextPictru();
    }

    ///


    /// 关闭放大图片
    ///

    public void DaQuit()
    {
        isDa = false;
        fangda.transform.DOScale(new Vector3(0.5f, 0.5f, 0.5f), 1f).SetEase(Ease.InExpo).OnComplete(() =>
         {
             Da.gameObject.SetActive(false);
             PictrueAnim.isStop = false;
         });
        Da.DOColor(new Color(0, 0, 0, 0f), 1f).SetEase(Ease.InExpo);

    }


    ///


    /// 找到物体
    ///

    /// 接受信息的物体
    public void Find(bool addNunmber  ,Transform go)
    {
        if (dirs.Length == 0)
        {
            go.GetComponent().xian(baodi);
        }
        else
        {

            StartCoroutine(GetTexture(dirs[ImageNumber], go));
            if (addNunmber)
            {
                AddSuoShow();
            }
            else
            {
                JianSuoShow();
            }
        }
    }

}

 图片可点击放大

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/901268.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号