public static void SetupTerrain(Terrain terrain, ref VegetationPackage package, out VegetationSystem system, out PersistentVegetationStorage storage)
        {
            var vetSystems          = GetVegetationSystemsForTerrain(terrain);
            VegetationSystem vetSys = null;

            for (var i = 0; i < vetSystems.Count; ++i)
            {
                var candidate = vetSystems[i];
                if (candidate.CurrentVegetationPackage == package)
                {
                    vetSys = candidate;
                    break;
                }
            }

            if (vetSys == null)
            {
                vetSys = VegetationStudioManager.AddVegetationSystemToTerrain(terrain, package, createPersistentVegetationStoragePackage: true);
            }
            if (package != null)
            {
                if (vetSys.VegetationPackageList.Count == 0)
                {
                    vetSys.VegetationPackageList.Add(package);
                }
                if (vetSys.VegetationPackageList.Count == 1 && vetSys.VegetationPackageList[0] == null)
                {
                    vetSys.VegetationPackageList[0] = package;
                }
                if (!vetSys.VegetationPackageList.Contains(package))
                {
                    vetSys.VegetationPackageList.Add(package);
                }
            }
            if (!vetSys.InitDone)
            {
                vetSys.SetupVegetationSystem();
                vetSys.RefreshVegetationPackage();
            }
            vetSys.SetSleepMode(false);
            package = vetSys.CurrentVegetationPackage;
            system  = vetSys;
            storage = vetSys.GetComponent <PersistentVegetationStorage>();
        }
    void AssignTerrainToVS(Terrain terrain, VegetationSystem vs, VegetationPackage vegetationPackage, PersistentVegetationStoragePackage storagePackage)
    {
        vs.AutoselectTerrain = false;

        vs.CurrentVegetationPackage = vegetationPackage;
        PersistentVegetationStorage storage = vs.GetComponent <PersistentVegetationStorage>();

        if (storage != null)
        {
            storage.SetPersistentVegetationStoragePackage(storagePackage);
        }
        if (vs.InitDone)
        {
            vs.HotswapTerrain(terrain, new UnityTerrainData(terrain));
        }
        else
        {
            vs.SetTerrain(terrain);
        }
    }
        public static IEnumerator Apply(CoordRect rect, Terrain terrain, object dataBox, Func <float, bool> stop = null)
        {
            Profiler.BeginSample("VS Apply");

                        #if VEGETATION_STUDIO
            //clear cache
            VegetationSystem vetSys = terrain.gameObject.GetComponentInChildren <VegetationSystem>();
            for (int i = 0; i < vetSys.VegetationCellList.Count; i++)
            {
                vetSys.VegetationCellList[i].ClearCache();
            }

            vetSys.UnityTerrainData = new UnityTerrainData(terrain, false, false);

            //refresh billboards
            BillboardSystem billboardSystem = vetSys.GetComponent <BillboardSystem>();
            billboardSystem.RefreshBillboards();

            Profiler.EndSample();
                        #endif

            yield return(null);
        }