/// <summary>
        /// Find the TextureArrayConfig for a given MicroSplat terrain material.
        /// This assumes it is always in the same project folder
        /// </summary>
        /// <param name="terrainMat"></param>
        /// <returns></returns>
        public static JBooth.MicroSplat.TextureArrayConfig GetTextureArrayConfig(Material terrainMat)
        {
            JBooth.MicroSplat.TextureArrayConfig textureArrayConfig = null;

            if (terrainMat != null)
            {
                string folder = GetMicroSplatDataFolder(terrainMat, true);
                textureArrayConfig = LBEditorHelper.GetAsset <JBooth.MicroSplat.TextureArrayConfig>(folder, terrainMat.name.Replace("MicroSplat", "MicroSplatConfig") + ".asset");
            }
            return(textureArrayConfig);
        }
        /// <summary>
        /// Find the TextureArrayConfig for a given MicroSplat terrain material.
        /// This assumes it is in the same project folder. If it is not in the
        /// default folder, it also checks the terrainData folder which can
        /// happen for imported terrains.
        /// </summary>
        /// <param name="landscape"></param>
        /// <param name="terrainMat"></param>
        /// <returns></returns>
        public static JBooth.MicroSplat.TextureArrayConfig GetTextureArrayConfig(LBLandscape landscape, Material terrainMat)
        {
            JBooth.MicroSplat.TextureArrayConfig textureArrayConfig = null;

            if (terrainMat != null)
            {
                string folder = GetMicroSplatDataFolder(terrainMat, true);

                textureArrayConfig = LBEditorHelper.GetAsset <JBooth.MicroSplat.TextureArrayConfig>(folder, terrainMat.name.Replace("MicroSplat", "MicroSplatConfig") + ".asset");

                // Check the terrain data folder if 1) The material is stored in the scene AND 2) it is not in the default location
                if (textureArrayConfig == null && landscape != null && landscape.useProjectForTerrainData)
                {
                    //Debug.Log("[DEBUG] GetTextureArrayConfig checking: " + landscape.terrainDataFolder + " rather than " + folder);
                    folder             = landscape.terrainDataFolder + "/MicroSplatData";
                    textureArrayConfig = LBEditorHelper.GetAsset <JBooth.MicroSplat.TextureArrayConfig>(folder, terrainMat.name.Replace("MicroSplat", "MicroSplatConfig") + ".asset");
                }
            }
            return(textureArrayConfig);
        }
        /// <summary>
        /// Copy LB Texturing tab Textures to MicroSplat.
        /// Typically called from within the Texturing tab of LB Editor window.
        /// </summary>
        /// <param name="landscape"></param>
        /// <param name="terrainMaterialType"></param>
        /// <param name="terrainMat"></param>
        /// <param name="showErrors"></param>
        public static void MicroSplatCopyTextures(LBLandscape landscape, LBLandscape.TerrainMaterialType terrainMaterialType, Material terrainMat, bool showErrors)
        {
            string methodName = "LBEditorIntegration.MicroSplatCopyTextures";

            // Basic validation
            if (landscape == null)
            {
                if (showErrors)
                {
                    Debug.LogWarning(methodName + " - landscape cannot be null");
                }
            }
            else if (terrainMaterialType != LBLandscape.TerrainMaterialType.MicroSplat)
            {
                if (showErrors)
                {
                    Debug.LogWarning(methodName + " - The Landscape Terrain Settings needs to have a Material Type of MicroSplat. Please set and try again.");
                }
            }
            else if (terrainMat == null)
            {
                if (showErrors)
                {
                    Debug.LogWarning(methodName + " - the landscape does not appear to have a MicroSplat material. Has it been initialised in the Landscape Terrain Settings?");
                }
            }
            else
            {
                // Find the Texture Array Config file
                //JBooth.MicroSplat.TextureArrayConfig textureArrayConfig = LBEditorHelper.GetAsset<JBooth.MicroSplat.TextureArrayConfig>("MicroSplatData", terrainMat.name.Replace("MicroSplat","MicroSplatConfig") + ".asset");
                JBooth.MicroSplat.TextureArrayConfig textureArrayConfig = GetTextureArrayConfig(terrainMat);
                if (textureArrayConfig == null)
                {
                    if (showErrors)
                    {
                        Debug.LogWarning("ERROR: " + methodName + " could not find TextureArrayConfig [MicroSplatData/" + terrainMat.name.Replace("MicroSplat", "MicroSplatConfig") + ".asset]");
                    }
                }
                else
                {
                    // Get existing list of Textures from MicroSplat
                    List <JBooth.MicroSplat.TextureArrayConfig.TextureEntry> textureOldEntryList = textureArrayConfig.sourceTextures;
                    //int numMSTextures = (textureOldEntryList == null ? 0 : textureOldEntryList.Count);
                    int numLBTextures         = (landscape.terrainTexturesList == null ? 0 : landscape.terrainTexturesList.Count);
                    LBTerrainTexture lbTrnTex = null;
                    JBooth.MicroSplat.TextureArrayConfig.TextureEntry textureEntry = null;

                    //Debug.Log("[DEBUG] numMSTextures: " + numMSTextures + " numLBTextures:" + numLBTextures);

                    // Create a new list
                    List <JBooth.MicroSplat.TextureArrayConfig.TextureEntry> textureNewEntryList = new List <JBooth.MicroSplat.TextureArrayConfig.TextureEntry>();

                    // Loop through all the Landscape Builder Textures
                    for (int lbTexIdx = 0; lbTexIdx < numLBTextures; lbTexIdx++)
                    {
                        lbTrnTex = landscape.terrainTexturesList[lbTexIdx];

                        if (lbTrnTex != null && !lbTrnTex.isDisabled)
                        {
                            // Find matching MicroSplat texture entry
                            textureEntry = textureOldEntryList.Find(te => (lbTrnTex.texture != null && te.diffuse != null && te.diffuse.name == lbTrnTex.texture.name) &&
                                                                    (lbTrnTex.normalMap == null && te.normal == null || (lbTrnTex.normalMap != null && te.normal != null && te.normal.name == lbTrnTex.normalMap.name)) &&
                                                                    (lbTrnTex.heightMap == null && te.height == null || (lbTrnTex.heightMap != null && te.height != null && te.height.name == lbTrnTex.heightMap.name))
                                                                    );
                            //textureEntry = textureOldEntryList.Find(te => lbTrnTex.texture != null && te.diffuse != null && te.diffuse.name == lbTrnTex.texture.name);

                            if (textureEntry != null)
                            {
                                // update tinted texture if required
                                textureEntry.diffuse = lbTrnTex.isTinted ? (lbTrnTex.tintedTexture == null ? lbTrnTex.texture : lbTrnTex.tintedTexture) : lbTrnTex.texture;

                                textureNewEntryList.Add(textureEntry);
                                //Debug.Log("[DEBUG] Added matching entry: " + textureEntry.diffuse.name);
                            }
                            else
                            {
                                // Add missing textures
                                textureEntry = new JBooth.MicroSplat.TextureArrayConfig.TextureEntry();
                                if (textureEntry != null)
                                {
                                    textureEntry.diffuse = lbTrnTex.isTinted ? (lbTrnTex.tintedTexture == null ? lbTrnTex.texture : lbTrnTex.tintedTexture) : lbTrnTex.texture;
                                    textureEntry.normal  = lbTrnTex.normalMap;
                                    textureEntry.height  = lbTrnTex.heightMap;
                                    textureNewEntryList.Add(textureEntry);

                                    //Debug.Log("[DEBUG] Adding new entry: " + lbTrnTex.texture.name);
                                }
                            }

                            // In MicroSplat the first splatprototype sets the default tilesize
                            if (lbTexIdx == 0)
                            {
                                // Convert UVs to MicroSplat tiling
                                Vector3 terrainSize = landscape.GetLandscapeTerrainSize();
                                // scale, offset (default offset to 0,0)
                                terrainMat.SetVector("_UVScale", new Vector4(1.0f / (lbTrnTex.tileSize.x / terrainSize.x), 1.0f / (lbTrnTex.tileSize.y / terrainSize.z), 0f, 0f));
                            }
                            //Texture2DArray diff = textureArrayConfig.GetTexture("_Diffuse") as Texture2DArray;
                        }
                    }

                    // Update the list of source textures in MicroSplat
                    textureArrayConfig.sourceTextures = textureNewEntryList;
                    staticMSTextureArrayConfig        = textureArrayConfig;
                    // Gets called once only, after all the Inspectors have been updated
                    EditorApplication.delayCall += MicroSplatDelayedCompileConfig;
                }
            }
        }