Ryle Radio 1.0.1
An open-source "radio" system for Unity, allowing tracks, tuning, broadcasters, and more!
Loading...
Searching...
No Matches
RadioData.cs
2using System;
3using System.Collections.Generic;
4using System.Linq;
5using UnityEngine;
6
7#pragma warning disable 0414 // we do this as the force clip sample rate variables are technically not used in code- but they are in the inspector
8
9namespace RyleRadio
10{
11 /// <summary>
12 /// The central data object defining the radio. Contains the tracks and information required to play the radio at runtime.
13 /// <br><br>Has a custom editor at \ref RadioDataEditor
14 /// </summary>
15 [CreateAssetMenu(fileName = "New Radio Data", menuName = "Ryle Radio/Radio Data")]
16 public class RadioData : ScriptableObject
17 {
18 // i wanted these to be editable in the inspector too, but for now it's internally hardcoded- looking into changing this at some point
19 /// The lower limit for tune on this radio. This may become non-const at some point
20 public const float LOW_TUNE = 0;
21 /// The upper limit for tune on this radio. This may become non-const at some point
22 public const float HIGH_TUNE = 1000;
23
24 /// The primary colour of gizmos relating to this radio
25 [SerializeField] private Color gizmoColor = new Color32(200, 180, 255, 255);
26 /// The secondary colour of gizmos relating to this radio
27 [SerializeField] private Color gizmoColorSecondary = new Color32(175, 105, 205, 255);
28
29 /// Whether or not this radio forces the sample rate on AudioClips it references
30 [SerializeField] private bool forceClipSampleRate = true;
31 /// The sample rate this radio can force on AudioClips it references. If left at 0, it chooses the project's default sample rate
32 [SerializeField] private int forcedSampleRate = 0;
33
34 /// <summary>
35 /// The tracks contained in this radio, editable in the inspector
36 /// </summary>
37 [SerializeField] private List<RadioTrackWrapper> trackWs = new() { new() };
38
39
40 /// Alias for \ref gizmoColor for safety
41 public Color GizmoColor => gizmoColor;
42 /// Alias for \ref gizmoColorSecondary for safety
44
45 /// Alias for \ref trackWs for safety- in documentation we usually call them the tracks, but for code clarity we explicitly call them wrappers in this object
46 public List<RadioTrackWrapper> TrackWrappers => trackWs;
47
48
49 /// Event invoked when \ref Init() is called
50 public Action<RadioData> OnInit { get; set; } = new(_ => { });
51 /// Event invoked when \ref Init() is called, but at the beginning before anything happens
52 public Action<RadioData> BeforeInit { get; set; } = new(_ => { });
53
54
55#if !SKIP_IN_DOXYGEN
56 // the names of all tracks contained in this radio
57 private List<string> trackNames;
58#endif
59 /// <summary>
60 /// The names of all tracks stored in this radio, used when selecting them in the inspector
61 /// </summary>
62 public List<string> TrackNames
63 {
64 get
65 {
66 // if the names haven't been generated, generate them
67 if (trackNames == null || trackNames.Count <= 0)
69
70 return trackNames;
71 }
72 }
73
74 /// <summary>
75 /// The IDs of all tracks stored in this radio
76 /// </summary>
77 private List<string> trackIDs;
78 public List<string> TrackIDs
79 {
80 get
81 {
82 // if the ids haven't been generated, generate them
83 if (trackIDs == null || trackIDs.Count <= 0)
85
86 return trackIDs;
87 }
88 }
89
90
91 /// <summary>
92 /// Converts a track's name to ID format
93 /// </summary>
94 /// <param name="_name">The name to convert</param>
95 /// <returns>The name transformed into ID format</returns>
96 public static string NameToID(string _name)
97 {
98 return _name.Split(", ")[0];
99 }
100
101 /// <summary>
102 /// Fills \ref TrackNames and \ref TrackIDs to match the current content of \ref trackWs
103 /// </summary>
104 private void PopulateTrackIDs()
105 {
106 // if there are no tracks, don't try to get the names
107 if (trackWs.Count <= 0)
108 return;
109
110 // reset the track info lists
111 trackNames = new List<string>();
112 trackIDs = new List<string>();
113
114 // for every track in the radio
115 for (int i = 0; i < TrackWrappers.Count; i++)
116 {
117 // cache the track
119
120 // find any other tracks with the same id as this one
121 var othersWithID = trackIDs.Where(t => t == track.id);
122
123 // if there are others, change the id of this track to match
124 if (othersWithID.Count() > 0)
125 track.id += othersWithID.Count(); // you can only add one new track at a time, so we just append the count to the end
126 // e.g a adding a track with the last one's id "music" will make the id of the new track "music1"
127
128 // combine some track info into a display id
129 string name = $"{track.id}, {track.range.x} - {track.range.y}";
130
131 // store the id and id
132 trackNames.Add(name);
133 trackIDs.Add(track.id);
134
135 // assign the id
136 track.name = name;
137 }
138 }
139
140 /// <summary>
141 /// Updates the track names and IDs when this object is changed
142 /// </summary>
143 private void OnValidate()
144 {
146 }
147
148 /// <summary>
149 /// Initialise this radio, its tracks, and referenced components
150 /// </summary>
151 public void Init()
152 {
153 BeforeInit(this);
154
155 // initialize all the tracks
156 foreach (RadioTrackWrapper trackW in TrackWrappers)
157 trackW.Init();
158
159 OnInit(this);
160 }
161
162 /// <summary>
163 /// Clears track names and IDs
164 /// </summary>
165 public void ClearCache()
166 {
167 TrackNames.Clear();
168 TrackIDs.Clear();
169 }
170
171 /// <summary>
172 /// Attempts to find a track in this radio using either an ID or a name
173 /// </summary>
174 /// <param name="_idOrName">The ID or name of the track to find</param>
175 /// <param name="_trackW">The track that has been found, or null if none was found</param>
176 /// <param name="_useID">If true, this method searches for a matching ID. If false, it searches for a matching name</param>
177 /// <returns>True if a track was found, false if not</returns>
178 public bool TryGetTrack(string _idOrName, out RadioTrackWrapper _trackW, bool _useID = true)
179 {
180 // either an id or id can be supplied here, but we always convert it to an id for this
181 string id = "";
182
183 if (_useID) // if an id was provided, use it
184 id = _idOrName;
185 else // if a id was provided, convert it to the id
186 id = NameToID(_idOrName);
187
188 // find any track with that id
189 var found = TrackWrappers.Find(t => t.id == id);
190
191 // if a track is found,
192 if (found != null)
193 {
194 // output it and return true
195 _trackW = found;
196 return true;
197 }
198 else // otherwise
199 {
200 // output it and return false
201 _trackW = null;
202 return false;
203 }
204 }
205 }
206
207}
The central data object defining the radio. Contains the tracks and information required to play the ...
Definition RadioData.cs:17
List< string > trackIDs
The IDs of all tracks stored in this radio.
Definition RadioData.cs:77
int forcedSampleRate
The sample rate this radio can force on AudioClips it references. If left at 0, it chooses the projec...
Definition RadioData.cs:32
List< string > TrackNames
The names of all tracks stored in this radio, used when selecting them in the inspector.
Definition RadioData.cs:63
Color gizmoColor
The primary colour of gizmos relating to this radio.
Definition RadioData.cs:25
void OnValidate()
Updates the track names and IDs when this object is changed.
Definition RadioData.cs:143
Action< RadioData > BeforeInit
Event invoked when Init() is called, but at the beginning before anything happens.
Definition RadioData.cs:52
static string NameToID(string _name)
Converts a track's name to ID format.
Definition RadioData.cs:96
void ClearCache()
Clears track names and IDs.
Definition RadioData.cs:165
void Init()
Initialise this radio, its tracks, and referenced components.
Definition RadioData.cs:151
Color GizmoColor
Alias for gizmoColor for safety.
Definition RadioData.cs:41
Color gizmoColorSecondary
The secondary colour of gizmos relating to this radio.
Definition RadioData.cs:27
const float LOW_TUNE
The lower limit for tune on this radio. This may become non-const at some point.
Definition RadioData.cs:20
bool TryGetTrack(string _idOrName, out RadioTrackWrapper _trackW, bool _useID=true)
Attempts to find a track in this radio using either an ID or a name.
Definition RadioData.cs:178
List< RadioTrackWrapper > trackWs
The tracks contained in this radio, editable in the inspector.
Definition RadioData.cs:37
Action< RadioData > OnInit
Event invoked when Init() is called.
Definition RadioData.cs:50
const float HIGH_TUNE
The upper limit for tune on this radio. This may become non-const at some point.
Definition RadioData.cs:22
bool forceClipSampleRate
Whether or not this radio forces the sample rate on AudioClips it references.
Definition RadioData.cs:30
List< RadioTrackWrapper > TrackWrappers
Alias for trackWs for safety- in documentation we usually call them the tracks, but for code clarity ...
Definition RadioData.cs:46
void PopulateTrackIDs()
Fills TrackNames and TrackIDs to match the current content of trackWs.
Definition RadioData.cs:104
Color GizmoColorSecondary
Alias for gizmoColorSecondary for safety.
Definition RadioData.cs:43
A wrapper class for RadioTrack so that track types can be switched between in the inspector!...
string id
The ID of this track- used to find and manipulate it in custom code.
void Init()
Initialize this wrapper and its track.
Tracks to be used on a radio- includes base classes.
Definition RadioUtils.cs:20
The entire package.