private Material CreateAtlasMaterialAndTexture(string shaderToAtlas, int shaderIndex, TextureReuseManager textureReuseManager) { string fileName = ((ObjectsGUI.CustomAtlasName == "") ? "Atlas " : (ObjectsGUI.CustomAtlasName + " ")) + shaderToAtlas.Replace('/', '_'); string folderToSaveAssets = EditorApplication.currentScene; if (folderToSaveAssets == "") //scene is not saved yet. { folderToSaveAssets = Constants.NonSavedSceneFolderName + ".unity"; Debug.LogWarning("WARNING: Scene has not been saved, saving baked objects to: " + Constants.NonSavedSceneFolderName + " folder"); } folderToSaveAssets = folderToSaveAssets.Substring(0, folderToSaveAssets.Length - 6) + "-Atlas"; //remove the ".unity" and add "-Atlas" if (!Directory.Exists(folderToSaveAssets)) { Directory.CreateDirectory(folderToSaveAssets); AssetDatabase.ImportAsset(folderToSaveAssets); } string atlasTexturePath = folderToSaveAssets + Path.DirectorySeparatorChar + fileName; //create the material in the project and set the shader material to shaderToAtlas Material atlasMaterial = new Material(Shader.Find(shaderToAtlas)); //save the material to the project view AssetDatabase.CreateAsset(atlasMaterial, atlasTexturePath + "Mat.mat"); AssetDatabase.ImportAsset(atlasTexturePath + "Mat.mat"); //load a reference from the project view to the material (this is done to be able to set the texture to the material in the project view) atlasMaterial = (Material)AssetDatabase.LoadAssetAtPath(atlasTexturePath + "Mat.mat", typeof(Material)); List <string> shaderDefines = ShaderManager.Instance.GetShaderTexturesDefines(shaderToAtlas); for (int k = 0; k < shaderDefines.Count; k++) //go trough each property of the shader. { List <Texture2D> texturesOfShader = ObjSorter.GetTexturesToAtlasForShaderDefine(shaderIndex, shaderDefines[k]); //Get thtextures for the property shderDefines[k] to atlas them List <Vector2> scales = ObjSorter.GetScalesToAtlasForShaderDefine(shaderIndex, shaderDefines[k]); List <Vector2> offsets = ObjSorter.GetOffsetsToAtlasForShaderDefine(shaderIndex, shaderDefines[k]); if (AdvancedMenuGUI.Instance.ReuseTextures) { texturesOfShader = Utils.FilterTexsByIndex(texturesOfShader, textureReuseManager.GetTextureIndexes()); scales = Utils.FilterVec2ByIndex(scales, textureReuseManager.GetTextureIndexes()); offsets = Utils.FilterVec2ByIndex(offsets, textureReuseManager.GetTextureIndexes()); } generatedAtlas.SaveAtlasToFile(atlasTexturePath + k.ToString() + ".png", texturesOfShader, scales, offsets); //save the atlas with the retrieved textures AssetDatabase.ImportAsset(atlasTexturePath + k.ToString() + ".png"); Texture2D tex = (Texture2D)AssetDatabase.LoadAssetAtPath(atlasTexturePath + k.ToString() + ".png", typeof(Texture2D)); atlasMaterial.SetTexture(shaderDefines[k], //set property shderDefines[k] for shader shaderToAtlas tex); } return(atlasMaterial); }