Example #1
0
    public void UpdateAll()
    {
        List <string> newNames = new List <string>();
        string        fullPath = Path.Combine(Application.streamingAssetsPath, "Resources", ResourcePath);

        // 生成 newNames:List 当前文件夹下所有 FileSuffix 后缀的文件名
        if (Directory.Exists(fullPath))
        {
            DirectoryInfo direction = new DirectoryInfo(fullPath);
            FileInfo[]    files     = direction.GetFiles("*", SearchOption.AllDirectories);

            foreach (FileInfo file in files)
            {
                if (ResAPI.HasSuffix(file.Name, false))
                {
                    newNames.Add(ResAPI.RemoveSuffix(file.Name));
                }
            }
        }

        // 删除:原List有 新List没有,需要删除
        IEnumerable <string> deleteList = imageNames.Except(newNames);
        // 添加:新List有 原List没有,需要添加
        IEnumerable <string> newList = newNames.Except(imageNames);

        // imageNames 列表更新,需及时更新,否则重复点击刷新可能会造成重复添加
        imageNames = newNames;

        // 添加的处理
        foreach (string name in newList)
        {
            StartCoroutine(AddNewImage(name));
        }

        // 删除的处理
        foreach (string name in deleteList)
        {
            Destroy(images[name].gameObject);
            images.Remove(name);
        }
    }
Example #2
0
    /// <summary>
    ///     用 IO 方式从外部加载图片到 Sprite
    /// </summary>
    /// <param name="path">
    ///     可以是 Resources/ 下的路径也可以是绝对路径,不加后缀
    /// </param>
    public static Sprite LoadSprite(string path)
    {
        // 路径格式处理
        if (!path.Contains(ResourcePath))
        {
            path = Path.Combine(ResourcePath, path);
        }
        if (!ResAPI.HasSuffix(path, false))
        {
            path = ResAPI.FillSuffix(path);
        }

        Texture2D texture = LoadTexture(path);

        if (texture == null)
        {
            return(null);
        }

        // 创建 Sprite
        Sprite sprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), new Vector2(0.5f, 0.5f));

        return(sprite);
    }