Example #1
0
 protected virtual UnityPath GetAssetPath(UnityPath prefabPath, UnityEngine.Object o)
 {
     if (o is Material)
     {
         var materialDir  = prefabPath.GetAssetFolder(".Materials");
         var materialPath = materialDir.Child(o.name.EscapeFilePath() + ".asset");
         return(materialPath);
     }
     else if (o is Texture2D)
     {
         var textureDir  = prefabPath.GetAssetFolder(".Textures");
         var texturePath = textureDir.Child(o.name.EscapeFilePath() + ".asset");
         return(texturePath);
     }
     else if (o is Mesh && !MeshAsSubAsset)
     {
         var meshDir  = prefabPath.GetAssetFolder(".Meshes");
         var meshPath = meshDir.Child(o.name.EscapeFilePath() + ".asset");
         return(meshPath);
     }
     else
     {
         return(default(UnityPath));
     }
 }
Example #2
0
        public void SaveTexturesAsPng(UnityPath prefabPath)
        {
            TextureBaseDir = prefabPath.Parent;
            TextureBaseDir.ImportAsset();

            //
            // https://answers.unity.com/questions/647615/how-to-update-import-settings-for-newly-created-as.html
            //
            for (int i = 0; i < GLTF.textures.Count; ++i)
            {
                var x     = GLTF.textures[i];
                var image = GLTF.images[x.source];
                if (string.IsNullOrEmpty(image.uri))
                {
                    // glb buffer
                    var folder = prefabPath.GetAssetFolder(".Textures");
                    folder.EnsureFolder();

                    // name & bytes
                    var textureName = !string.IsNullOrEmpty(image.name) ? image.name : string.Format("{0:00}#GLB", i);
                    var byteSegment = GLTF.GetViewBytes(image.bufferView);

                    // path
                    var png = folder.Child(textureName + ".png");
                    File.WriteAllBytes(png.FullPath, byteSegment.ToArray());
                    png.ImportAsset();

                    image.uri = png.Value.Substring(TextureBaseDir.Value.Length + 1);
                    //Debug.LogFormat("image.uri: {0}", image.uri);
                }
            }
            UnityEditor.AssetDatabase.Refresh();
        }
        /// <summary>
        /// Extract images from glb or gltf out of Assets folder.
        /// </summary>
        /// <param name="prefabPath"></param>
        public void ExtranctImages(UnityPath prefabPath)
        {
            var prefabParentDir = prefabPath.Parent;

            // glb buffer
            var folder = prefabPath.GetAssetFolder(".Textures");

            //
            // https://answers.unity.com/questions/647615/how-to-update-import-settings-for-newly-created-as.html
            //
            int created = 0;

            //for (int i = 0; i < GLTF.textures.Count; ++i)
            for (int i = 0; i < GLTF.images.Count; ++i)
            {
                folder.EnsureFolder();

                //var x = GLTF.textures[i];
                var image = GLTF.images[i];
                var src   = Storage.GetPath(image.uri);
                if (UnityPath.FromFullpath(src).IsUnderAssetsFolder)
                {
                    // asset is exists.
                }
                else
                {
                    string textureName;
                    var    byteSegment = GLTF.GetImageBytes(Storage, i, out textureName);

                    // path
                    var dst = folder.Child(textureName + image.GetExt());
                    File.WriteAllBytes(dst.FullPath, byteSegment.ToArray());
                    dst.ImportAsset();

                    // make relative path from PrefabParentDir
                    image.uri = dst.Value.Substring(prefabParentDir.Value.Length + 1);
                    ++created;
                }
            }

            if (created > 0)
            {
                AssetDatabase.Refresh();
            }

            CreateTextureItems(prefabParentDir);
        }
Example #4
0
        /// <summary>
        /// Extract images from glb or gltf out of Assets folder.
        /// </summary>
        /// <param name="prefabPath"></param>
        public void ExtractImages(UnityPath prefabPath)
        {
            var prefabParentDir = prefabPath.Parent;

            // glb buffer
            var folder = prefabPath.GetAssetFolder(".Textures");

            //
            // https://answers.unity.com/questions/647615/how-to-update-import-settings-for-newly-created-as.html
            //
            int created = 0;

            for (int i = 0; i < m_context.GLTF.textures.Count; ++i)
            {
                folder.EnsureFolder();

                var gltfTexture = m_context.GLTF.textures[i];
                var gltfImage   = m_context.GLTF.images[gltfTexture.source];
                var src         = m_context.Storage.GetPath(gltfImage.uri);
                if (UnityPath.FromFullpath(src).IsUnderAssetsFolder)
                {
                    // asset is exists.
                }
                else
                {
                    var byteSegment = m_context.GLTF.GetImageBytes(m_context.Storage, gltfTexture.source);
                    var textureName = gltfTexture.name;

                    // path
                    var dst = folder.Child(textureName + gltfImage.GetExt());
                    File.WriteAllBytes(dst.FullPath, byteSegment.ToArray());
                    dst.ImportAsset();

                    // make relative path from PrefabParentDir
                    gltfImage.uri = dst.Value.Substring(prefabParentDir.Value.Length + 1);
                    ++created;
                }
            }

            if (created > 0)
            {
                AssetDatabase.Refresh();
            }

            // texture will load from assets
            m_context.TextureFactory.ImageBaseDir = prefabParentDir;
        }
Example #5
0
        public void SaveAsAsset(UnityPath prefabPath)
        {
            ShowMeshes();

            //var prefabPath = PrefabPath;
            if (prefabPath.IsFileExists)
            {
                // clear SubAssets
                foreach (var x in prefabPath.GetSubAssets().Where(x => !(x is GameObject) && !(x is Component)))
                {
                    GameObject.DestroyImmediate(x, true);
                }
            }

            // Add SubAsset
            var materialDir = prefabPath.GetAssetFolder(".Materials");

            materialDir.EnsureFolder();
            var textureDir = prefabPath.GetAssetFolder(".Textures");

            textureDir.EnsureFolder();
            var meshDir = prefabPath.GetAssetFolder(".Meshes");

            if (!MeshAsSubAsset)
            {
                meshDir.EnsureFolder();
            }

            var paths = new List <UnityPath>()
            {
                prefabPath
            };

            foreach (var o in ObjectsForSubAsset())
            {
                if (o is Material)
                {
                    var materialPath = materialDir.Child(o.name.EscapeFilePath() + ".asset");
                    materialPath.CreateAsset(o);
                    paths.Add(materialPath);
                }
                else if (o is Texture2D)
                {
                    var texturePath = textureDir.Child(o.name.EscapeFilePath() + ".asset");
                    texturePath.CreateAsset(o);
                    paths.Add(texturePath);
                }
                else if (o is Mesh && !MeshAsSubAsset)
                {
                    var meshPath = meshDir.Child(o.name.EscapeFilePath() + ".asset");
                    meshPath.CreateAsset(o);
                    paths.Add(meshPath);
                }
                else
                {
                    // save as subasset
                    prefabPath.AddObjectToAsset(o);
                }
            }

            // Create or upate Main Asset
            if (prefabPath.IsFileExists)
            {
                Debug.LogFormat("replace prefab: {0}", prefabPath);
                var prefab = prefabPath.LoadAsset <GameObject>();
                PrefabUtility.ReplacePrefab(Root, prefab, ReplacePrefabOptions.ReplaceNameBased);
            }
            else
            {
                Debug.LogFormat("create prefab: {0}", prefabPath);
                PrefabUtility.CreatePrefab(prefabPath.Value, Root);
            }
            foreach (var x in paths)
            {
                x.ImportAsset();
            }
        }