コード例 #1
0
        public static Texture LoadTexture(string basePath, SaveTextureInfo info)
        {
            if (info == null || info.path == null)
            {
                return(null);
            }
            string path = System.IO.Path.Combine(basePath, info.path);

            byte[]    data = System.IO.File.ReadAllBytes(path);
            Texture2D tex  = null;

#if UNITY_2019_2_OR_NEWER
            tex = new Texture2D(info.width, info.height, info.rawFormat, info.mipCount, false);
#else
            tex = new Texture2D(info.width, info.height, info.rawFormat, (info.mipCount > 0), false);
#endif
            switch (info.type)
            {
            case SaveTextureInfo.TYPE_PNG:
                ImageConversion.LoadImage(tex, data);
                break;

            case SaveTextureInfo.TYPE_EXR:
                ImageConversion.LoadImage(tex, data);
                break;

            case SaveTextureInfo.TYPE_RAWDATA:
                tex.LoadRawTextureData(data);
                tex.Apply();
                break;
            }

            return(tex);
        }
コード例 #2
0
        public static SaveTextureInfo SaveRenderTexture(RenderTexture renderTexture, string file)
        {
            SaveTextureInfo saveInfo = null;

            try
            {
                Texture2D tex = new Texture2D(renderTexture.width, renderTexture.height, GetTextureFormat(renderTexture), false);
                RenderTexture.active = renderTexture;
                tex.ReadPixels(new Rect(0, 0, renderTexture.width, renderTexture.height), 0, 0);
                tex.Apply();

                if (ShoudSaveRawData(tex))
                {
                    byte[] bytes = tex.GetRawTextureData();
                    file += ".raw";
                    System.IO.File.WriteAllBytes(file, bytes);
                    saveInfo = new SaveTextureInfo(file, renderTexture, SaveTextureInfo.TYPE_RAWDATA);
                }
                else
                {
                    byte[] bytes = tex.EncodeToPNG();
                    file += ".png";
                    System.IO.File.WriteAllBytes(file, bytes);
                    saveInfo = new SaveTextureInfo(file, renderTexture, SaveTextureInfo.TYPE_PNG);
                }
                Object.DestroyImmediate(tex);
                return(saveInfo);
            }catch (System.Exception e) {
                Debug.LogError(e);
            }
            return(null);
        }
コード例 #3
0
        public static SaveTextureInfo SaveTexture(Texture2D tex, string file)
        {
            SaveTextureInfo saveInfo = null;

            try
            {
                Texture2D writeTexture = null;
                if (tex.isReadable)
                {
                    writeTexture = tex;
                }
                else
                {
#if UNITY_2019_2_OR_NEWER
                    writeTexture = new Texture2D(tex.width, tex.height, tex.format, tex.mipmapCount, false);
#else
                    writeTexture = new Texture2D(tex.width, tex.height, tex.format, tex.mipmapCount > 0, false);
#endif
                    Graphics.CopyTexture(tex, writeTexture);
                }
                if (ShoudSaveRawData(tex))
                {
                    byte[] bytes = writeTexture.GetRawTextureData();
                    file += ".raw";
                    System.IO.File.WriteAllBytes(file, bytes);
                    saveInfo = new SaveTextureInfo(file, tex, SaveTextureInfo.TYPE_RAWDATA);
                }
                else
                {
                    byte[] bytes = writeTexture.EncodeToPNG();
                    file += ".png";
                    System.IO.File.WriteAllBytes(file, bytes);
                    saveInfo = new SaveTextureInfo(file, tex, SaveTextureInfo.TYPE_PNG);
                }
                if (tex != writeTexture)
                {
                    Object.DestroyImmediate(writeTexture);
                }
                return(saveInfo);
            }
            catch (System.Exception e)
            {
                Debug.LogError(e);
            }
            return(null);
        }