Created
March 18, 2023 11:02
-
-
Save FREEZX/eaeda3f5cf489ce4fdfab60437492618 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System.Collections; | |
| using System.Collections.Generic; | |
| using FishNet; | |
| using FishNet.Object; | |
| using FishNet.Utility.Performance; | |
| using UnityEngine; | |
| public class ReproScript : NetworkBehaviour | |
| { | |
| public GameObject spawnObject; | |
| public uint SpawnInterval; | |
| List<NetworkObject> spawned = new List<NetworkObject>(); | |
| public override void OnStartNetwork() | |
| { | |
| base.OnStartNetwork(); | |
| // Prewarm pool | |
| PrewarmPools(); | |
| } | |
| void PrewarmPools() { | |
| DefaultObjectPool pool = InstanceFinder.NetworkManager.GetComponent<DefaultObjectPool>(); | |
| pool.CacheObjects(spawnObject.GetComponent<NetworkObject>(), 100, IsServer); | |
| } | |
| public override void OnStartServer() | |
| { | |
| base.OnStartServer(); | |
| this.TimeManager.OnTick += Tick; | |
| } | |
| void Tick() { | |
| // Spawn objects on SpawnInterval ticks | |
| if(IsServer && (TimeManager.Tick % SpawnInterval == 0)) { | |
| NetworkObject instance = NetworkManager.GetPooledInstantiated(spawnObject.GetComponent<NetworkObject>(), true); | |
| instance.transform.position = new Vector3(100, 0, 0); | |
| InstanceFinder.ServerManager.Spawn(instance); | |
| spawned.Add(instance); | |
| } | |
| } | |
| void Update() { | |
| for (int i = 0; i < spawned.Count; ++i) { | |
| spawned[i].transform.position = spawned[i].transform.position - Vector3.right * Time.deltaTime * 30; | |
| if(spawned[i].transform.position.x < 0) { | |
| Despawn(spawned[i], DespawnType.Pool); | |
| spawned.RemoveAt(i); | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment