/// <summary> /// Fetch the grass set from the project-based file /// Populate the list of grass configurations /// Sort the grass config list /// Populate the source list /// </summary> private void GetGrassSettings() { if (lbGrassSetup == null) { lbGrassSetup = new LBGrassSetup(); } if (lbGrassSetup != null) { EditorUtility.DisplayProgressBar("Loading grass configurations", "Please Wait", 0f); lbGrassSetup.Retrieve(); if (lbGrassSetup.lbGrassConfigList != null) { int numGrassConfigs = lbGrassSetup.lbGrassConfigList.Count; EditorUtility.ClearProgressBar(); // Get a list of the Texture2D assets for (int i = 0; i < numGrassConfigs; i++) { LBGrassConfig lbGrassConfig = lbGrassSetup.lbGrassConfigList[i]; EditorUtility.DisplayProgressBar("Loading grass settings", "Please Wait", i + 1 / numGrassConfigs); if (lbGrassConfig != null) { lbGrassConfig.texture2D = (Texture2D)AssetDatabase.LoadAssetAtPath(lbGrassConfig.grassTexturePath, typeof(Texture2D)); #if LBGrassEditorAdmin if (lbGrassConfig.texture2D == null) { Debug.Log("LBGrassEditor.GetGrassSettings " + lbGrassConfig.grassTexturePath + " not installed"); } #endif //Debug.Log("GetGrassSettings: " + lbGrassConfig.grassTexturePath); } } // Sort the list by grass texture name lbGrassSetup.lbGrassConfigList.Sort(delegate(LBGrassConfig grass1, LBGrassConfig grass2) { return(grass1.grassTextureName.CompareTo(grass2.grassTextureName)); }); } lbGrassSetup.PopulateSourceList(); EditorUtility.ClearProgressBar(); } }
/// <summary> /// This will import grass settings from the latest LB version file /// which is located in Assets/LandscapeBuilder/Setup folder and gets /// updated with newer versions of LB. /// WARNING: It will overwrite any user settings made in this Editor /// </summary> private void UpgradeGrassSettings() { // Need to load the textures first. GetGrassSettings(); // Import the file from the Assets folder which was downloaded with the latest package of LB LBGrassSetup lbGrassSetupUpgrade = new LBGrassSetup(); if (lbGrassSetupUpgrade != null) { lbGrassSetupUpgrade.Retrieve(true); if (lbGrassSetupUpgrade.lbGrassConfigList != null) { int numConfigsToUpgrade = lbGrassSetupUpgrade.lbGrassConfigList.Count; int upgradingCount = 0; int numAdded = 0; int numUpgraded = 0; int numSkipped = 0; float upgradeProgress = 0f; string progressMsg = string.Empty; string textureFolder = string.Empty; // Loop throught the new grass configs foreach (LBGrassConfig lbGrassConfigUpgrade in lbGrassSetupUpgrade.lbGrassConfigList) { if (lbGrassConfigUpgrade != null) { allowRepaint = true; progressMsg = "Upgrading " + (upgradingCount++).ToString() + " of " + numConfigsToUpgrade.ToString() + " ... Please wait"; upgradeProgress = upgradingCount / numConfigsToUpgrade; if (EditorUtility.DisplayCancelableProgressBar("Upgrading Grass Configurations", progressMsg, upgradeProgress)) { break; } // Find a match in the current config list (used in the LandscapeBuilderGrassSelector) LBGrassConfig lbGrassconfigCurrent = lbGrassSetup.lbGrassConfigList.Find(g => g.grassTextureName == lbGrassConfigUpgrade.grassTextureName && g.sourceName == lbGrassConfigUpgrade.sourceName); // Is this a new configuration to be added? if (lbGrassconfigCurrent == null) { LBGrassConfig newGrassConfig = new LBGrassConfig(); if (newGrassConfig != null) { // default configuration if (!string.IsNullOrEmpty(lbGrassConfigUpgrade.grassTextureName)) { newGrassConfig = new LBGrassConfig(lbGrassConfigUpgrade); // Select the correct folder based on the source of the LBGrassConfig (i.e. which Unity package is it from) if (lbGrassConfigUpgrade.sourceName == sourceNameHQPhotoPackVol1) { textureFolder = "Assets/" + pathHQPhotoPackVol1; } else if (lbGrassConfigUpgrade.sourceName == sourceNameHQPhotoPackVol2) { textureFolder = "Assets/" + pathHQPhotoPackVol2; } else if (lbGrassConfigUpgrade.sourceName == sourceNameRusticGrass) { textureFolder = "Assets/" + pathRusticGrass; } else { textureFolder = "unknown"; } // The paths may not match the current project //string[] lookFor = new string[] { textureFolder }; if (textureFolder == "unknown") { numSkipped++; } else { //string shortName = lbGrassConfigUpgrade.grassTextureName.Substring(0, lbGrassConfigUpgrade.grassTextureName.LastIndexOf('.')); // If the folder doesn't exist this typically means it is not installed in the project, so use the default path. if (AssetDatabase.IsValidFolder(textureFolder)) { //Debug.Log("valid: " + textureFolder); // TODO - LB Grass Editor - get the correct project folder path when upgrading rather than using the default. // Currently this can return multiple textures for names that have a space in them. For now // we're going to use the default path... //string[] textureGUIDArray = AssetDatabase.FindAssets(shortName + " t:texture2D", lookFor); //if (textureGUIDArray != null) //{ // foreach (string guidstr in textureGUIDArray) // { // string pathToTexture2D = AssetDatabase.GUIDToAssetPath(guidstr); // //Debug.Log(" tx: " + pathToTexture2D); // } //} } //Debug.Log("upgrade path: " + lbGrassConfigUpgrade.grassTexturePath); } lbGrassSetup.lbGrassConfigList.Add(newGrassConfig); numAdded++; } } } else { // Existing configuration - so update settings lbGrassconfigCurrent.grassPatchFadingMode = lbGrassConfigUpgrade.grassPatchFadingMode; lbGrassconfigCurrent.healthyColour = lbGrassConfigUpgrade.healthyColour; lbGrassconfigCurrent.dryColour = lbGrassConfigUpgrade.dryColour; lbGrassconfigCurrent.detailRenderMode = lbGrassConfigUpgrade.detailRenderMode; lbGrassconfigCurrent.minWidth = lbGrassConfigUpgrade.minWidth; lbGrassconfigCurrent.maxWidth = lbGrassConfigUpgrade.maxWidth; lbGrassconfigCurrent.minHeight = lbGrassConfigUpgrade.minHeight; lbGrassconfigCurrent.maxHeight = lbGrassConfigUpgrade.maxHeight; // Update the texture settings - only update installed textures if (lbGrassconfigCurrent.texture2D != null) { #if UNITY_5_5_OR_NEWER LBTextureOperations.SetTextureAttributes(lbGrassconfigCurrent.texture2D, TextureImporterCompression.CompressedHQ, FilterMode.Bilinear, false, 0, true); #else LBTextureOperations.SetTextureAttributes(lbGrassconfigCurrent.texture2D, TextureImporterFormat.AutomaticCompressed, FilterMode.Bilinear, false, 0, true); #endif } numUpgraded++; } } } // Save the updates if (lbGrassSetup != null) { lbGrassSetup.Save(); } Debug.Log("Landscape Builder Grass Editor: Added " + numAdded.ToString() + " new grass configurations added. " + numUpgraded.ToString() + " updated. " + numSkipped.ToString() + " skipped."); allowRepaint = false; // Refresh the on-screen list GetGrassSettings(); EditorUtility.ClearProgressBar(); } } }