Esempio n. 1
0
        public void Export()
        {
            var transforms = GetComponentsInChildren <Transform>(true);

#if UNITY_EDITOR
            if (m_ExportAllScene)
            {
                var tr = Resources.FindObjectsOfTypeAll(typeof(Transform));
                Array.Resize(ref transforms, tr.Length);

                for (var i = 0; i < tr.Length; i++)
                {
                    transforms[i] = (Transform)tr[i];
                }
            }
#endif
            // Name, Path

            var list = new List <UGameObject>();

            m_ExportedTextures.Clear();

            if (m_MonoGameExport)
            {
                MonoGameExporter.BeginContentFile("Windows");
            }

            foreach (var tr in transforms)
            {
                list.Add(ExportObject(tr));

                if (m_LogEnabled)
                {
                    Debug.Log($"Exporter: {tr.name}");
                }
            }

            if (!Directory.Exists(m_ExportPath))
            {
                Directory.CreateDirectory(m_ExportPath);
            }

            var json = JsonConvert.SerializeObject(list.ToArray(), m_JSONFormat);
            var path = Path.Combine(m_ExportPath, $"{m_ExportFilename}.json");

            File.WriteAllText(path, json);

            if (m_MonoGameExport)
            {
                MonoGameExporter.AddMap(path);
                File.WriteAllText(Path.Combine(m_ExportPath, "Content.mgcb"), MonoGameExporter.GetContentData());
            }

            if (m_LogEnabled)
            {
                Debug.Log($"Exported: {list.Count} objects");
            }
        }
Esempio n. 2
0
        private string ExportTexture(Texture texture, string folder)
        {
            if (texture == null)
            {
                return(null);
            }

            if (!m_ExportTextures)
            {
                return(texture.name);
            }

            if (!texture.isReadable)
            {
                Debug.LogWarning($"The texture {texture.name} is not readable so we can't export it.");
                return(texture.name);
            }

            if (m_ExportedTextures.ContainsKey(texture.name))
            {
                return(m_ExportedTextures[texture.name]);
            }

            Texture2D tex2D = null;

            if (texture is RenderTexture)
            {
                RenderTexture.active = (RenderTexture)texture;
                tex2D = new Texture2D(texture.width, texture.height);
                tex2D.ReadPixels(new Rect(0, 0, texture.width, texture.height), 0, 0);
                tex2D.Apply();
            }
            else
            {
                tex2D = ToTexture2D(texture);
            }

            var bytes = tex2D.EncodeToPNG();
            var absoluteTexturePath = Path.Combine(m_ExportPath, "Textures", folder);

            if (!Directory.Exists(absoluteTexturePath))
            {
                Directory.CreateDirectory(absoluteTexturePath);
            }

            var textureName = texture.name;

            if (string.IsNullOrEmpty(textureName))
            {
                textureName = Guid.NewGuid().ToString();
            }

            try
            {
                File.WriteAllBytes(Path.Combine(absoluteTexturePath, $"{texture.name}.png"), bytes);

                var relativeTexturePath = Path.Combine("Textures", folder, $"{texture.name}.png");

                m_ExportedTextures.Add(texture.name, relativeTexturePath);

                if (m_MonoGameExport)
                {
                    MonoGameExporter.AddTexture(relativeTexturePath);
                }

                Debug.Log($"Texture {texture.name} was exported in {absoluteTexturePath}.");

                return(relativeTexturePath);
            }
            catch (Exception ex)
            {
                Debug.Log(ex.Message);

                return(texture.name);
            }
        }