コード例 #1
0
        /// <summary>
        /// Loads a texture from a stream
        /// </summary>
        /// <param name="stream">The stream</param>
        /// <param name="format">The format **NOT UNITYENGINE.TEXTUREFORMAT**</param>
        /// <returns></returns>
        public static Texture2D LoadTexture(Stream stream, TextureFormat format)
        {
            if (format == TextureFormat.BMP)
            {
                return(new BMPLoader().LoadBMP(stream).ToTexture2D());
            }
            else if (format == TextureFormat.DDS)
            {
                return(DDSLoader.Load(stream));
            }
            else if (format == TextureFormat.JPG || format == TextureFormat.PNG)
            {
                byte[] buffer = new byte[stream.Length];
                stream.Read(buffer, 0, (int)stream.Length);

                Texture2D texture = new Texture2D(1, 1);
                texture.LoadImage(buffer);
                return(texture);
            }
            else if (format == TextureFormat.TGA)
            {
                return(TGALoader.Load(stream));
            }
            else
            {
                return(null);
            }
        }
コード例 #2
0
        /// <summary>
        /// Loads a texture from a file
        /// </summary>
        /// <param name="fn"></param>
        /// <param name="normalMap"></param>
        /// <returns></returns>
        public static Texture2D LoadTexture(string fn)
        {
            if (!File.Exists(fn))
            {
                return(null);
            }

            var       textureBytes = File.ReadAllBytes(fn);
            string    ext          = Path.GetExtension(fn).ToLower();
            string    name         = Path.GetFileName(fn);
            Texture2D returnTex    = null;

            switch (ext)
            {
            case ".png":
            case ".jpg":
            case ".jpeg":
                returnTex = new Texture2D(1, 1);
                returnTex.LoadImage(textureBytes);
                returnTex = ImageLoaderHelper.VerifyFormat(returnTex);
                break;

            case ".dds":
                returnTex = DDSLoader.Load(textureBytes);
                break;

            case ".tga":
                returnTex = TGALoader.Load(textureBytes);
                break;

            case ".bmp":
                returnTex = new BMPLoader().LoadBMP(textureBytes).ToTexture2D();
                break;

            case ".crn":
                byte[] crnBytes = textureBytes;
                ushort crnWidth = System.BitConverter.ToUInt16(new byte[2] {
                    crnBytes[13], crnBytes[12]
                }, 0);
                ushort crnHeight = System.BitConverter.ToUInt16(new byte[2] {
                    crnBytes[15], crnBytes[14]
                }, 0);
                byte crnFormatByte = crnBytes[18];

                var crnTextureFormat = UnityEngine.TextureFormat.RGB24;
                if (crnFormatByte == 0)
                {
                    crnTextureFormat = UnityEngine.TextureFormat.DXT1Crunched;
                }
                else if (crnFormatByte == 2)
                {
                    crnTextureFormat = UnityEngine.TextureFormat.DXT5Crunched;
                }
                else if (crnFormatByte == 12)
                {
                    crnTextureFormat = UnityEngine.TextureFormat.ETC2_RGBA8Crunched;
                }
                else
                {
                    Debug.LogError("Could not load crunched texture " + name + " because its format is not supported (" + crnFormatByte + "): " + fn);
                    break;
                }

                returnTex = new Texture2D(crnWidth, crnHeight, crnTextureFormat, true);
                returnTex.LoadRawTextureData(crnBytes);
                returnTex.Apply(true);

                break;

            default:
                Debug.LogError("Could not load texture " + name + " because its format is not supported : " + fn);
                break;
            }

            if (returnTex != null)
            {
                returnTex.name = Path.GetFileNameWithoutExtension(fn);
            }

            return(returnTex);
        }