Skip to content
This repository was archived by the owner on Mar 24, 2022. It is now read-only.

Commit 5dfc57a

Browse files
committed
Improved BarChart example
- Added chart colour configuration
1 parent ad25e84 commit 5dfc57a

File tree

3 files changed

+169
-12
lines changed

3 files changed

+169
-12
lines changed

Assets/ScreenSpace/BarChart/Scenes/BarChart.unity

+116-4
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,40 @@ CanvasRenderer:
176176
m_PrefabParentObject: {fileID: 0}
177177
m_PrefabInternal: {fileID: 0}
178178
m_GameObject: {fileID: 136087302}
179+
--- !u!1 &156350117
180+
GameObject:
181+
m_ObjectHideFlags: 0
182+
m_PrefabParentObject: {fileID: 0}
183+
m_PrefabInternal: {fileID: 0}
184+
serializedVersion: 5
185+
m_Component:
186+
- component: {fileID: 156350118}
187+
m_Layer: 5
188+
m_Name: Labels
189+
m_TagString: Untagged
190+
m_Icon: {fileID: 0}
191+
m_NavMeshLayer: 0
192+
m_StaticEditorFlags: 0
193+
m_IsActive: 1
194+
--- !u!224 &156350118
195+
RectTransform:
196+
m_ObjectHideFlags: 0
197+
m_PrefabParentObject: {fileID: 0}
198+
m_PrefabInternal: {fileID: 0}
199+
m_GameObject: {fileID: 156350117}
200+
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
201+
m_LocalPosition: {x: 0, y: 0, z: 0}
202+
m_LocalScale: {x: 1, y: 1, z: 1}
203+
m_Children:
204+
- {fileID: 835784920}
205+
m_Father: {fileID: 2079946680}
206+
m_RootOrder: 4
207+
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
208+
m_AnchorMin: {x: 0, y: 0}
209+
m_AnchorMax: {x: 1, y: 1}
210+
m_AnchoredPosition: {x: 0, y: 0}
211+
m_SizeDelta: {x: 0, y: 0}
212+
m_Pivot: {x: 0.5, y: 0.5}
179213
--- !u!1 &209690544
180214
GameObject:
181215
m_ObjectHideFlags: 0
@@ -583,6 +617,80 @@ CanvasRenderer:
583617
m_PrefabParentObject: {fileID: 0}
584618
m_PrefabInternal: {fileID: 0}
585619
m_GameObject: {fileID: 747071699}
620+
--- !u!1 &835784919
621+
GameObject:
622+
m_ObjectHideFlags: 0
623+
m_PrefabParentObject: {fileID: 0}
624+
m_PrefabInternal: {fileID: 0}
625+
serializedVersion: 5
626+
m_Component:
627+
- component: {fileID: 835784920}
628+
- component: {fileID: 835784922}
629+
- component: {fileID: 835784921}
630+
m_Layer: 5
631+
m_Name: Text
632+
m_TagString: Untagged
633+
m_Icon: {fileID: 0}
634+
m_NavMeshLayer: 0
635+
m_StaticEditorFlags: 0
636+
m_IsActive: 1
637+
--- !u!224 &835784920
638+
RectTransform:
639+
m_ObjectHideFlags: 0
640+
m_PrefabParentObject: {fileID: 0}
641+
m_PrefabInternal: {fileID: 0}
642+
m_GameObject: {fileID: 835784919}
643+
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
644+
m_LocalPosition: {x: 0, y: 0, z: 0}
645+
m_LocalScale: {x: 1, y: 1, z: 1}
646+
m_Children: []
647+
m_Father: {fileID: 156350118}
648+
m_RootOrder: 0
649+
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
650+
m_AnchorMin: {x: 0, y: 1}
651+
m_AnchorMax: {x: 1, y: 1}
652+
m_AnchoredPosition: {x: 0, y: -38}
653+
m_SizeDelta: {x: -400, y: 30}
654+
m_Pivot: {x: 0.5, y: 0.5}
655+
--- !u!114 &835784921
656+
MonoBehaviour:
657+
m_ObjectHideFlags: 0
658+
m_PrefabParentObject: {fileID: 0}
659+
m_PrefabInternal: {fileID: 0}
660+
m_GameObject: {fileID: 835784919}
661+
m_Enabled: 1
662+
m_EditorHideFlags: 0
663+
m_Script: {fileID: 708705254, guid: f70555f144d8491a825f0804e09c671c, type: 3}
664+
m_Name:
665+
m_EditorClassIdentifier:
666+
m_Material: {fileID: 0}
667+
m_Color: {r: 0.19607843, g: 0.19607843, b: 0.19607843, a: 1}
668+
m_RaycastTarget: 1
669+
m_OnCullStateChanged:
670+
m_PersistentCalls:
671+
m_Calls: []
672+
m_TypeName: UnityEngine.UI.MaskableGraphic+CullStateChangedEvent, UnityEngine.UI,
673+
Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
674+
m_FontData:
675+
m_Font: {fileID: 10102, guid: 0000000000000000e000000000000000, type: 0}
676+
m_FontSize: 14
677+
m_FontStyle: 0
678+
m_BestFit: 1
679+
m_MinSize: 10
680+
m_MaxSize: 40
681+
m_Alignment: 4
682+
m_AlignByGeometry: 0
683+
m_RichText: 1
684+
m_HorizontalOverflow: 0
685+
m_VerticalOverflow: 0
686+
m_LineSpacing: 1
687+
m_Text: An Amazing Title
688+
--- !u!222 &835784922
689+
CanvasRenderer:
690+
m_ObjectHideFlags: 0
691+
m_PrefabParentObject: {fileID: 0}
692+
m_PrefabInternal: {fileID: 0}
693+
m_GameObject: {fileID: 835784919}
586694
--- !u!1 &1019806550
587695
GameObject:
588696
m_ObjectHideFlags: 0
@@ -749,7 +857,7 @@ MonoBehaviour:
749857
m_Script: {fileID: 11500000, guid: 0620ac826f590b749960a0a46e79c920, type: 3}
750858
m_Name:
751859
m_EditorClassIdentifier:
752-
colour: {r: 255, g: 0, b: 0, a: 1}
860+
colour: {r: 0, g: 0, b: 0, a: 0}
753861
showValueText: 1
754862
valueText: {fileID: 1308495441}
755863
--- !u!114 &1049236255
@@ -1597,6 +1705,7 @@ RectTransform:
15971705
- {fileID: 659990842}
15981706
- {fileID: 1214717182}
15991707
- {fileID: 2084300901}
1708+
- {fileID: 156350118}
16001709
m_Father: {fileID: 1445041688}
16011710
m_RootOrder: 0
16021711
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
@@ -1660,10 +1769,13 @@ MonoBehaviour:
16601769
m_Script: {fileID: 11500000, guid: 6c69a24082c97aa41bfd7f5f3a670018, type: 3}
16611770
m_Name:
16621771
m_EditorClassIdentifier:
1663-
BarChartColour:
1664-
SolidColour: {r: 0, g: 0, b: 0, a: 0}
16651772
BarChartComponentGameObject: {fileID: 1483493903191022, guid: 2307b2c483549c84bbc95b8d42cc940f,
16661773
type: 2}
1774+
chartColourConfig:
1775+
GardientEndColour: {r: 0, g: 0, b: 0, a: 0}
1776+
GradientStartColour: {r: 0, g: 0, b: 0, a: 0}
1777+
SolidColour: {r: 0, g: 0, b: 0, a: 0}
1778+
chartColourStyle: 0
16671779
barDirection: 2
16681780
heightPercentage: 80
16691781
leftPaddingPercentage: 10
@@ -1837,7 +1949,7 @@ MonoBehaviour:
18371949
m_Name:
18381950
m_EditorClassIdentifier:
18391951
m_Material: {fileID: 0}
1840-
m_Color: {r: 255, g: 0, b: 0, a: 1}
1952+
m_Color: {r: 0, g: 0, b: 0, a: 0}
18411953
m_RaycastTarget: 1
18421954
m_OnCullStateChanged:
18431955
m_PersistentCalls:

Assets/ScreenSpace/BarChart/Scripts/BarChartEditor.cs

+9-4
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,14 @@ public class BarChartEditor : Editor {
4141

4242
public override void OnInspectorGUI() {
4343
BarChartManager bcmScript = (BarChartManager)target;
44+
EditorGUILayout.LabelField(
45+
"Bar Chart Manager",
46+
new GUIStyle { fontSize = 18, fontStyle = FontStyle.Bold, alignment = TextAnchor.MiddleCenter });
47+
EditorGUILayout.Space();
48+
EditorGUILayout.Space();
4449
DrawDefaultInspector();
4550

46-
if (GUILayout.Button("Update")) {
51+
if (GUILayout.Button("Update Styles")) {
4752
bcmScript.UpdateVisuals();
4853
}
4954

@@ -52,18 +57,18 @@ public override void OnInspectorGUI() {
5257
EditorGUILayout.LabelField(
5358
"Bar Chart Editor",
5459
new GUIStyle { fontSize = 18, fontStyle = FontStyle.Bold, alignment = TextAnchor.MiddleCenter });
55-
EditorGUILayout.LabelField("New Bar", new GUIStyle { fontSize = 12, fontStyle = FontStyle.Bold });
60+
EditorGUILayout.LabelField("New", new GUIStyle { fontSize = 12, fontStyle = FontStyle.Bold });
5661

5762
value = EditorGUILayout.IntField("Value", value);
5863
min = EditorGUILayout.IntField("Min", min);
5964
max = EditorGUILayout.IntField("Max", max);
6065
showValue = EditorGUILayout.Toggle("Show value text", showValue);
6166

62-
if (GUILayout.Button("Build Object")) {
67+
if (GUILayout.Button("Create")) {
6368
bcmScript.AddNewBar(min, max, value, showValue);
6469
}
6570

66-
EditorGUILayout.LabelField("Edit bar(s)", new GUIStyle { fontSize = 12, fontStyle = FontStyle.Bold });
71+
EditorGUILayout.LabelField("Edit", new GUIStyle { fontSize = 12, fontStyle = FontStyle.Bold });
6772
for (int i = 0; i < bcmScript.transform.childCount; i++) {
6873

6974
EditorGUILayout.LabelField(string.Format("Bar {0}", i), new GUIStyle { fontSize = 10 });

Assets/ScreenSpace/BarChart/Scripts/BarChartManager.cs

+44-4
Original file line numberDiff line numberDiff line change
@@ -22,20 +22,23 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2222
SOFTWARE.
2323
*/
2424

25+
using System;
26+
using System.Diagnostics.CodeAnalysis;
2527
using System.Globalization;
2628
using Assets.ScreenSpace.PercentageBased.Scripts;
2729
using UnityEngine;
2830
using UnityEngine.UI;
31+
using Random = System.Random;
2932

3033
namespace Assets.ScreenSpace.BarChart.Scripts {
3134

3235
[AddComponentMenu("Scripts/Bar Chart/Bar Chart Manager")]
3336
[RequireComponent(typeof(RectTransform))]
34-
[ExecuteInEditMode]
37+
//[ExecuteInEditMode]
3538
public class BarChartManager : MonoBehaviour {
36-
37-
public BarChartColourStyle BarChartColour = new BarChartColourStyle();
3839
public GameObject BarChartComponentGameObject;
40+
public BarChartColourConfig ChartColourConfig;
41+
public BarChartColourStyle ChartColourStyle;
3942

4043
[SerializeField] private Slider.Direction barDirection;
4144
[SerializeField] private int heightPercentage;
@@ -146,6 +149,10 @@ public void AddNewBar(float min, float max, float value, bool showValue) {
146149
UpdateVisuals();
147150
}
148151

152+
[SuppressMessage(
153+
"StyleCop.CSharp.NamingRules",
154+
"SA1305:FieldNamesMustNotUseHungarianNotation",
155+
Justification = "Reviewed. Suppression is OK here.")]
149156
public void UpdateVisuals() {
150157
for (int i = 0; i < transform.childCount; i++) {
151158
if (transform.GetChild(i).tag != "Bar") {
@@ -157,11 +164,12 @@ public void UpdateVisuals() {
157164
|| transform.GetChild(i).GetComponent<BarChartComponent>() == null) {
158165
Debug.LogWarningFormat(
159166
transform,
160-
"Manager contains children not fit for purpose.",
167+
"Manager contains child not fit for purpose.",
161168
transform.GetChild(i));
162169
return;
163170
}
164171

172+
// locals
165173
Slider childSlider = transform.GetChild(i).GetComponent<Slider>();
166174
ScaledComponent childScaledComponent = transform.GetChild(i).GetComponent<ScaledComponent>();
167175
BarChartComponent childBcc = transform.GetChild(i).GetComponent<BarChartComponent>();
@@ -183,8 +191,40 @@ public void UpdateVisuals() {
183191
childScaledComponent.Container = GetComponent<RectTransform>();
184192
childScaledComponent.ComponentScale.Height = heightPercentage;
185193
childScaledComponent.ComponentScale.Width = widthPercentage;
194+
195+
// Colour
196+
switch (ChartColourStyle) {
197+
case BarChartColourStyle.Solid:
198+
childBcc.Colour = ChartColourConfig.SolidColour;
199+
break;
200+
case BarChartColourStyle.Random:
201+
Random rand = new Random();
202+
int r = rand.Next(0, 255);
203+
int g = rand.Next(0, 255);
204+
int b = rand.Next(0, 255);
205+
childBcc.Colour = new Color(r, g, b);
206+
break;
207+
case BarChartColourStyle.RandomSoft:
208+
Random randSoft = new Random();
209+
int rSoft = randSoft.Next(170, 210);
210+
int gSoft = randSoft.Next(170, 210);
211+
int bSoft = randSoft.Next(170, 210);
212+
childBcc.Colour = new Color(rSoft, gSoft, bSoft);
213+
break;
214+
case BarChartColourStyle.RandomRGB:
215+
break;
216+
case BarChartColourStyle.Gradient:
217+
break;
218+
}
186219
}
187220
}
221+
222+
[Serializable]
223+
public class BarChartColourConfig {
224+
public Color GardientEndColour;
225+
public Color GradientStartColour;
226+
public Color SolidColour;
227+
}
188228
}
189229

190230
}

0 commit comments

Comments
 (0)