示例#1
0
        protected Resource(ResourceMetadata metadata)
        {
            Assert.ArgumentNotNull(metadata, nameof(metadata));

            Metadata = metadata;
        }
示例#2
0
        private bool TryLoadResourceMetadata(string directory, out ResourceMetadata resourceMetadata)
        {
            resourceMetadata = null;

            if (Directory.GetFiles(directory).Length == 0)
            {
                return(false);
            }

            var metadataPath = Path.Combine(directory, "metadata.json");

            if (!File.Exists(metadataPath))
            {
                return(false);
            }

            var resourceTypeString = "";
            var guidString         = "";
            var name = "";
            var textureExtensionString = "";

            try
            {
                var json = File.ReadAllText(metadataPath);

                var jsonMetadata = JObject.Parse(json);

                resourceTypeString = jsonMetadata["ResourceType"].ToString();
                name                   = jsonMetadata["Name"].ToString();
                guidString             = jsonMetadata["Guid"].ToString();
                textureExtensionString = jsonMetadata["TextureExtension"].ToString();
            }
            catch (Exception e)
            {
                var a = e;
                return(false);
            }

            if (string.IsNullOrEmpty(resourceTypeString) ||
                string.IsNullOrEmpty(guidString) ||
                string.IsNullOrEmpty(name) ||
                string.IsNullOrEmpty(textureExtensionString))
            {
                return(false);
            }

            if (!Enum.TryParse <ResourceType> (resourceTypeString, out var resourceType))
            {
                return(false);
            }

            if (!Guid.TryParse(guidString, out var guid))
            {
                return(false);
            }

            if (!Enum.TryParse <TextureExtension> (textureExtensionString, out var textureExtension))
            {
                return(false);
            }

            if (new DirectoryInfo(directory).Name != name + "_" + guidString)
            {
                return(false);
            }

            resourceMetadata = new ResourceMetadata(Context, name, guid, textureExtension, resourceType);

            return(true);
        }
示例#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));
            }
        }
示例#4
0
        public static Texture2DResource Existing(ResourceMetadata metadata)
        {
            Assert.ArgumentNotNull(metadata, nameof(metadata));

            return(new Texture2DResource(metadata));
        }