unity加载本地图片轮播

using System.IO;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class Picplay : MonoBehaviour
{

    Image m_image;
    int picindex;//图片名称
    string filepath;//图片所在文件夹
    public float loadspeed;//每帧加载速度 
    bool bload;
    bool bsub;

    // Use this for initialization
    void Start()
    {
        m_image = GameObject.Find("Image").GetComponent<Image>();
        filepath = Application.streamingAssetsPath;

        m_image.fillAmount = 0;
        bload = false;
        bsub = false;

        Picplaycontrol(0);
    }

    // Update is called once per frame
    void Update()
    {
        if (bload)
        {
            m_image.fillAmount += loadspeed;
            if (m_image.fillAmount == 1) bload = false;
        }
        if (bsub)
        {
            m_image.fillAmount -= loadspeed;
            if (m_image.fillAmount == 0) bsub = false;
        }
    }


	/// <summary>
	/// 调用轮播
	/// </summary>
	/// <param name="i">图片名称</param>
    public void Picplaycontrol(int i)
    {
        picindex = i;
        print(i);
        InvokeRepeating("Loadpic", 0, 5f);
    }
    void Loadpic()
    {

        string[] dirs = System.IO.Directory.GetFileSystemEntries(filepath);
        if (picindex > dirs.Length)
        {
            picindex = 0;
        }

        string picpath = filepath + "/" + picindex.ToString() + ".png";
        if (File.Exists(picpath))
        {
            print(2);
            byte[] bt = File.ReadAllBytes(picpath);
            Texture2D t2d = new Texture2D(1920, 1080);
            t2d.LoadImage(bt);
            Sprite sprite = Sprite.Create(t2d, new Rect(0, 0, t2d.width, t2d.height), Vector2.zero);
            m_image.sprite = sprite;
            Invoke("Setboolload", 0.2f);//因为加载图片需要时间
            Invoke("Setboolsub", 3f);
            picindex++;
        }

    }
    public void Setboolload()
    {
        bload = true;
    }
    public void Setboolsub()
    {
        bsub = true;
    }

}

 

  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 8
    评论
好的,我理解了您的问题。首先,我们需要准备好背包的预制体,并将其打包成AssetBundle。在Unity中,可以通过以下步骤来完成: 1. 创建一个空的场景,并在场景中添背包预制体。 2. 在菜单栏中选择 "AssetBundles" -> "Build AssetBundles"。 3. 在弹出的窗口中,选择要打包的平台和输出路径,然后点击 "Build" 按钮。 打包完成后,我们需要在代码中AssetBundle并实例化背包预制体。可以参考以下代码: ```csharp using UnityEngine; using System.Collections; public class LoadAssetBundle : MonoBehaviour { public string assetBundleUrl; public string assetName; IEnumerator Start () { // AssetBundle using (WWW www = new WWW(assetBundleUrl)) { yield return www; AssetBundle bundle = www.assetBundle; // 实例化背包预制体 GameObject prefab = bundle.LoadAsset<GameObject>(assetName); Instantiate(prefab); // 卸AssetBundle bundle.Unload(false); } } } ``` 在上述代码中,`assetBundleUrl` 是AssetBundle的路径,`assetName` 是要的预制体名称。完成后,我们需要通过 `Instantiate` 方法创建背包的实例。 接下来,我们需要编写一个 `UImanager` 脚本来控制背包的打开和关闭。可以参考以下代码: ```csharp using UnityEngine; using UnityEngine.UI; public class UImanager : MonoBehaviour { public GameObject bag; public void ToggleBag () { bag.SetActive(!bag.activeSelf); } } ``` 在上述代码中,`bag` 是背包的实例,`ToggleBag` 方法用于切换背包的状态。在UI中,我们可以通过添一个按钮,并将其绑定到 `ToggleBag` 方法来实现打开和关闭背包的功能。 希望以上内容能够对您有所帮助。如有任何问题,请随时与我联系。
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值