Files changed (1) hide show
  1. Assets/ThirdPersonUserControlHFT.cs +72 -1
Assets/ThirdPersonUserControlHFT.cs CHANGED
@@ -15,7 +15,15 @@ namespace UnityStandardAssets.Characters.ThirdPerson
15
15
16
16
private HFTInput m_hftInput;
17
17
private HFTGamepad m_gamepad;
18
-
18
+ private GUIStyle m_guiStyle = new GUIStyle();
19
+ private GUIContent m_guiName = new GUIContent("");
20
+ private Rect m_nameRect = new Rect(0,0,0,0);
21
+ private string m_playerName;
22
+
23
+ private static int m_playerNumber = 0;
24
+
25
+ public Transform nameTransform;
26
+
19
27
private void Start()
20
28
{
21
29
// get the transform of the main camera
@@ -36,6 +44,13 @@ namespace UnityStandardAssets.Characters.ThirdPerson
36
44
m_hftInput = GetComponent<HFTInput>();
37
45
m_gamepad = GetComponent<HFTGamepad>();
38
46
47
+ SetColor(m_playerNumber++);
48
+ SetName(m_gamepad.Name);
49
+
50
+ // Notify us if the name changes.
51
+ m_gamepad.OnNameChange += ChangeName;
52
+
53
+ // Delete ourselves if disconnected
39
54
m_gamepad.OnDisconnect += Remove;
40
55
}
41
56
@@ -44,6 +59,62 @@ namespace UnityStandardAssets.Characters.ThirdPerson
44
59
Destroy(gameObject);
45
60
}
46
61
62
+ void SetName(string name)
63
+ {
64
+ m_playerName = name;
65
+ gameObject.name = "Player-" + m_playerName;
66
+ m_guiName = new GUIContent(m_playerName);
67
+ Vector2 size = m_guiStyle.CalcSize(m_guiName);
68
+ m_nameRect.width = size.x + 12;
69
+ m_nameRect.height = size.y + 5;
70
+ }
71
+
72
+ void OnGUI()
73
+ {
74
+ // If someone knows a better way to do
75
+ // names in Unity3D please tell me!
76
+ Vector2 size = m_guiStyle.CalcSize(m_guiName);
77
+ Vector3 coords = Camera.main.WorldToScreenPoint(nameTransform.position);
78
+ m_nameRect.x = coords.x - size.x * 0.5f - 5f;
79
+ m_nameRect.y = Screen.height - coords.y;
80
+ m_guiStyle.normal.textColor = Color.black;
81
+ m_guiStyle.contentOffset = new Vector2(4, 2);
82
+ GUI.Box(m_nameRect, m_playerName, m_guiStyle);
83
+ }
84
+
85
+ // Called when the user changes their name.
86
+ void ChangeName(object sender, System.EventArgs e)
87
+ {
88
+ SetName(m_gamepad.Name);
89
+ }
90
+
91
+ void SetColor(int colorNdx) {
92
+ // Pick a color
93
+ // see http://greggman.github.io/doodles/picking-colors.html
94
+ float hue = (((colorNdx & 0x01) << 5) |
95
+ ((colorNdx & 0x02) << 3) |
96
+ ((colorNdx & 0x04) << 1) |
97
+ ((colorNdx & 0x08) >> 1) |
98
+ ((colorNdx & 0x10) >> 3) |
99
+ ((colorNdx & 0x20) >> 5)) / 64.0f;
100
+ float val = (colorNdx & 0x20) != 0 ? 0.5f : 1.0f;
101
+ float sat = (colorNdx & 0x10) != 0 ? 0.5f : 1.0f;
102
+
103
+ // now get the adjusted color.
104
+ Color playerColor = Color.HSVToRGB(hue, sat, val);
105
+
106
+ // Tell the gamepad to change color
107
+ m_gamepad.color = playerColor;
108
+
109
+ // Create a 1 pixel texture for the OnGUI code to draw the label
110
+ Color[] pix = new Color[1];
111
+ pix[0] = playerColor;
112
+ Texture2D tex = new Texture2D(1, 1);
113
+ tex.SetPixels(pix);
114
+ tex.Apply();
115
+ m_guiStyle.normal.background = tex;
116
+ }
117
+
47
118
private void Update()
48
119
{
49
120
if (!m_Jump)