Skip to content

Instantly share code, notes, and snippets.

@FREEZX
Created March 18, 2023 11:02
Show Gist options
  • Select an option

  • Save FREEZX/eaeda3f5cf489ce4fdfab60437492618 to your computer and use it in GitHub Desktop.

Select an option

Save FREEZX/eaeda3f5cf489ce4fdfab60437492618 to your computer and use it in GitHub Desktop.
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