unity 加载图片,并且图片不在resource、StreamingAssets里面。

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

public  enum UI_name{
    UpBtn,
    DownBtn,
    SubmitBtn
}

public class ExplainFun : MonoBehaviour {

    public Image ShowPic;
    public UI_name UiName;

    public float picTotalNum;


    private float picNum = 5;
    private string filePath;
    private FileInfo[] files;
    void Start () {
        switch (UiName) {
            case UI_name.UpBtn:
                EventTriggerListener.Get(gameObject).onClick = onUpBtn;
                break;
            case UI_name.DownBtn:
                EventTriggerListener.Get(gameObject).onClick = onDownBtn;
                break;
            case UI_name.SubmitBtn:
                EventTriggerListener.Get(gameObject).onClick = onSubmit;
                break;
            default:
                break;
        }

        filePath = Application.dataPath+ "/切削力和切削温度测量虚拟仿真实验  内页切图/2/";      
    }
    
    // Update is called once per frame
    void onUpBtn(GameObject sender) {
        picNum--;
        Debug.Log(picNum);
        if (picNum <= 0) {
            picNum = 0;
        }
        FileStream fs = new FileStream(filePath + picNum + ".png", FileMode.Open, FileAccess.Read);
        byte[] buffur = new byte[fs.Length];

        fs.Read(buffur, 0, buffur.Length);
        fs.Close();

        Texture2D texture = new Texture2D(10, 10);
        texture.LoadImage(buffur);//流数据转换成Texture2D
        //创建一个Sprite,以Texture2D对象为基础
        Sprite sp = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), Vector2.zero);
        ShowPic.GetComponent<Image>().sprite = sp;






    }
    void onDownBtn(GameObject sender) {
        picNum++;
        if (picNum >= picTotalNum) {
            picNum = picTotalNum;
        }

    }
    void onSubmit(GameObject sender) {

    }
}

优点是减少发布出来的exe大小

缺点是不能随着一起发布 出来。

posted on 2020-08-20 11:45  zqiang0803  阅读(988)  评论(3编辑  收藏  举报

导航