public Texture2D LoadTexture(string path, CreateTextureOptions options)
        {
            Ensure.That(nameof(path)).IsNotNull(path);

            path = NormalizePath(path);

            var stream = assembly.GetManifestResourceStream(path);

            if (stream == null)
            {
                Debug.LogWarning("Failed to get assembly resource stream:\n" + path);

                return(null);
            }

            BinaryReader reader = null;

            try
            {
                reader = new BinaryReader(stream);
                var bytes = reader.ReadBytes((int)stream.Length);

                var texture = new Texture2D(0, 0, options.textureFormat, options.mipmaps, options.linear ?? LudiqGUIUtility.createLinearTextures);
                texture.alphaIsTransparency = options.alphaIsTransparency;
                texture.filterMode          = options.filterMode;
                texture.hideFlags           = options.hideFlags;
                texture.LoadImage(bytes);

                return(texture);
            }
            finally
            {
                reader?.Close();
            }
        }
        public static EditorTexture Load(IResourceProvider resources, string path, CreateTextureOptions options, bool required)
        {
            using (ProfilingUtility.SampleBlock("Load Editor Texture"))
            {
                Ensure.That(nameof(resources)).IsNotNull(resources);
                Ensure.That(nameof(path)).IsNotNull(path);

                var set       = new EditorTexture();
                var name      = Path.GetFileNameWithoutExtension(path).PartBefore('@');
                var extension = Path.GetExtension(path);
                var directory = Path.GetDirectoryName(path);

                var personalPath     = Path.Combine(directory, $"{name}{extension}");
                var professionalPath = Path.Combine(directory, $"{name}@Pro{extension}");

                var texture = resources.LoadTexture(personalPath, options);

                if (texture != null)
                {
                    set.personal.Add(texture.width, texture);
                }

                texture = resources.LoadTexture(professionalPath, options);

                if (texture != null)
                {
                    set.professional.Add(texture.width, texture);
                }

                if (set.personal.Count == 0)
                {
                    if (required)
                    {
                        Debug.LogWarning($"Missing editor texture: {name}\n{resources.DebugPath(path)}");
                    }

                    // Never return an empty set; the codebase assumes this guarantee

                    return(null);
                }

                return(set);
            }
        }
 public Texture2D LoadTexture(string path, CreateTextureOptions options)
 {
     return(LoadAsset <Texture2D>(path));
 }
        public static EditorTexture Load(IResourceProvider resources, string path, TextureResolution[] resolutions, CreateTextureOptions options, bool required)
        {
            using (ProfilingUtility.SampleBlock("Load Editor Texture"))
            {
                Ensure.That(nameof(resources)).IsNotNull(resources);
                Ensure.That(nameof(path)).IsNotNull(path);
                Ensure.That(nameof(resolutions)).HasItems(resolutions);

                var set       = new EditorTexture();
                var name      = Path.GetFileNameWithoutExtension(path).PartBefore('@');
                var extension = Path.GetExtension(path);
                var directory = Path.GetDirectoryName(path);

                // Try with explicit resolutions first
                foreach (var resolution in resolutions)
                {
                    var width = resolution.width;
                    // var height = resolution.height;

                    var personalPath     = Path.Combine(directory, $"{name}@{width}x{extension}");
                    var professionalPath = Path.Combine(directory, $"{name}@{width}x_Pro{extension}");

                    if (resources.FileExists(personalPath))
                    {
                        set.personal.Add(width, resources.LoadTexture(personalPath, options));
                    }

                    if (resources.FileExists(professionalPath))
                    {
                        set.professional.Add(width, resources.LoadTexture(professionalPath, options));
                    }
                }

                if (set.personal.Count == 0)
                {
                    if (required)
                    {
                        Debug.LogWarning($"Missing editor texture: {name}\n{resources.DebugPath(path)}");
                    }

                    // Never return an empty set; the codebase assumes this guarantee

                    return(null);
                }

                return(set);
            }
        }
        public static EditorTexture Load(IEnumerable <IResourceProvider> resourceProviders, string path, TextureResolution[] resolutions, CreateTextureOptions options, bool required)
        {
            foreach (var resources in resourceProviders)
            {
                var texture = Load(resources, path, resolutions, options, false);

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

            if (required)
            {
                var message = new StringBuilder();
                message.AppendLine("Missing editor texture: ");

                foreach (var resources in resourceProviders)
                {
                    message.AppendLine($"{resources.GetType().HumanName()}: {resources.DebugPath(path)}");
                }

                Debug.LogWarning(message.ToString());
            }

            return(null);
        }
Example #6
0
 public EditorTexture LoadTexture(string path, TextureResolution[] resolutions, CreateTextureOptions options, bool required = true)
 {
     return(EditorTexture.Load(providers, path, resolutions, options, required));
 }