Siccity / GLTFUtility

Simple GLTF importer for Unity
MIT License
976 stars 217 forks source link

Is there any way to save to prefab in editor #205

Closed catsuperberg closed 2 years ago

catsuperberg commented 2 years ago

I want to implement a system that scans a folder and from every .glb it creates a prefab and saves it in the project (if i launch game in editor window). Simply saving a prefab with that model doesn't work. I tried saving a material from imported gameobject but it only makes it lose Albedo and become white. Is there any way to transform imported model to permanent prefab during the runtime, or at least in the editor (with script)?

EDIT: Managed to do it Sorry, didn't think it would have any reason to work, but i just used GetTexture("_MainTex") on a shader, also got a mesh, then saved texture, mesh and material as .asset files and that did it. So just save texture, mesh and material as .asset files and create a prefab after that, no need to hook anything up.

catsuperberg commented 2 years ago

Managed to find a solution

ugurberkecan commented 1 year ago

@KEALHOVIK hey could you share your getTexture methods ?

catsuperberg commented 1 year ago

@KEALHOVIK hey could you share your getTexture methods ?

@ugurberkecan Hey, here's how i store assets (model, texture, material) and create prefabs:


        public string CreatePrefab(GameObject skinObject, string folderToSaveTo)
        {          
            BlenderToGameTransform(skinObject);
            skinObject.SetActive(true);

            SaveModel(skinObject, folderToSaveTo);
            var pathToPrefab = SavePrefab(skinObject, folderToSaveTo);
            return pathToPrefab;
        }

        void SaveModel(GameObject modelObject, string folder)
        {
            var textureName = "texture_" + modelObject.name + ".asset";
            var materialName = "material_" + modelObject.name + ".asset";
            var meshName = "mesh_" + modelObject.name + ".asset";
            var renderer = modelObject.GetComponent<Renderer>();
            var material = renderer.sharedMaterial;
            var mesh = modelObject.GetComponent<MeshFilter>().sharedMesh;
            var texture = material.GetTexture("_MainTex");

            // Order is relevant, material wouldn't have texture if texture is't saved before it     
            if(texture != null)  
                AssetDatabase.CreateAsset(texture, System.IO.Path.Combine(folder, textureName));  
            AssetDatabase.CreateAsset(material, System.IO.Path.Combine(folder, materialName));    
            AssetDatabase.CreateAsset(mesh, System.IO.Path.Combine(folder, meshName));        
        }      

        string SavePrefab(GameObject prefab, string folder)
        {
            bool prefabSuccess;            
            var fullPath = System.IO.Path.Combine(folder, prefab.name + ".prefab");
            PrefabUtility.SaveAsPrefabAsset(prefab, fullPath, out prefabSuccess);
            if(!prefabSuccess)
                throw new Exception("Couldn't save created asset as prefab");

            return fullPath.Replace("Assets/Prefabs/Gameplay Items/Projectiles/Resources/", "");
        }