Код не выполняется после вызова конструктора Texture2D - PullRequest
0 голосов
/ 29 июня 2019

Я загружаю изображения из папки и подаю заявку на RawImage.Затем я проверяю, есть ли новое изображение в папке с помощью FileSystemWatcher класса.

public static Dictionary<string, Object> textures = new Dictionary<string, Object>();

 private void Start()
    {
        //Path
        try
        {
            string[] configMessage = ReadConfig.GetLines("Path");
            path = configMessage[0];
        }
        catch
        {
            path = "C:\\xampp\\htdocs\\folder\\images";
        }

        FileSystemWatcher watcher = new FileSystemWatcher();
        watcher.Path = path;
        watcher.Created += new FileSystemEventHandler(OnChanged);

        watcher.EnableRaisingEvents = true;

        GetAssetList();

        for (int i = 0; i < textures.Count; ++i)
        {
            AddImageToMosaic((Texture)textures.ElementAt(i).Value);
        }

    } 

private void GetAssetList()
    {
        try
        {
            string[] fileEntries = Directory.GetFiles(path);

            foreach (string fileName in fileEntries)
            {
                byte[] fileData;
                Texture2D tex = null;
                if (File.Exists(fileName))
                {
                    fileData = File.ReadAllBytes(fileName);
                    tex = new Texture2D(2, 2);
                    tex.LoadImage(fileData);
                    textures.Add(fileName, tex);
                }
            }
        }
        catch (System.Exception e)
        {
            print(e.ToString());
        }

    }

private void OnChanged(object source, FileSystemEventArgs e)
    {
        print("OnChanged " + e.FullPath);
        Populate(e.FullPath);
    }

Затем при FileSystemWatcher событии я вызываю Populate():

 static Populate(string imageName)
    {
        string fileName = imageName;
        byte[] fileData;
        if (File.Exists(fileName))
        {
            fileData = File.ReadAllBytes(fileName);
            Texture2D tex = new Texture2D(128, 128);
            tex.LoadImage(fileData);
            if (!textures.ContainsKey(fileName))
            {
                textures.Add(fileName, tex);
            }

        }
    }

Код не выполняется после Texture2D tex = new Texture2D(128, 128);.Я подтвердил это несколькими журналами до и после.

...