public VegetationSystem CopyVegetationSystem(GameObject srcGameObj, Terrain terrain)
        {
            GameObject vegetationSystemObject = GameObject.Instantiate(srcGameObj);

            vegetationSystemObject.transform.SetParent(terrain.transform);

            VegetationSystem vegetationSystem = vegetationSystemObject.GetComponent <VegetationSystem>();

            if (vegetationSystem == null)
            {
                vegetationSystem = vegetationSystemObject.AddComponent <VegetationSystem>();
            }

            if (package != null)
            {
                vegetationSystem.AddVegetationPackage(package, true);
            }

            if (vegetationSystemObject.GetComponent <TerrainSystem>() == null)
            {
                vegetationSystemObject.AddComponent <TerrainSystem>();
            }

            BillboardSystem billboardSystem = vegetationSystemObject.GetComponent <BillboardSystem>();

            if (billboardSystem == null)
            {
                billboardSystem = vegetationSystemObject.AddComponent <BillboardSystem>();
            }
            billboardSystem.VegetationSystem = vegetationSystem;

            PersistentVegetationStorage srcStorage = vegetationSystemObject.GetComponent <PersistentVegetationStorage>();

            if (srcStorage != null)
            {
                GameObject.DestroyImmediate(srcStorage);
            }

            PersistentVegetationStorage persistentVegetationStorage = vegetationSystemObject.AddComponent <PersistentVegetationStorage>();

            persistentVegetationStorage.VegetationSystem = vegetationSystem;
            vegetationSystem.AutoselectTerrain           = false;
            vegetationSystem.currentTerrain = terrain;
            //vegetationSystem.AutomaticWakeup = true;

            PersistentVegetationStoragePackage persistentVegetationStoragePackage = ScriptableObject.CreateInstance <PersistentVegetationStoragePackage>();

            persistentVegetationStorage.PersistentVegetationStoragePackage         = persistentVegetationStoragePackage;
            persistentVegetationStorage.AutoInitPersistentVegetationStoragePackage = true;

            vegetationSystem.SetSleepMode(false);

            return(vegetationSystem);
        }
Exemple #2
0
    //v1 just deletes all and rebakes
    //void ReBakeAllStorage()
    //{
    //    ClearAllVegetationSystems();

    //    if (_usePrefab)
    //    {
    //        SetupFromPrefab();
    //    }
    //    else
    //    {
    //        SetupFromVegetationPackage();
    //    }

    //}



    void AddAdditionalPackage()
    {
        // do a rebake of existing persistent storage packages.
        // Not quite working as expected - adds additional VegetationPackages to the VegetationSystem component, rather than replacing the existing one
        // Also, buggy?... seems to work first time but not on subsequent rebakes (baked storage ends up empty).


        // take care of storage (path)
        _assetsPath = RemoveTrailingSlash(_assetsPath); // ensures that there isn't an ending slash

        if (!AssetDatabase.IsValidFolder(_assetsPath))
        {
            if (_debugToLog)
            {
                Debug.Log("Asset path is invalid. Creating your folders");
            }
            CreateAssetFolders();
        }

        ClearAllStorageInPath();



        if (_vegetationPackage == null)
        {
            Debug.LogWarning("You need to assign a vegetation package");
            EditorUtility.DisplayDialog("No Vegetation Package",
                                        "You need to drag and drop a vegetation package into the utility window", "OK");
            return;
        }
        if (_forceItemsEnabled)     // Make sure all Vegetation is enabled on Vegetation Package
        {
            SetAllItemsToRuntimeSpawn();
        }


        var terrains = _rootObject ? _rootObject.GetComponentsInChildren <Terrain>() : GameObject.FindObjectsOfType <Terrain>();
        int totalNum = terrains.Length;
        int counter  = 0;

        foreach (Terrain terrain in terrains)
        {
            counter++;
            if (_addPersistentStorage)
            {
                // first check if a Vegetation System already exists
                VegetationSystem _vegetationSystem = terrain.gameObject.GetComponentInChildren <VegetationSystem>();
                if (_vegetationSystem == null)
                {     // add if not already there
                    _vegetationSystem = VegetationStudioManager.AddVegetationSystemToTerrain(terrain, _vegetationPackage, true);
                    if (_debugToLog)
                    {
                        Debug.Log("Rebaking: Added missing VS to " + terrain.name + " (" + counter + " of " + totalNum + ")");
                    }
                }
                else
                {
                    if (_debugToLog)
                    {
                        Debug.Log("Rebaking VS already existed on " + terrain.name + " (" + counter + " of " + totalNum + ")");
                    }
                    _vegetationSystem.AddVegetationPackage(_vegetationPackage, true);
                }

                if (_debugToLog)
                {
                    Debug.Log("Adding Persistent Storage " + terrain.name + " (" + counter + " of " + totalNum + ")");
                }
                DoAddPersistentStorage(_vegetationSystem);
            }
            else
            {
                // just add a Vegetation System
                VegetationStudioManager.AddVegetationSystemToTerrain(terrain, _vegetationPackage);
            }
        }


        EditorSceneManager.MarkSceneDirty(EditorSceneManager.GetActiveScene());
    }