combining png file textures and the Streaming Assets folder

Below is some code that takes whatever pngs you put in the Streaming Assets folder, puts them all into one texture and assigns them to a File Texture  input for a Visual FX graph. Certain things like resolution have been hardcoded into this script, so you might want to alter those numbers. You'll want to use the flipbook image so that each image can be used seperately (or not), as sprites. This should work with normal particles as well, if altered a bit.

 

 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using UnityEngine.UI;
using UnityEngine.Networking;
using UnityEngine.VFX;
using UnityEngine.Rendering;
using UnityEngine.Rendering.HighDefinition;



public class multiTextureScan : MonoBehaviour
{
    public VisualEffect _visualEffect;
    public Dictionary<string, Texture2D> textures = new Dictionary<string, Texture2D>();
    // Source textures.
    public Texture2D[] textureShop;
    // Rectangles for individual atlas textures.
    public Rect[] rects;
    public Texture2D atlas;

    public string filesLocation = Application.streamingAssetsPath;
    private int i;
    public int fileNum=0;
    private void Start()
    {
        i = 0;
        var fileNames = Directory.GetFiles(filesLocation, "*.png");
        fileNum = fileNames.Length;
        textureShop = new Texture2D[fileNum];

        Debug.Log("array is " + fileNum + " long");

        foreach (var fileName in fileNames)
        {
            StartCoroutine(LoadFile(fileName));
            Debug.Log("image file found: " + fileName);
        }
        
    }


    public void PackStuff()
    {
        // Pack the individual textures into the smallest possible space,
        // while leaving a ONE pixel gap between their edges.
        atlas = new Texture2D(4096 ,4096);
        rects = atlas.PackTextures(textureShop, 1, 4096);
        Debug.Log("packed textures");
    }

    void SetStuff()
    {
        _visualEffect.SetTexture("fileTexture", atlas);
        Debug.Log("set atlas to vfx");

    }



    /*
        public void printImage()
        {
            foreach(var texture in textures)
            {

                Debug.Log("accessing dictionary"+ textures[texture]);

            }


        }
        */
    private IEnumerator LoadFile(string filePath)
    {
        using (UnityWebRequest uwr = UnityWebRequestTexture.GetTexture(filePath))
        {
            yield return uwr.SendWebRequest();

            if (uwr.isNetworkError || uwr.isHttpError)
            {
                Debug.Log(uwr.error);
            }
            else
            {
                // Get downloaded asset bundle
                var texture = DownloadHandlerTexture.GetContent(uwr);

                // Something with the texture e.g.
                // store it to later access it by fileName
                var fileName = Path.GetFileName(filePath);
                textureShop[i] = texture;
                Debug.Log("stored " + fileName + " into array");
                i++;
                Debug.Log("i is now : " + i);

                if (i >= fileNum - 1)
                {
                    PackStuff();
                    SetStuff();
                }

            }
        }
    }



    /*
            private IEnumerator LoadFile(string filePath)
        {
            using (UnityWebRequest uwr = UnityWebRequestTexture.GetTexture(filePath))
            {
                yield return uwr.SendWebRequest();

                if (uwr.isNetworkError || uwr.isHttpError)
                {
                    Debug.Log(uwr.error);
                }
                else
                {
                    // Get downloaded asset bundle
                    var texture = DownloadHandlerTexture.GetContent(uwr);

                    // Something with the texture e.g.
                    // store it to later access it by fileName
                    var fileName = Path.GetFileName(filePath);
                    textures[fileName] = texture;
                    Debug.Log("stored " + fileName + " into array");
                }
            }
        }
    */


}





Comments

Popular posts from this blog

setting VFX graph properties using C#