diff --git a/Attributes/MinMaxRangeAttribute.cs b/Attributes/MinMaxRangeAttribute.cs new file mode 100644 index 0000000..e1aa92f --- /dev/null +++ b/Attributes/MinMaxRangeAttribute.cs @@ -0,0 +1,13 @@ +using System; + +public class MinMaxRangeAttribute : Attribute +{ + public float Min { get; private set; } + public float Max { get; private set; } + + public MinMaxRangeAttribute(float min, float max) + { + Min = min; + Max = max; + } +} \ No newline at end of file diff --git a/CustomTypes/RangedFloat.cs b/CustomTypes/RangedFloat.cs new file mode 100644 index 0000000..d4e44ec --- /dev/null +++ b/CustomTypes/RangedFloat.cs @@ -0,0 +1,11 @@ +using System; + +namespace Util.CustomTypes +{ + [Serializable] + public struct RangedFloat + { + public float minValue; + public float maxValue; + } +} \ No newline at end of file diff --git a/Editor/CustomInspectors/AudioEventInspector.cs b/Editor/CustomInspectors/AudioEventInspector.cs new file mode 100644 index 0000000..be48d99 --- /dev/null +++ b/Editor/CustomInspectors/AudioEventInspector.cs @@ -0,0 +1,38 @@ +using UnityEditor; +using UnityEngine; +using Util.ScriptableObjects.Audio; + +namespace Editor.customInspector +{ + [CustomEditor(typeof(AudioEvent), true)] + public class AudioEventInspector : UnityEditor.Editor + { + [SerializeField] private AudioSource audioSource; + + private void OnEnable() + { + audioSource = + EditorUtility.CreateGameObjectWithHideFlags("Audio preview", HideFlags.HideAndDontSave, + typeof(AudioSource)).GetComponent(); + } + + private void OnDisable() + { + DestroyImmediate(audioSource.gameObject); + } + + + public override void OnInspectorGUI() + { + DrawDefaultInspector(); + + EditorGUI.BeginDisabledGroup(serializedObject.isEditingMultipleObjects); + if (GUILayout.Button("Preview")) + { + ((AudioEvent) target).Play(audioSource); + } + + EditorGUI.EndDisabledGroup(); + } + } +} \ No newline at end of file diff --git a/Editor/PropertyDrawers/RangedFloatDrawer.cs b/Editor/PropertyDrawers/RangedFloatDrawer.cs new file mode 100644 index 0000000..78f1511 --- /dev/null +++ b/Editor/PropertyDrawers/RangedFloatDrawer.cs @@ -0,0 +1,54 @@ +using UnityEditor; +using UnityEngine; +using Util.CustomTypes; + +namespace Editor.PropertyDrawer +{ + [CustomPropertyDrawer(typeof(RangedFloat), true)] + public class RangedFloatDrawer : UnityEditor.PropertyDrawer + { + public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) + { + label = EditorGUI.BeginProperty(position, label, property); + position = EditorGUI.PrefixLabel(position, label); + + var minProp = property.FindPropertyRelative("minValue"); + var maxProp = property.FindPropertyRelative("maxValue"); + + var minValue = minProp.floatValue; + var maxValue = maxProp.floatValue; + + float rangeMin = 0; + float rangeMax = 1; + + var ranges = (MinMaxRangeAttribute[]) fieldInfo.GetCustomAttributes(typeof(MinMaxRangeAttribute), true); + + if (ranges.Length > 0) + { + rangeMin = ranges[0].Min; + rangeMax = ranges[0].Max; + } + + const float rangeBoundsLabelWidth = 40f; + + var rangeBoundsLabel1Rect = new Rect(position) {width = rangeBoundsLabelWidth}; + GUI.Label(rangeBoundsLabel1Rect, new GUIContent(minValue.ToString("F2"))); + position.xMin += rangeBoundsLabelWidth; + + var rangeBoundsLabel2Rect = new Rect(position); + rangeBoundsLabel2Rect.xMin = rangeBoundsLabel2Rect.xMax - rangeBoundsLabelWidth + 5; + GUI.Label(rangeBoundsLabel2Rect, new GUIContent(maxValue.ToString("F2"))); + position.xMax -= rangeBoundsLabelWidth; + + EditorGUI.BeginChangeCheck(); + EditorGUI.MinMaxSlider(position, ref minValue, ref maxValue, rangeMin, rangeMax); + if (EditorGUI.EndChangeCheck()) + { + minProp.floatValue = minValue; + maxProp.floatValue = maxValue; + } + + EditorGUI.EndProperty(); + } + } +} \ No newline at end of file diff --git a/ScriptableObjects/Audio/AudioEvent.cs b/ScriptableObjects/Audio/AudioEvent.cs new file mode 100644 index 0000000..b67cf1a --- /dev/null +++ b/ScriptableObjects/Audio/AudioEvent.cs @@ -0,0 +1,9 @@ +using UnityEngine; + +namespace Util.ScriptableObjects.Audio +{ + public abstract class AudioEvent : ScriptableObject + { + public abstract void Play(AudioSource source); + } +} \ No newline at end of file diff --git a/ScriptableObjects/Audio/SimpleAudioEvent.cs b/ScriptableObjects/Audio/SimpleAudioEvent.cs new file mode 100644 index 0000000..519d35f --- /dev/null +++ b/ScriptableObjects/Audio/SimpleAudioEvent.cs @@ -0,0 +1,24 @@ +using UnityEngine; +using Util.CustomTypes; + +namespace Util.ScriptableObjects.Audio +{ + [CreateAssetMenu(menuName = "ScriptableObject/SimpleAudioEvent")] + public class SimpleAudioEvent : AudioEvent + { + public AudioClip[] clips; + public RangedFloat volume; + + [MinMaxRange(0, 2)] public RangedFloat pitch; + + public override void Play(AudioSource source) + { + if (clips.Length == 0) return; + + source.clip = clips[Random.Range(0, clips.Length)]; + source.volume = Random.Range(volume.minValue, volume.maxValue); + source.pitch = Random.Range(pitch.minValue, pitch.maxValue); + source.Play(); + } + } +} \ No newline at end of file diff --git a/UI/Components/ComponentBase.cs b/UI/Components/ComponentBase.cs new file mode 100644 index 0000000..276fe48 --- /dev/null +++ b/UI/Components/ComponentBase.cs @@ -0,0 +1,17 @@ +using UnityEngine; + +namespace Util.UiComponents +{ + public class ComponentBase : MonoBehaviour + { + public void Show() + { + gameObject.SetActive(true); + } + + public void Hide() + { + gameObject.SetActive(false); + } + } +} \ No newline at end of file diff --git a/UI/Components/SliderComponent.cs b/UI/Components/SliderComponent.cs new file mode 100644 index 0000000..929b797 --- /dev/null +++ b/UI/Components/SliderComponent.cs @@ -0,0 +1,33 @@ +using System; +using UnityEngine; +using UnityEngine.Events; +using UnityEngine.UI; + +namespace Util.UiComponents +{ + public class SliderComponent : ComponentBase + { + [SerializeField] private Slider slider; + + public void SetValue(float value, float max) + { + slider.maxValue = max; + slider.value = value; + } + + public float GetValue() + { + return slider.value; + } + + public void SetOnValueChanged(UnityAction callBack) + { + slider.onValueChanged.AddListener(callBack); + } + + private void OnDestroy() + { + slider.onValueChanged?.RemoveAllListeners(); + } + } +} \ No newline at end of file diff --git a/UI/Components/TextComponent.cs b/UI/Components/TextComponent.cs new file mode 100644 index 0000000..3db554e --- /dev/null +++ b/UI/Components/TextComponent.cs @@ -0,0 +1,15 @@ +using TMPro; +using UnityEngine; + +namespace Util.UiComponents +{ + public class TextComponent : ComponentBase + { + [SerializeField] private TMP_Text text; + + public void SetText(string newText) + { + text.text = newText; + } + } +} \ No newline at end of file diff --git a/Utils/ObjectPool.cs b/Utils/ObjectPool.cs new file mode 100644 index 0000000..71e69cc --- /dev/null +++ b/Utils/ObjectPool.cs @@ -0,0 +1,48 @@ +using System.Collections.Generic; +using UnityEngine; + +namespace Util +{ + public class ObjectPool where T : Object + { + private readonly Queue _pool; + private readonly List _prefab; + + public ObjectPool(IEnumerable poolPrefab) + { + _pool = new Queue(); + _prefab = new List(); + _prefab.AddRange(poolPrefab); + } + + /*public ObjectPool(IEnumerable prefabs) + { + _pool = new Queue(); + _prefab.AddRange(prefabs); + }*/ + + public void Pool(T objectToPool) + { + _pool.Enqueue(objectToPool); + } + + public T DePool() + { + if (_pool.Count > 0) + { + return _pool.Dequeue(); + } + + var randomNumber = Random.Range(0, _prefab.Count); + return Object.Instantiate(_prefab[randomNumber]); + } + + public void PoolAll(IEnumerable listOfObject) + { + foreach (var obj in listOfObject) + { + Pool(obj); + } + } + } +} \ No newline at end of file