75 lines
1.6 KiB
C#
75 lines
1.6 KiB
C#
using System;
|
|
using Mirror;
|
|
using TMPro;
|
|
using UnityEngine;
|
|
|
|
public class MyNetworkPlayer : NetworkBehaviour
|
|
{
|
|
[Header("Connections")]
|
|
[SerializeField] private TMP_Text _playerNameObject;
|
|
[Header("Variables")]
|
|
[SyncVar(hook = nameof(SetPlayerNameTagText))]
|
|
[SerializeField]
|
|
private string _displayName = "Missing Name";
|
|
|
|
[SyncVar(hook = nameof(SetPlayerNameTagColor))]
|
|
[SerializeField]
|
|
private Color _playerColor = Color.black;
|
|
|
|
[SyncVar(hook = nameof(UpdateTransform))]
|
|
[SerializeField]
|
|
private Transform _transform;
|
|
|
|
[Server]
|
|
public void SetDisplayName(string newDisplayName)
|
|
{
|
|
_displayName = newDisplayName;
|
|
}
|
|
|
|
[Server]
|
|
public void SetPlayerColor(Color newPlayerColor)
|
|
{
|
|
_playerColor = newPlayerColor;
|
|
}
|
|
|
|
private void SetPlayerNameTagText(string oldText, string newText)
|
|
{
|
|
if (_playerNameObject != null)
|
|
{
|
|
_playerNameObject.text = newText;
|
|
}
|
|
}
|
|
|
|
private void SetPlayerNameTagColor(Color oldColor, Color newColor)
|
|
{
|
|
if (_playerNameObject != null)
|
|
{
|
|
_playerNameObject.color = newColor;
|
|
}
|
|
}
|
|
|
|
private void OnConnectedToServer()
|
|
{
|
|
BindTransform();
|
|
}
|
|
|
|
[Server]
|
|
private void BindTransform()
|
|
{
|
|
_transform = transform;
|
|
}
|
|
|
|
[Server]
|
|
private void UpdateTransform(Transform oldValue, Transform newValue)
|
|
{
|
|
var cachedTransform = transform;
|
|
cachedTransform.position = newValue.position;
|
|
cachedTransform.rotation = newValue.rotation;
|
|
}
|
|
|
|
[Client]
|
|
private void Update()
|
|
{
|
|
_transform = transform;
|
|
}
|
|
} |