add unity project for networking
This commit is contained in:
@@ -0,0 +1,40 @@
|
||||
using System;
|
||||
using Unity.Netcode;
|
||||
using UnityEngine;
|
||||
using UnityEngine.SceneManagement;
|
||||
|
||||
namespace DefaultNamespace
|
||||
{
|
||||
public class Menu : MonoBehaviour
|
||||
{
|
||||
[SerializeField] private GameObject lobbyUi;
|
||||
|
||||
private void Start()
|
||||
{
|
||||
NetworkManager.Singleton.OnServerStarted += OnNetworkInstanceStarted;
|
||||
NetworkManager.Singleton.OnClientStarted += OnNetworkInstanceStarted;
|
||||
|
||||
NetworkManager.Singleton.OnServerStopped += OnNetworkInstanceStopped;
|
||||
NetworkManager.Singleton.OnClientStopped += OnNetworkInstanceStopped;
|
||||
}
|
||||
|
||||
private void OnDestroy()
|
||||
{
|
||||
NetworkManager.Singleton.OnServerStarted -= OnNetworkInstanceStarted;
|
||||
NetworkManager.Singleton.OnClientStarted -= OnNetworkInstanceStarted;
|
||||
|
||||
NetworkManager.Singleton.OnServerStopped -= OnNetworkInstanceStopped;
|
||||
NetworkManager.Singleton.OnClientStopped -= OnNetworkInstanceStopped;
|
||||
}
|
||||
|
||||
private void OnNetworkInstanceStopped(bool obj)
|
||||
{
|
||||
lobbyUi.SetActive(true);
|
||||
}
|
||||
|
||||
private void OnNetworkInstanceStarted()
|
||||
{
|
||||
lobbyUi.SetActive(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 1024f896ffbf4a459f9db11586837d87
|
||||
timeCreated: 1765210397
|
||||
@@ -0,0 +1,88 @@
|
||||
using System;
|
||||
using Unity.Netcode;
|
||||
using UnityEngine;
|
||||
using UnityEngine.InputSystem;
|
||||
using UnityEngine.UI;
|
||||
|
||||
namespace DefaultNamespace
|
||||
{
|
||||
public class NetworkPlayer : NetworkBehaviour
|
||||
{
|
||||
[Header("Player Values")]
|
||||
[SerializeField] private float speed = 10f;
|
||||
[SerializeField] private float initialHealth = 100.0f;
|
||||
[SerializeField] private NetworkObject bulletPrefab;
|
||||
|
||||
[Header("Input")]
|
||||
[SerializeField] private InputActionProperty moveInput;
|
||||
[SerializeField] private InputActionProperty fireInput;
|
||||
|
||||
[Header("Elements")]
|
||||
[SerializeField] private Camera playerCamera;
|
||||
[SerializeField] private Canvas playerCanvas;
|
||||
[SerializeField] private Image playerHealthBar;
|
||||
|
||||
private NetworkVariable<float> _health = new NetworkVariable<float>();
|
||||
|
||||
public override void OnNetworkSpawn()
|
||||
{
|
||||
// playerCamera.gameObject.SetActive(IsLocalPlayer);
|
||||
// playerCanvas.gameObject.SetActive(IsLocalPlayer);
|
||||
|
||||
|
||||
if (IsLocalPlayer)
|
||||
{
|
||||
_health.OnValueChanged += OnHealthChanged;
|
||||
|
||||
moveInput.action.Enable();
|
||||
|
||||
fireInput.action.performed += OnFired;
|
||||
fireInput.action.Enable();
|
||||
}
|
||||
|
||||
if (IsServer)
|
||||
{
|
||||
_health.Value = initialHealth;
|
||||
}
|
||||
}
|
||||
|
||||
public override void OnNetworkDespawn()
|
||||
{
|
||||
|
||||
if (IsLocalPlayer)
|
||||
{
|
||||
_health.OnValueChanged -= OnHealthChanged;
|
||||
|
||||
moveInput.action.Disable();
|
||||
|
||||
fireInput.action.performed -= OnFired;
|
||||
fireInput.action.Disable();
|
||||
}
|
||||
}
|
||||
|
||||
private void FixedUpdate()
|
||||
{
|
||||
if (!IsLocalPlayer) return;
|
||||
|
||||
Vector2 input = moveInput.action.ReadValue<Vector2>() * (speed * Time.deltaTime);
|
||||
transform.Translate(input.x, 0.0f, input.y);
|
||||
}
|
||||
|
||||
private void OnHealthChanged(float previousValue, float newValue)
|
||||
{
|
||||
// playerHealthBar.fillAmount = newValue / initialHealth;
|
||||
}
|
||||
|
||||
private void OnFired(InputAction.CallbackContext obj)
|
||||
{
|
||||
SpawnBulletRpc();
|
||||
}
|
||||
|
||||
[Rpc(SendTo.Server)]
|
||||
private void SpawnBulletRpc()
|
||||
{
|
||||
NetworkObject bullet = Instantiate(bulletPrefab, transform.position, transform.rotation);
|
||||
bullet.Spawn(true);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: cdb72dd90b7f4d9c8c1a565a8f3acbd8
|
||||
timeCreated: 1765212689
|
||||
Reference in New Issue
Block a user