Resources.LoadAll并不总是返回相同的结果

时间:2018-10-08 10:59:19

标签: unity3d

启动应用程序时,我将Texture2D数组发送给方法。

namespace MyNamespace
{
    public class App : MonoBehaviour
    {
        public static App Instance { get; private set; }
        public Manager Manager = new Manager();

        [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
        static void OnRuntimeMethodLoad()
        {
            if (Instance == null)
            {
                Instance = new GameObject(nameof(App)).AddComponent<App>();
                Instance.Manager.TexturesConsumer(Resources.LoadAll("Textures/Ingredients/", typeof(Texture2D)));
            }
            DontDestroyOnLoad(Instance);
        }
    }
}

TexturesConsumer从指定目录检索纹理并将其存储在ObservableCollection中。 顺便说一句,它修改了每个texture.name,以使对象名称层次更加清晰。

public void TexturesConsumer(UnityEngine.Object[] textures)
{
    var txturList = new List<Texture2D>();
    string[] sp;
    string[] sp2;
    foreach (var txtur in Array.FindAll(textures, _ => IsValidImageFileName(_.name)))
    {
        txtur.name = txtur.name.Substring(IngredientTexturePrefix.Length, txtur.name.Length - IngredientTexturePrefix.Length - 2);
        sp = (txtur.name).Split(FSep);
        foreach (var compo in Enum.GetNames(typeof(Compos)))
            if (AvailableCompositions.Contains((Compos)Enum.Parse(typeof(Compos), compo)))
                foreach (var candid in Array.FindAll(textures, _ => _.name == IngredientTexturePrefix + sp[0] + FSep + sp[1] + FSep + "t" + FSep + compo.ToString()))
                {
                    sp2 = candid.name.Split(FSep);
                    candid.name = sp2[1] + FSep + sp2[2];
                    txturList.Add((Texture2D)candid);
                }
        if (txturList.Any())
            ListUIi.Add(new IngredientUI((Category)Enum.Parse(typeof(Category), sp[0]), int.Parse(sp[1]), (Texture2D)txtur, txturList));
        txturList.Clear();
    }
}

在第一次执行时一切正常,Resources.LoadAll的每个纹理的名称都与png文件的名称相对应。因此TexturesConsumer可以正常工作。

但是在第二次执行时,Resources.LoadAll返回已重命名的纹理“已经”! 好像png文件在第一次运行时就被重命名了!

在第三次执行时,一切再次恢复正常,我恢复了第一次执行时的纹理。

第四次执行,已经重命名了纹理:(

依此类推...

编辑添加
我忘了提到Resources / Textures / Ingredients目录是一个符号链接。
这只能解释问题的一部分

谁仍然是真实的。

为了说服自己,只需将此脚本添加到新项目并运行几次(单击“播放”)。

using System;
using System.Collections.Generic;
using UnityEngine;

namespace MyNamespace
{
    public class App : MonoBehaviour
    {
        public static App Instance { get; private set; }
        private List<Texture2D> Listt = new List<Texture2D>();

        [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
        static void OnRuntimeMethodLoad()
        {
            if (Instance == null)
            {
                Instance = new GameObject(nameof(App)).AddComponent<App>();
                Instance.TexturesConsumer(Resources.LoadAll("Textures/", typeof(Texture2D)));
            }
            DontDestroyOnLoad(Instance);
        }
        public void TexturesConsumer(UnityEngine.Object[] textures)
        {
            var outString = "";
            foreach (var txtur in textures)
            {
                txtur.name = txtur.name.Substring(0, txtur.name.Length - 1);
                Instance.Listt.Add((Texture2D)txtur);
                outString += txtur.name + "\n";
            }
            Debug.LogFormat(outString);
        }
    }
}

执行一定次数后(取决于目录的最短文件名的大小),该问题将在控制台中出现。

0 个答案:

没有答案