Example #1
0
        public static Texture2DResource New(ResourceMetadata metadata, Texture2D texture)
        {
            Assert.ArgumentNotNull(metadata, nameof(metadata));
            Assert.ArgumentNotNull(texture, nameof(texture));

            var resource = new Texture2DResource(metadata);

            resource.texture = texture;
            resource.Cache();
            resource.UnloadResource();

            return(resource);
        }
Example #2
0
        private void Setup()
        {
            if (!Directory.Exists(Context.ResourcesCacheDirectory))
            {
                return;
            }

            foreach (var directory in Directory.GetDirectories(Context.ResourcesCacheDirectory))
            {
                if (TryLoadResourceMetadata(directory, out var metadata))
                {
                    if (metadata.ResourceType == ResourceType.Texture2D)
                    {
                        Register(Texture2DResource.Existing(metadata));
                    }
                }
                else
                {
                    Directory.Delete(directory, true);
                }
            }
        }
Example #3
0
        private ResourceImportResult ImportResource_Internal(string path)
        {
            if (string.IsNullOrEmpty(path))
            {
                return(new ResourceImportResult(false, "Path is null or emptry"));
            }

            if (!Path.HasExtension(path))
            {
                return(new ResourceImportResult(false, $"Path({path}) does not has any extension"));
            }

            if (!File.Exists(path))
            {
                return(new ResourceImportResult(false, $"File at ({path}) does not exists"));
            }

            try
            {
                var extension = Path.GetExtension(path);
                var name      = Path.GetFileNameWithoutExtension(path);

                if (extension == ".png" || extension == ".jpeg" || extension == ".jpg" || extension == ".tga")
                {
                    var texture = TextureUtility.LoadTexture2DFromDisk(path);

                    if (texture.width != texture.height)
                    {
                        var result = new ResourceImportResult(false, $"Texture({path}) is not square ({texture.width}, {texture.height})");
                        GameObject.DestroyImmediate(texture);
                        return(result);
                    }

                    if (!Mathf.IsPowerOfTwo(texture.width))
                    {
                        var result = new ResourceImportResult(false, $"Texture({path}) has not power of 2 size ({texture.width}, {texture.height})");
                        GameObject.DestroyImmediate(texture);
                        return(result);
                    }

                    var textureMinSize = 256;

                    if (texture.width < textureMinSize)
                    {
                        var result = new ResourceImportResult(false, $"Texture({path}) is too small({texture.width}, {texture.height})). Min size is {textureMinSize}");
                        GameObject.DestroyImmediate(texture);
                        return(result);
                    }

                    var metadata = new ResourceMetadata(Context, name, Context.CacheTextureResolution, ResourceType.Texture2D);

                    var resource = Texture2DResource.New(metadata, texture);
                    return(new ResourceImportResult(true, "", resource));
                }
                else
                {
                    return(new ResourceImportResult(false, $"Extension {extension} is not supported."));
                }
            }
            catch (Exception e)
            {
                return(new ResourceImportResult(false, "An exception was thrown when importing a resource! " + e.Message));
            }
        }