58 lines
1.3 KiB
C#
Raw Normal View History

2024-03-16 20:34:22 +03:00
using System;
2024-02-19 21:00:36 +03:00
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Mirror;
public class CellSpawner : NetworkBehaviour
{
public GameObject prefab;
public float spawnTime;
2024-03-20 21:17:05 +03:00
private int _maxConnections;
2024-03-16 20:34:22 +03:00
private CustomNetworkManager _networkManager;
2024-02-19 21:00:36 +03:00
private GameObject _instance;
2024-03-16 20:34:22 +03:00
private bool _spawnStarted = false;
2024-02-19 21:00:36 +03:00
private float _timer;
2024-03-16 20:34:22 +03:00
2024-02-19 21:00:36 +03:00
void Start()
{
if (!isServer) return;
2024-03-16 21:11:37 +03:00
_networkManager = NetworkManager.singleton.GetComponent<CustomNetworkManager>();
2024-03-17 00:30:59 +03:00
_maxConnections = _networkManager.maxConnections;
2024-02-19 21:00:36 +03:00
SpawnPref();
}
void FixedUpdate()
{
2024-03-17 00:30:59 +03:00
if (!isServer || _maxConnections <= 0) return;
2024-02-19 21:00:36 +03:00
2024-03-16 20:34:22 +03:00
if (_instance == null && _spawnStarted == false)
2024-02-19 21:00:36 +03:00
{
2024-03-16 20:34:22 +03:00
_spawnStarted = true;
2024-02-19 21:00:36 +03:00
}
2024-03-16 20:34:22 +03:00
if (_spawnStarted)
2024-02-19 21:00:36 +03:00
{
_timer += Time.fixedDeltaTime;
}
if (_timer >= 10)
{
_timer = 0;
SpawnPref();
}
}
2024-03-17 00:30:59 +03:00
2024-02-19 21:00:36 +03:00
void SpawnPref()
{
_instance = Instantiate(prefab, transform.position + transform.up * 1.2f, Quaternion.identity); //* 1.8f
2024-03-21 18:28:37 +03:00
_instance.transform.SetParent(transform, false);
2024-02-19 21:00:36 +03:00
NetworkServer.Spawn(_instance);
2024-03-16 20:34:22 +03:00
_spawnStarted = false;
2024-03-17 00:30:59 +03:00
_maxConnections--;
2024-02-19 21:00:36 +03:00
}
}