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大小

缺點是不能隨着一起發布 出來。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM