Added Monkey project reusable utils

This commit is contained in:
Martin 2020-04-29 22:08:44 -04:00
parent d61b3e55b0
commit ddd8e232a7
10 changed files with 262 additions and 0 deletions

View File

@ -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;
}
}

View File

@ -0,0 +1,11 @@
using System;
namespace Util.CustomTypes
{
[Serializable]
public struct RangedFloat
{
public float minValue;
public float maxValue;
}
}

View File

@ -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<AudioSource>();
}
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();
}
}
}

View File

@ -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();
}
}
}

View File

@ -0,0 +1,9 @@
using UnityEngine;
namespace Util.ScriptableObjects.Audio
{
public abstract class AudioEvent : ScriptableObject
{
public abstract void Play(AudioSource source);
}
}

View File

@ -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();
}
}
}

View File

@ -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);
}
}
}

View File

@ -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<float> callBack)
{
slider.onValueChanged.AddListener(callBack);
}
private void OnDestroy()
{
slider.onValueChanged?.RemoveAllListeners();
}
}
}

View File

@ -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;
}
}
}

48
Utils/ObjectPool.cs Normal file
View File

@ -0,0 +1,48 @@
using System.Collections.Generic;
using UnityEngine;
namespace Util
{
public class ObjectPool<T> where T : Object
{
private readonly Queue<T> _pool;
private readonly List<T> _prefab;
public ObjectPool(IEnumerable<T> poolPrefab)
{
_pool = new Queue<T>();
_prefab = new List<T>();
_prefab.AddRange(poolPrefab);
}
/*public ObjectPool(IEnumerable<T> prefabs)
{
_pool = new Queue<T>();
_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<T> listOfObject)
{
foreach (var obj in listOfObject)
{
Pool(obj);
}
}
}
}