-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCharacterSwap.cs
198 lines (176 loc) · 7.57 KB
/
CharacterSwap.cs
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
using System;
using System.Collections.Generic;
using Unity.BossRoom.Gameplay.GameplayObjects.AnimationCallbacks;
using UnityEngine;
namespace Unity.BossRoom.Gameplay.GameplayObjects.Character
{
/// <summary>
/// Responsible for storing of all of the pieces of a character, and swapping the pieces out en masse when asked to.
/// </summary>
public class CharacterSwap : MonoBehaviour
{
[System.Serializable]
public class CharacterModelSet
{
public GameObject ears;
public GameObject head;
public GameObject mouth;
public GameObject hair;
public GameObject eyes;
public GameObject torso;
public GameObject gearRightHand;
public GameObject gearLeftHand;
public GameObject handRight;
public GameObject handLeft;
public GameObject shoulderRight;
public GameObject shoulderLeft;
public GameObject handSocket;
public AnimatorTriggeredSpecialFX specialFx;
public AnimatorOverrideController animatorOverrides; // references a separate stand-alone object in the project
private List<Renderer> m_CachedRenderers;
public void SetFullActive(bool isActive)
{
ears.SetActive(isActive);
head.SetActive(isActive);
mouth.SetActive(isActive);
hair.SetActive(isActive);
eyes.SetActive(isActive);
torso.SetActive(isActive);
gearLeftHand.SetActive(isActive);
gearRightHand.SetActive(isActive);
handRight.SetActive(isActive);
handLeft.SetActive(isActive);
shoulderRight.SetActive(isActive);
shoulderLeft.SetActive(isActive);
}
public List<Renderer> GetAllBodyParts()
{
if (m_CachedRenderers == null)
{
m_CachedRenderers = new List<Renderer>();
AddRenderer(ref m_CachedRenderers, ears);
AddRenderer(ref m_CachedRenderers, head);
AddRenderer(ref m_CachedRenderers, mouth);
AddRenderer(ref m_CachedRenderers, hair);
AddRenderer(ref m_CachedRenderers, torso);
AddRenderer(ref m_CachedRenderers, gearRightHand);
AddRenderer(ref m_CachedRenderers, gearLeftHand);
AddRenderer(ref m_CachedRenderers, handRight);
AddRenderer(ref m_CachedRenderers, handLeft);
AddRenderer(ref m_CachedRenderers, shoulderRight);
AddRenderer(ref m_CachedRenderers, shoulderLeft);
}
return m_CachedRenderers;
}
private void AddRenderer(ref List<Renderer> rendererList, GameObject bodypartGO)
{
if (!bodypartGO) { return; }
var bodyPartRenderer = bodypartGO.GetComponent<Renderer>();
if (!bodyPartRenderer) { return; }
rendererList.Add(bodyPartRenderer);
}
}
[SerializeField]
CharacterModelSet m_CharacterModel;
public CharacterModelSet CharacterModel => m_CharacterModel;
/// <summary>
/// Reference to our shared-characters' animator.
/// Can be null, but if so, animator overrides are not supported!
/// </summary>
[SerializeField]
private Animator m_Animator;
/// <summary>
/// Reference to the original controller in our Animator.
/// We switch back to this whenever we don't have an Override.
/// </summary>
private RuntimeAnimatorController m_OriginalController;
[SerializeField]
[Tooltip("Special Material we plug in when the local player is \"stealthy\"")]
private Material m_StealthySelfMaterial;
[SerializeField]
[Tooltip("Special Material we plug in when another player is \"stealthy\"")]
private Material m_StealthyOtherMaterial;
public enum SpecialMaterialMode
{
None,
StealthySelf,
StealthyOther,
}
/// <summary>
/// When we swap all our Materials out for a special material,
/// we keep the old references here, so we can swap them back.
/// </summary>
private Dictionary<Renderer, Material> m_OriginalMaterials = new Dictionary<Renderer, Material>();
ClientCharacter m_ClientCharacter;
void Awake()
{
m_ClientCharacter = GetComponentInParent<ClientCharacter>();
m_Animator = m_ClientCharacter.OurAnimator;
m_OriginalController = m_Animator.runtimeAnimatorController;
}
private void OnDisable()
{
// It's important that the original Materials that we pulled out of the renderers are put back.
// Otherwise nothing will Destroy() them and they will leak! (Alternatively we could manually
// Destroy() these in our OnDestroy(), but in this case it makes more sense just to put them back.)
ClearOverrideMaterial();
}
/// <summary>
/// Swap the visuals of the character to the index passed in.
/// </summary>
/// <param name="specialMaterialMode">Special Material to apply to all body parts</param>
public void SwapToModel(SpecialMaterialMode specialMaterialMode = SpecialMaterialMode.None)
{
ClearOverrideMaterial();
if (m_CharacterModel.specialFx)
{
m_CharacterModel.specialFx.enabled = true;
}
if (m_Animator)
{
// plug in the correct animator override... or plug the original non - overridden version back in!
if (m_CharacterModel.animatorOverrides)
{
m_Animator.runtimeAnimatorController = m_CharacterModel.animatorOverrides;
}
else
{
m_Animator.runtimeAnimatorController = m_OriginalController;
}
}
// lastly, now that we're all assembled, apply any override material.
switch (specialMaterialMode)
{
case SpecialMaterialMode.StealthySelf:
SetOverrideMaterial(m_StealthySelfMaterial);
break;
case SpecialMaterialMode.StealthyOther:
SetOverrideMaterial(m_StealthyOtherMaterial);
break;
}
}
private void ClearOverrideMaterial()
{
foreach (var entry in m_OriginalMaterials)
{
if (entry.Key)
{
entry.Key.material = entry.Value;
}
}
m_OriginalMaterials.Clear();
}
private void SetOverrideMaterial(Material overrideMaterial)
{
ClearOverrideMaterial(); // just sanity-checking; this should already have been called!
foreach (var bodyPart in m_CharacterModel.GetAllBodyParts())
{
if (bodyPart)
{
m_OriginalMaterials[bodyPart] = bodyPart.material;
bodyPart.material = overrideMaterial;
}
}
}
}
}