private void OnEnable()
        {
            if (lbGrassSetup == null)
            {
                lbGrassSetup = new LBGrassSetup();
            }

            pathHQPhotoPackVol1 = LBLandscape.GetPath(LBSavedData.PathType.HQPhotographicTexturesVol1);
            pathHQPhotoPackVol2 = LBLandscape.GetPath(LBSavedData.PathType.HQPhotographicTexturesVol2);
            pathRusticGrass     = LBLandscape.GetPath(LBSavedData.PathType.RusticGrass);

            // If the paths aren't set, set them to the default. Then check if they exist in the current project
            if (string.IsNullOrEmpty(pathHQPhotoPackVol1))
            {
                pathHQPhotoPackVol1 = LBSavedData.GetHQPhotographicTexturesVol1DefaultPath;
            }
            if (string.IsNullOrEmpty(pathHQPhotoPackVol1))
            {
                isHQPhotoPackVol1Installed = false;
            }
            else
            {
                isHQPhotoPackVol1Installed = AssetDatabase.IsValidFolder("Assets/" + pathHQPhotoPackVol1);
            }

            if (string.IsNullOrEmpty(pathHQPhotoPackVol2))
            {
                pathHQPhotoPackVol2 = LBSavedData.GetHQPhotographicTexturesVol2DefaultPath;
            }
            if (string.IsNullOrEmpty(pathHQPhotoPackVol2))
            {
                isHQPhotoPackVol2Installed = false;
            }
            else
            {
                isHQPhotoPackVol2Installed = AssetDatabase.IsValidFolder("Assets/" + pathHQPhotoPackVol2);
            }

            if (string.IsNullOrEmpty(pathRusticGrass))
            {
                pathRusticGrass = LBSavedData.GetRusticGrassDefaultPath;
            }
            if (string.IsNullOrEmpty(pathRusticGrass))
            {
                isRusticGrassInstalled = false;
            }
            else
            {
                isRusticGrassInstalled = AssetDatabase.IsValidFolder("Assets/" + pathRusticGrass);
            }

            sourceNameHQPhotoPackVol1 = LBSavedData.GetHQPhotographicTexturesVol1SourceName;
            sourceNameHQPhotoPackVol2 = LBSavedData.GetHQPhotographicTexturesVol2SourceName;
            sourceNameRusticGrass     = LBSavedData.GetRusticGrassSourceName;
        }
        /// <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();
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Used to pass in the script to the selector so that it
        /// can return the LBGrassConfig item selected.
        /// </summary>
        /// <param name="lbWindow"></param>
        public void Initialize(LandscapeBuilderWindow lbWindow, List <LBGrassConfig> lbGrassConfigList)
        {
            callingLBWindow = lbWindow;
            if (lbGrassSetup == null)
            {
                lbGrassSetup = new LBGrassSetup();
            }

            if (lbGrassSetup != null)
            {
                if (lbGrassSetup.lbGrassConfigList == null)
                {
                    lbGrassSetup.lbGrassConfigList = new List <LBGrassConfig>(lbGrassConfigList);
                }

                if (lbGrassSetup.lbGrassConfigList != null)
                {
                    isStartedLoading = true;
                    isLoading        = true;

                    if (lbGrassSetup.lbGrassConfigList != null)
                    {
                        int grassConfigInt  = 0;
                        int numGrassConfigs = lbGrassSetup.lbGrassConfigList.Count;

                        // Get a list of the Texture2D assets
                        // This could be quite memory hungry...
                        foreach (LBGrassConfig lbGrassConfig in lbGrassSetup.lbGrassConfigList)
                        {
                            EditorUtility.DisplayProgressBar("Loading grasses", "Please Wait", (grassConfigInt++) / numGrassConfigs);
                            lbGrassConfig.texture2D            = (Texture2D)AssetDatabase.LoadAssetAtPath(lbGrassConfig.grassTexturePath, typeof(Texture2D));
                            lbGrassConfig.texture2DAlternative = (Texture2D)AssetDatabase.LoadAssetAtPath(lbGrassConfig.grassTextureAlternativePath, typeof(Texture2D));
                        }

                        // 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();
                        isLoading = false;
                    }
                }
            }
        }
        /// <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();
                }
            }
        }
Ejemplo n.º 5
0
    public void Retrieve(bool useAssetFolder = false)
    {
        // Default path - this is the working copy of LBGrassSetup and is used to edit settings
        // and is used by the user via the LandscapeBuilderGrassSelector
        string pathLBGrassSetup = "LandscapeBuilder/LBGrassSetup.dat";

        // The Asset folder stores the latest setup file downloaded with the latest build of LB.
        // This should only be used under special circumstances by like when updating the primary
        // LBGrassSetup.dat file located at LandscapeBuilder/LBGrassSetup.dat
        if (useAssetFolder)
        {
            pathLBGrassSetup = "Assets/LandscapeBuilder/Setup/LBGrassSetup.dat";
        }

        bool isFileFound = File.Exists(pathLBGrassSetup);

        if (!useAssetFolder && !isFileFound)
        {
            // Attempt to restore file from Asset folder
            LBSetup.SetupGrass();
            isFileFound = File.Exists(pathLBGrassSetup);
        }

        if (isFileFound)
        {
            BinaryFormatter binaryFormatter = null;
            FileStream      fs = null;
            try
            {
                // Read the binary data from file in the application default data folder
                binaryFormatter = new BinaryFormatter();

                fs = File.Open(pathLBGrassSetup, FileMode.Open);
                LBGrassSetup tmplbGrassSetup = (LBGrassSetup)binaryFormatter.Deserialize(fs);
                fs.Close();
                if (tmplbGrassSetup == null)
                {
                    Debug.Log("INFO: LBGrassSetup.Retrieve tmplbGrassSetup is null");
                }
                else
                {
                    if (tmplbGrassSetup.lbGrassConfigList == null)
                    {
                        Debug.LogWarning("INFO: LBGrassSetup.Retrieve lbGrassConfigList is empty");
                    }
                    else
                    {
                        // Create a new list if it doesn't already exist
                        if (lbGrassConfigList == null)
                        {
                            lbGrassConfigList = new List <LBGrassConfig>();
                        }
                        // Remove anything from the list
                        lbGrassConfigList.Clear();
                        lbGrassConfigList.AddRange(tmplbGrassSetup.lbGrassConfigList);
                        //Debug.Log("LBGrassSetup.Retrieve lbGrassConfigList " + tmplbGrassSetup.lbGrassConfigList.Count.ToString() + " retrieved. Set: " + lbGrassConfigList.Count.ToString());
                    }
                }
            }
            catch (Exception ex)
            {
                Debug.Log("LBGrassSetup.Retrieve Exception - " + ex.Message + " Path: " + pathLBGrassSetup);
            }
            finally
            {
                // Cleanup
                if (binaryFormatter != null)
                {
                    binaryFormatter = null;
                }
                if (fs != null)
                {
                    fs.Close(); fs.Dispose(); fs = null;
                }
            }
        }
        else
        {
            Debug.LogWarning("INFO: LBGrassSetup.Retrieve(" + useAssetFolder.ToString() + ") - could not find " + pathLBGrassSetup);
        }
    }