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);
        }
Esempio n. 2
0
    void DoAddPersistentStorage(VegetationSystem _vegetationSystem)
    {
        _vegetationSystem.AutomaticWakeup = true;
        // NB seems redundant to recurse into parent object - but this is needed to get the terrain name
        GameObject parentTerrainGo = _vegetationSystem.gameObject.transform.parent.gameObject;
        PersistentVegetationStorage _persistentVegetationStorage = parentTerrainGo.GetComponentInChildren <PersistentVegetationStorage>();

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

        AssetDatabase.CreateAsset(newPersistentVegetationStoragePackage, _assetsPath + "/" + parentTerrainGo.name + _DEFAULT_SUFFIX + ".asset");
        _persistentVegetationStorage.PersistentVegetationStoragePackage = newPersistentVegetationStoragePackage;

        // initialize
        _persistentVegetationStorage.InitializePersistentStorage();

        if (_debugToLog)
        {
            Debug.Log("Added storage asset for " + parentTerrainGo.name);
        }

        if (_autoBake)
        {
            if (_debugToLog)
            {
                Debug.Log("Auto Baking " + parentTerrainGo.name);
            }
            //if (_vegetationSystem.GetSleepMode())
            //{
            _vegetationSystem.SetSleepMode(false);     // wake up VegetationSystem, needed for prefab
            //}
            // now bake
            List <string> vegetationItemIdList =
                VegetationPackageEditorTools.CreateVegetationInfoIdList(_persistentVegetationStorage.VegetationSystem.CurrentVegetationPackage);


            for (int i = 0; i <= vegetationItemIdList.Count - 1; i++)
            {
                _persistentVegetationStorage.BakeVegetationItem(vegetationItemIdList[i]);
            }
            _persistentVegetationStorage.VegetationSystem.DelayedClearVegetationCellCache();
            EditorUtility.SetDirty(_persistentVegetationStorage.PersistentVegetationStoragePackage);
            EditorUtility.SetDirty(_persistentVegetationStorage.VegetationSystem.CurrentVegetationPackage);
            if (_debugToLog)
            {
                Debug.Log("Finished Baking for " + parentTerrainGo.name);
            }
        }
    }
        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>();
        }