mirror of
https://github.com/ConjureETS/Unity_Utils.git
synced 2026-03-23 20:40:58 +00:00
Added Monkey project reusable utils
This commit is contained in:
parent
d61b3e55b0
commit
ddd8e232a7
13
Attributes/MinMaxRangeAttribute.cs
Normal file
13
Attributes/MinMaxRangeAttribute.cs
Normal 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;
|
||||
}
|
||||
}
|
||||
11
CustomTypes/RangedFloat.cs
Normal file
11
CustomTypes/RangedFloat.cs
Normal file
@ -0,0 +1,11 @@
|
||||
using System;
|
||||
|
||||
namespace Util.CustomTypes
|
||||
{
|
||||
[Serializable]
|
||||
public struct RangedFloat
|
||||
{
|
||||
public float minValue;
|
||||
public float maxValue;
|
||||
}
|
||||
}
|
||||
38
Editor/CustomInspectors/AudioEventInspector.cs
Normal file
38
Editor/CustomInspectors/AudioEventInspector.cs
Normal 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();
|
||||
}
|
||||
}
|
||||
}
|
||||
54
Editor/PropertyDrawers/RangedFloatDrawer.cs
Normal file
54
Editor/PropertyDrawers/RangedFloatDrawer.cs
Normal 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();
|
||||
}
|
||||
}
|
||||
}
|
||||
9
ScriptableObjects/Audio/AudioEvent.cs
Normal file
9
ScriptableObjects/Audio/AudioEvent.cs
Normal file
@ -0,0 +1,9 @@
|
||||
using UnityEngine;
|
||||
|
||||
namespace Util.ScriptableObjects.Audio
|
||||
{
|
||||
public abstract class AudioEvent : ScriptableObject
|
||||
{
|
||||
public abstract void Play(AudioSource source);
|
||||
}
|
||||
}
|
||||
24
ScriptableObjects/Audio/SimpleAudioEvent.cs
Normal file
24
ScriptableObjects/Audio/SimpleAudioEvent.cs
Normal 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();
|
||||
}
|
||||
}
|
||||
}
|
||||
17
UI/Components/ComponentBase.cs
Normal file
17
UI/Components/ComponentBase.cs
Normal 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
33
UI/Components/SliderComponent.cs
Normal file
33
UI/Components/SliderComponent.cs
Normal 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();
|
||||
}
|
||||
}
|
||||
}
|
||||
15
UI/Components/TextComponent.cs
Normal file
15
UI/Components/TextComponent.cs
Normal 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
48
Utils/ObjectPool.cs
Normal 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user