add unity project for networking
This commit is contained in:
@@ -0,0 +1,26 @@
|
||||
using System.Collections.Generic;
|
||||
using Unity.Netcode;
|
||||
using UnityEngine;
|
||||
|
||||
namespace DefaultNamespace
|
||||
{
|
||||
public class GameManager : NetworkBehaviour
|
||||
{
|
||||
[SerializeField] private NetworkObject playerPrefab;
|
||||
[SerializeField] private Transform[] startPositions;
|
||||
|
||||
private int _currentStartPositionIndex = 0;
|
||||
|
||||
protected override void OnInSceneObjectsSpawned()
|
||||
{
|
||||
if (!IsServer) return;
|
||||
|
||||
foreach (KeyValuePair<ulong, NetworkClient> client in NetworkManager.ConnectedClients)
|
||||
{
|
||||
Transform position = startPositions[_currentStartPositionIndex++ % startPositions.Length];
|
||||
NetworkObject player = Instantiate(playerPrefab, position.position, position.rotation);
|
||||
player.SpawnAsPlayerObject(client.Key);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 3126aff9b38a49b7b7c2e80705ca0e38
|
||||
timeCreated: 1749655488
|
||||
@@ -0,0 +1,75 @@
|
||||
using System;
|
||||
using Unity.Netcode;
|
||||
using UnityEngine;
|
||||
|
||||
namespace DefaultNamespace
|
||||
{
|
||||
public class NetworkPlayer : NetworkBehaviour
|
||||
{
|
||||
[SerializeField] private Camera camera;
|
||||
[SerializeField] private float speed = 10.0f;
|
||||
[SerializeField] private float rotationSpeed = 60.0f;
|
||||
[SerializeField] private Rigidbody bulletPrefab;
|
||||
[SerializeField] private float bulletForce = 50.0f;
|
||||
[SerializeField] private float initialHealth = 100.0f;
|
||||
|
||||
private NetworkVariable<float> _health = new NetworkVariable<float>(0);
|
||||
|
||||
public override void OnNetworkSpawn()
|
||||
{
|
||||
camera.gameObject.SetActive(IsLocalPlayer);
|
||||
|
||||
if (IsLocalPlayer)
|
||||
{
|
||||
_health.OnValueChanged += OnHealthChanged;
|
||||
}
|
||||
|
||||
if (IsServer)
|
||||
{
|
||||
_health.Value = initialHealth;
|
||||
}
|
||||
}
|
||||
|
||||
public override void OnNetworkDespawn()
|
||||
{
|
||||
if (IsLocalPlayer)
|
||||
{
|
||||
_health.OnValueChanged -= OnHealthChanged;
|
||||
}
|
||||
}
|
||||
|
||||
private void OnHealthChanged(float previousValue, float newValue)
|
||||
{
|
||||
// update health bar etc.
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
if (!IsLocalPlayer) return;
|
||||
|
||||
if (Input.GetButtonDown("Fire1"))
|
||||
{
|
||||
ShootRpc();
|
||||
}
|
||||
}
|
||||
|
||||
private void FixedUpdate()
|
||||
{
|
||||
if (!IsLocalPlayer) return;
|
||||
|
||||
transform.Translate(
|
||||
(Vector3.right * Input.GetAxis("Horizontal") + Vector3.forward * Input.GetAxis("Vertical")) *
|
||||
(speed * Time.deltaTime));
|
||||
|
||||
transform.Rotate(Vector3.up, Input.GetAxis("Mouse X") * rotationSpeed * Time.deltaTime);
|
||||
}
|
||||
|
||||
[Rpc(SendTo.Server)]
|
||||
private void ShootRpc()
|
||||
{
|
||||
Rigidbody bullet = Instantiate(bulletPrefab, transform.position + transform.forward, transform.rotation);
|
||||
bullet.GetComponent<NetworkObject>().Spawn();
|
||||
bullet.AddForce(transform.forward * bulletForce, ForceMode.Impulse);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: d88689de44f645e5b10cdfea836c8eb2
|
||||
timeCreated: 1749657014
|
||||
@@ -0,0 +1,43 @@
|
||||
using System;
|
||||
using Unity.Netcode;
|
||||
using UnityEditor;
|
||||
using UnityEngine;
|
||||
using UnityEngine.EventSystems;
|
||||
using UnityEngine.SceneManagement;
|
||||
|
||||
namespace DefaultNamespace
|
||||
{
|
||||
public class StartGameButton : MonoBehaviour, IPointerClickHandler
|
||||
{
|
||||
#if UNITY_EDITOR
|
||||
[SerializeField] private SceneAsset sceneAsset;
|
||||
|
||||
private void OnValidate()
|
||||
{
|
||||
sceneName = sceneAsset.name;
|
||||
}
|
||||
#endif
|
||||
|
||||
[SerializeField, HideInInspector] private string sceneName;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
NetworkManager.Singleton.OnClientConnectedCallback += OnClientConnected;
|
||||
}
|
||||
|
||||
private void OnDestroy()
|
||||
{
|
||||
NetworkManager.Singleton.OnClientConnectedCallback -= OnClientConnected;
|
||||
}
|
||||
|
||||
private void OnClientConnected(ulong obj)
|
||||
{
|
||||
gameObject.SetActive(NetworkManager.Singleton.IsServer);
|
||||
}
|
||||
|
||||
public void OnPointerClick(PointerEventData eventData)
|
||||
{
|
||||
NetworkManager.Singleton.SceneManager.LoadScene(sceneName, LoadSceneMode.Single);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: c49867b1609847119871805163febb0b
|
||||
timeCreated: 1749654423
|
||||
Reference in New Issue
Block a user