forked from michelemorello97/Boss_Room-ThinkEngine_repo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClientAvatarGuidHandler.cs
More file actions
50 lines (41 loc) · 1.55 KB
/
ClientAvatarGuidHandler.cs
File metadata and controls
50 lines (41 loc) · 1.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
using System;
using Unity.Netcode;
using UnityEngine;
namespace Unity.BossRoom.Gameplay.GameplayObjects.Character
{
/// <summary>
/// Client-side component that awaits a state change on an avatar's Guid, and fetches matching Avatar from the
/// AvatarRegistry, if possible. Once fetched, the Graphics GameObject is spawned.
/// </summary>
[RequireComponent(typeof(NetworkAvatarGuidState))]
public class ClientAvatarGuidHandler : NetworkBehaviour
{
[SerializeField]
Animator m_GraphicsAnimator;
[SerializeField]
NetworkAvatarGuidState m_NetworkAvatarGuidState;
public Animator graphicsAnimator => m_GraphicsAnimator;
public event Action<GameObject> AvatarGraphicsSpawned;
public override void OnNetworkSpawn()
{
if (IsClient)
{
InstantiateAvatar();
}
}
void InstantiateAvatar()
{
if (m_GraphicsAnimator.transform.childCount > 0)
{
// we may receive a NetworkVariable's OnValueChanged callback more than once as a client
// this makes sure we don't spawn a duplicate graphics GameObject
return;
}
// spawn avatar graphics GameObject
Instantiate(m_NetworkAvatarGuidState.RegisteredAvatar.Graphics, m_GraphicsAnimator.transform);
m_GraphicsAnimator.Rebind();
m_GraphicsAnimator.Update(0f);
AvatarGraphicsSpawned?.Invoke(m_GraphicsAnimator.gameObject);
}
}
}