Initial Unity project commit
This commit is contained in:
@@ -0,0 +1,511 @@
|
||||
using System.Collections;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
using UnityEngine.Events;
|
||||
using UnityEngine.EventSystems;
|
||||
#if ENABLE_INPUT_SYSTEM
|
||||
using UnityEngine.InputSystem;
|
||||
#endif
|
||||
using TMPro;
|
||||
|
||||
namespace Michsky.MUIP
|
||||
{
|
||||
[ExecuteInEditMode]
|
||||
public class ButtonManager : MonoBehaviour, IPointerClickHandler, IPointerDownHandler, IPointerUpHandler, IPointerEnterHandler, IPointerExitHandler, ISelectHandler, IDeselectHandler, ISubmitHandler
|
||||
{
|
||||
// Content
|
||||
public Sprite buttonIcon;
|
||||
public string buttonText = "Button";
|
||||
[Range(0.1f, 10)] public float iconScale = 1;
|
||||
[Range(10, 200)] public float textSize = 24;
|
||||
|
||||
// Auto Size
|
||||
public bool autoFitContent = true;
|
||||
public Padding padding;
|
||||
[Range(0, 100)] public int spacing = 15;
|
||||
public HorizontalLayoutGroup disabledLayout;
|
||||
public HorizontalLayoutGroup normalLayout;
|
||||
public HorizontalLayoutGroup highlightedLayout;
|
||||
[SerializeField] private HorizontalLayoutGroup mainLayout;
|
||||
[SerializeField] private ContentSizeFitter mainFitter;
|
||||
[SerializeField] private ContentSizeFitter targetFitter;
|
||||
[SerializeField] private RectTransform targetRect;
|
||||
|
||||
// Resources
|
||||
public CanvasGroup normalCG;
|
||||
public CanvasGroup highlightCG;
|
||||
public CanvasGroup disabledCG;
|
||||
public TextMeshProUGUI normalText;
|
||||
public TextMeshProUGUI highlightedText;
|
||||
public TextMeshProUGUI disabledText;
|
||||
public Image normalImage;
|
||||
public Image highlightImage;
|
||||
public Image disabledImage;
|
||||
public AudioSource soundSource;
|
||||
[SerializeField] private GameObject rippleParent;
|
||||
|
||||
// Settings
|
||||
public bool isInteractable = true;
|
||||
public bool enableIcon = false;
|
||||
public bool enableText = true;
|
||||
public bool useCustomContent = false;
|
||||
[SerializeField] private bool useCustomTextSize = false;
|
||||
public bool checkForDoubleClick = true;
|
||||
public bool enableButtonSounds = false;
|
||||
public bool useHoverSound = true;
|
||||
public bool useClickSound = true;
|
||||
public AudioClip hoverSound;
|
||||
public AudioClip clickSound;
|
||||
public bool useUINavigation = false;
|
||||
public Navigation.Mode navigationMode = Navigation.Mode.Automatic;
|
||||
public GameObject selectOnUp;
|
||||
public GameObject selectOnDown;
|
||||
public GameObject selectOnLeft;
|
||||
public GameObject selectOnRight;
|
||||
public bool wrapAround = false;
|
||||
public bool useRipple = true;
|
||||
[Range(0.1f, 1)] public float doubleClickPeriod = 0.25f;
|
||||
[Range(0.25f, 15)] public float fadingMultiplier = 8;
|
||||
[SerializeField] private AnimationSolution animationSolution = AnimationSolution.ScriptBased;
|
||||
|
||||
// Events
|
||||
public UnityEvent onClick = new UnityEvent();
|
||||
public UnityEvent onDoubleClick = new UnityEvent();
|
||||
public UnityEvent onHover = new UnityEvent();
|
||||
public UnityEvent onLeave = new UnityEvent();
|
||||
|
||||
// Ripple
|
||||
[SerializeField] private RippleUpdateMode rippleUpdateMode = RippleUpdateMode.UnscaledTime;
|
||||
[SerializeField] private Canvas targetCanvas;
|
||||
public Sprite rippleShape;
|
||||
[Range(0.1f, 5)] public float speed = 1f;
|
||||
[Range(0.5f, 25)] public float maxSize = 4f;
|
||||
public Color startColor = new Color(1f, 1f, 1f, 0.2f);
|
||||
public Color transitionColor = new Color(1f, 1f, 1f, 0f);
|
||||
[SerializeField] private bool renderOnTop = false;
|
||||
[SerializeField] private bool centered = false;
|
||||
|
||||
// Helpers
|
||||
bool isInitialized = false;
|
||||
Button targetButton;
|
||||
bool isPointerOn;
|
||||
bool waitingForDoubleClickInput;
|
||||
const int navHelper = 1;
|
||||
|
||||
#if UNITY_EDITOR
|
||||
public bool isPreset;
|
||||
public int latestTabIndex = 0;
|
||||
#endif
|
||||
|
||||
public enum AnimationSolution
|
||||
{
|
||||
Custom,
|
||||
ScriptBased
|
||||
}
|
||||
|
||||
public enum RippleUpdateMode
|
||||
{
|
||||
Normal,
|
||||
UnscaledTime
|
||||
}
|
||||
|
||||
[System.Serializable] public class Padding
|
||||
{
|
||||
public int left = 20;
|
||||
public int right = 20;
|
||||
public int top = 5;
|
||||
public int bottom = 5;
|
||||
}
|
||||
|
||||
void OnEnable()
|
||||
{
|
||||
if (!isInitialized) { Initialize(); }
|
||||
UpdateUI();
|
||||
}
|
||||
|
||||
void OnDisable()
|
||||
{
|
||||
if (!isInteractable)
|
||||
return;
|
||||
|
||||
if (disabledCG != null) { disabledCG.alpha = 0; }
|
||||
if (normalCG != null) { normalCG.alpha = 1; }
|
||||
if (highlightCG != null) { highlightCG.alpha = 0; }
|
||||
}
|
||||
|
||||
void Initialize()
|
||||
{
|
||||
#if UNITY_EDITOR
|
||||
if (!Application.isPlaying) { return; }
|
||||
#endif
|
||||
if (animationSolution == AnimationSolution.ScriptBased && TryGetComponent<Animator>(out var tempAnimator)) { Destroy(tempAnimator); }
|
||||
if (gameObject.GetComponent<Image>() == null)
|
||||
{
|
||||
Image raycastImg = gameObject.AddComponent<Image>();
|
||||
raycastImg.color = new Color(0, 0, 0, 0);
|
||||
raycastImg.raycastTarget = true;
|
||||
}
|
||||
|
||||
if (targetCanvas == null) { targetCanvas = GetComponentInParent<Canvas>(); }
|
||||
if (normalCG == null) { normalCG = new GameObject().AddComponent<CanvasGroup>(); normalCG.gameObject.AddComponent<RectTransform>(); normalCG.transform.SetParent(transform); normalCG.gameObject.name = "Normal"; }
|
||||
if (highlightCG == null) { highlightCG = new GameObject().AddComponent<CanvasGroup>(); highlightCG.gameObject.AddComponent<RectTransform>(); highlightCG.transform.SetParent(transform); highlightCG.gameObject.name = "Highlight"; }
|
||||
if (disabledCG == null) { disabledCG = new GameObject().AddComponent<CanvasGroup>(); disabledCG.gameObject.AddComponent<RectTransform>(); disabledCG.transform.SetParent(transform); disabledCG.gameObject.name = "Disabled"; }
|
||||
|
||||
if (useRipple && rippleParent != null) { rippleParent.SetActive(false); }
|
||||
else if (!useRipple && rippleParent != null) { Destroy(rippleParent); }
|
||||
|
||||
if (gameObject.activeInHierarchy) { StartCoroutine(nameof(LayoutFix)); }
|
||||
if (targetButton == null)
|
||||
{
|
||||
if (gameObject.GetComponent<Button>() == null) { targetButton = gameObject.AddComponent<Button>(); }
|
||||
else { targetButton = GetComponent<Button>(); }
|
||||
|
||||
targetButton.transition = Selectable.Transition.None;
|
||||
|
||||
Navigation customNav = new Navigation();
|
||||
customNav.mode = Navigation.Mode.None;
|
||||
targetButton.navigation = customNav;
|
||||
}
|
||||
if (useUINavigation) { AddUINavigation(); }
|
||||
|
||||
isInitialized = true;
|
||||
}
|
||||
|
||||
public void UpdateUI()
|
||||
{
|
||||
if (autoFitContent == false)
|
||||
{
|
||||
if (mainFitter != null) { mainFitter.enabled = false; }
|
||||
if (mainLayout != null) { mainLayout.enabled = false; }
|
||||
if (targetFitter != null)
|
||||
{
|
||||
targetFitter.enabled = false;
|
||||
|
||||
if (targetRect != null)
|
||||
{
|
||||
targetRect.anchorMin = new Vector2(0, 0);
|
||||
targetRect.anchorMax = new Vector2(1, 1);
|
||||
targetRect.offsetMin = new Vector2(0, 0);
|
||||
targetRect.offsetMax = new Vector2(0, 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
else
|
||||
{
|
||||
if (mainFitter != null) { mainFitter.enabled = true; }
|
||||
if (mainLayout != null) { mainLayout.enabled = true; }
|
||||
if (targetFitter != null) { targetFitter.enabled = true; }
|
||||
}
|
||||
|
||||
if (disabledLayout != null) { disabledLayout.padding = new RectOffset(padding.left, padding.right, padding.top, padding.bottom); disabledLayout.spacing = spacing; }
|
||||
if (normalLayout != null) { normalLayout.padding = new RectOffset(padding.left, padding.right, padding.top, padding.bottom); normalLayout.spacing = spacing; }
|
||||
if (highlightedLayout != null) { highlightedLayout.padding = new RectOffset(padding.left, padding.right, padding.top, padding.bottom); highlightedLayout.spacing = spacing; }
|
||||
|
||||
if (normalCG != null && isInteractable) { normalCG.alpha = 1; }
|
||||
if (disabledCG != null && !isInteractable) { disabledCG.alpha = 1; }
|
||||
if (highlightCG != null) { highlightCG.alpha = 0; }
|
||||
|
||||
if (!useCustomContent)
|
||||
{
|
||||
|
||||
if (enableText)
|
||||
{
|
||||
if (normalText != null)
|
||||
{
|
||||
normalText.gameObject.SetActive(true);
|
||||
normalText.text = buttonText;
|
||||
if (!useCustomTextSize) { normalText.fontSize = textSize; }
|
||||
}
|
||||
|
||||
if (highlightedText != null)
|
||||
{
|
||||
highlightedText.gameObject.SetActive(true);
|
||||
highlightedText.text = buttonText;
|
||||
if (!useCustomTextSize) { highlightedText.fontSize = textSize; }
|
||||
}
|
||||
|
||||
if (disabledText != null)
|
||||
{
|
||||
disabledText.gameObject.SetActive(true);
|
||||
disabledText.text = buttonText;
|
||||
if (!useCustomTextSize) { disabledText.fontSize = textSize; }
|
||||
}
|
||||
}
|
||||
|
||||
else if (!enableText)
|
||||
{
|
||||
if (normalText != null) { normalText.gameObject.SetActive(false); }
|
||||
if (highlightedText != null) { highlightedText.gameObject.SetActive(false); }
|
||||
if (disabledText != null) { disabledText.gameObject.SetActive(false); }
|
||||
}
|
||||
|
||||
if (enableIcon)
|
||||
{
|
||||
Vector3 tempScale = new Vector3(iconScale, iconScale, iconScale);
|
||||
if (normalImage != null) { normalImage.transform.parent.gameObject.SetActive(true); normalImage.sprite = buttonIcon; normalImage.transform.localScale = tempScale; }
|
||||
if (highlightImage != null) { highlightImage.transform.parent.gameObject.SetActive(true); highlightImage.sprite = buttonIcon; ; highlightImage.transform.localScale = tempScale; }
|
||||
if (disabledImage != null) { disabledImage.transform.parent.gameObject.SetActive(true); disabledImage.sprite = buttonIcon; ; disabledImage.transform.localScale = tempScale; }
|
||||
}
|
||||
|
||||
else
|
||||
{
|
||||
if (normalImage != null) { normalImage.transform.parent.gameObject.SetActive(false); }
|
||||
if (highlightImage != null) { highlightImage.transform.parent.gameObject.SetActive(false); }
|
||||
if (disabledImage != null) { disabledImage.transform.parent.gameObject.SetActive(false); }
|
||||
}
|
||||
}
|
||||
|
||||
#if UNITY_EDITOR
|
||||
if (!Application.isPlaying && autoFitContent)
|
||||
{
|
||||
LayoutRebuilder.ForceRebuildLayoutImmediate(GetComponent<RectTransform>());
|
||||
if (disabledCG != null) { LayoutRebuilder.ForceRebuildLayoutImmediate(disabledCG.GetComponent<RectTransform>()); }
|
||||
if (normalCG != null) { LayoutRebuilder.ForceRebuildLayoutImmediate(normalCG.GetComponent<RectTransform>()); }
|
||||
if (highlightCG != null) { LayoutRebuilder.ForceRebuildLayoutImmediate(highlightCG.GetComponent<RectTransform>()); }
|
||||
}
|
||||
#endif
|
||||
|
||||
if (!Application.isPlaying || !gameObject.activeInHierarchy) { return; }
|
||||
if (!isInteractable) { StartCoroutine(nameof(SetDisabled)); }
|
||||
else if (isInteractable && disabledCG.alpha == 1) { StartCoroutine(nameof(SetNormal)); }
|
||||
|
||||
StartCoroutine(nameof(LayoutFix));
|
||||
}
|
||||
|
||||
public void SetText(string text) { buttonText = text; UpdateUI(); }
|
||||
public void SetIcon(Sprite icon) { buttonIcon = icon; UpdateUI(); }
|
||||
|
||||
public void Interactable(bool value)
|
||||
{
|
||||
isInteractable = value;
|
||||
|
||||
if (!gameObject.activeInHierarchy) { return; }
|
||||
if (!isInteractable) { StartCoroutine(nameof(SetDisabled)); }
|
||||
else if (isInteractable && disabledCG.alpha == 1) { StartCoroutine(nameof(SetNormal)); }
|
||||
}
|
||||
|
||||
public void AddUINavigation()
|
||||
{
|
||||
if (targetButton == null)
|
||||
return;
|
||||
|
||||
targetButton.transition = Selectable.Transition.None;
|
||||
Navigation customNav = new Navigation
|
||||
{
|
||||
mode = navigationMode
|
||||
};
|
||||
|
||||
if (navigationMode == Navigation.Mode.Vertical || navigationMode == Navigation.Mode.Horizontal) { customNav.wrapAround = wrapAround; }
|
||||
else if (navigationMode == Navigation.Mode.Explicit) { StartCoroutine(nameof(InitUINavigation), customNav); return; }
|
||||
|
||||
targetButton.navigation = customNav;
|
||||
}
|
||||
|
||||
public void CreateRipple(Vector2 pos)
|
||||
{
|
||||
if (rippleParent != null)
|
||||
{
|
||||
GameObject rippleObj = new GameObject();
|
||||
rippleObj.AddComponent<Image>();
|
||||
rippleObj.GetComponent<Image>().sprite = rippleShape;
|
||||
rippleObj.name = "Ripple";
|
||||
rippleParent.SetActive(true);
|
||||
rippleObj.transform.SetParent(rippleParent.transform);
|
||||
|
||||
if (renderOnTop == true) { rippleParent.transform.SetAsLastSibling(); }
|
||||
else { rippleParent.transform.SetAsFirstSibling(); }
|
||||
|
||||
if (centered == true) { rippleObj.transform.localPosition = new Vector2(0f, 0f); }
|
||||
else { rippleObj.transform.position = pos; }
|
||||
|
||||
rippleObj.AddComponent<Ripple>();
|
||||
Ripple tempRipple = rippleObj.GetComponent<Ripple>();
|
||||
tempRipple.speed = speed;
|
||||
tempRipple.maxSize = maxSize;
|
||||
tempRipple.startColor = startColor;
|
||||
tempRipple.transitionColor = transitionColor;
|
||||
|
||||
if (rippleUpdateMode == RippleUpdateMode.Normal) { tempRipple.unscaledTime = false; }
|
||||
else { tempRipple.unscaledTime = true; }
|
||||
}
|
||||
}
|
||||
|
||||
public void OnPointerClick(PointerEventData eventData)
|
||||
{
|
||||
if (!isInteractable || eventData.button != PointerEventData.InputButton.Left) { return; }
|
||||
if (enableButtonSounds && useClickSound == true && soundSource != null) { soundSource.PlayOneShot(clickSound); }
|
||||
|
||||
// Invoke click actions
|
||||
onClick.Invoke();
|
||||
|
||||
// Check for double click
|
||||
if (checkForDoubleClick == false || !gameObject.activeInHierarchy) { return; }
|
||||
if (waitingForDoubleClickInput == true)
|
||||
{
|
||||
onDoubleClick.Invoke();
|
||||
waitingForDoubleClickInput = false;
|
||||
return;
|
||||
}
|
||||
|
||||
waitingForDoubleClickInput = true;
|
||||
|
||||
StopCoroutine("CheckForDoubleClick");
|
||||
StartCoroutine("CheckForDoubleClick");
|
||||
}
|
||||
|
||||
public void OnPointerDown(PointerEventData eventData)
|
||||
{
|
||||
if (!isInteractable) { return; }
|
||||
#if UNITY_IOS || UNITY_ANDROID
|
||||
if (animationSolution == AnimationSolution.ScriptBased) { StartCoroutine(nameof(SetHighlight)); }
|
||||
if (useRipple)
|
||||
#else
|
||||
if (useRipple && isPointerOn)
|
||||
#endif
|
||||
#if ENABLE_LEGACY_INPUT_MANAGER
|
||||
if (targetCanvas != null && (targetCanvas.renderMode == RenderMode.ScreenSpaceCamera || targetCanvas.renderMode == RenderMode.WorldSpace)) { CreateRipple(targetCanvas.worldCamera.ScreenToWorldPoint(Input.mousePosition)); }
|
||||
else { CreateRipple(Input.mousePosition); }
|
||||
#elif ENABLE_INPUT_SYSTEM
|
||||
if (targetCanvas != null && (targetCanvas.renderMode == RenderMode.ScreenSpaceCamera || targetCanvas.renderMode == RenderMode.WorldSpace)) { CreateRipple(targetCanvas.worldCamera.ScreenToWorldPoint(Mouse.current.position.ReadValue())); }
|
||||
#if UNITY_IOS || UNITY_ANDROID
|
||||
else { CreateRipple(Touchscreen.current.primaryTouch.position.ReadValue()); }
|
||||
#else
|
||||
else { CreateRipple(Mouse.current.position.ReadValue()); }
|
||||
#endif
|
||||
#endif
|
||||
}
|
||||
|
||||
public void OnPointerUp(PointerEventData eventData)
|
||||
{
|
||||
#if UNITY_IOS || UNITY_ANDROID
|
||||
if (!isInteractable) { return; }
|
||||
if (animationSolution == AnimationSolution.ScriptBased) { StartCoroutine(nameof(SetNormal)); }
|
||||
#endif
|
||||
}
|
||||
|
||||
public void OnPointerEnter(PointerEventData eventData)
|
||||
{
|
||||
if (!isInteractable) { return; }
|
||||
if (enableButtonSounds && useHoverSound && soundSource != null) { soundSource.PlayOneShot(hoverSound); }
|
||||
if (animationSolution == AnimationSolution.ScriptBased) { StartCoroutine(nameof(SetHighlight)); }
|
||||
|
||||
isPointerOn = true;
|
||||
onHover.Invoke();
|
||||
}
|
||||
|
||||
public void OnPointerExit(PointerEventData eventData)
|
||||
{
|
||||
if (!isInteractable) { return; }
|
||||
if (animationSolution == AnimationSolution.ScriptBased) { StartCoroutine(nameof(SetNormal)); }
|
||||
|
||||
isPointerOn = false;
|
||||
onLeave.Invoke();
|
||||
}
|
||||
|
||||
public void OnSelect(BaseEventData eventData)
|
||||
{
|
||||
if (!isInteractable) { return; }
|
||||
if (animationSolution == AnimationSolution.ScriptBased) { StartCoroutine(nameof(SetHighlight)); }
|
||||
if (useUINavigation) { onHover.Invoke(); }
|
||||
}
|
||||
|
||||
public void OnDeselect(BaseEventData eventData)
|
||||
{
|
||||
if (!isInteractable) { return; }
|
||||
if (animationSolution == AnimationSolution.ScriptBased) { StartCoroutine(nameof(SetNormal)); }
|
||||
if (useUINavigation) { onLeave.Invoke(); }
|
||||
}
|
||||
|
||||
public void OnSubmit(BaseEventData eventData)
|
||||
{
|
||||
if (!isInteractable) { return; }
|
||||
if (animationSolution == AnimationSolution.ScriptBased) { StartCoroutine(nameof(SetNormal)); }
|
||||
|
||||
onClick.Invoke();
|
||||
}
|
||||
|
||||
IEnumerator LayoutFix()
|
||||
{
|
||||
yield return new WaitForSecondsRealtime(0.025f);
|
||||
|
||||
LayoutRebuilder.ForceRebuildLayoutImmediate(GetComponent<RectTransform>());
|
||||
|
||||
if (disabledCG != null) { LayoutRebuilder.ForceRebuildLayoutImmediate(disabledCG.GetComponent<RectTransform>()); }
|
||||
if (normalCG != null) { LayoutRebuilder.ForceRebuildLayoutImmediate(normalCG.GetComponent<RectTransform>()); }
|
||||
if (highlightCG != null) { LayoutRebuilder.ForceRebuildLayoutImmediate(highlightCG.GetComponent<RectTransform>()); }
|
||||
}
|
||||
|
||||
IEnumerator SetNormal()
|
||||
{
|
||||
StopCoroutine(nameof(SetHighlight));
|
||||
StopCoroutine(nameof(SetDisabled));
|
||||
|
||||
while (normalCG.alpha < 0.99f)
|
||||
{
|
||||
normalCG.alpha += Time.unscaledDeltaTime * fadingMultiplier;
|
||||
highlightCG.alpha -= Time.unscaledDeltaTime * fadingMultiplier;
|
||||
disabledCG.alpha -= Time.unscaledDeltaTime * fadingMultiplier;
|
||||
yield return null;
|
||||
}
|
||||
|
||||
normalCG.alpha = 1;
|
||||
highlightCG.alpha = 0;
|
||||
disabledCG.alpha = 0;
|
||||
}
|
||||
|
||||
IEnumerator SetHighlight()
|
||||
{
|
||||
StopCoroutine(nameof(SetNormal));
|
||||
StopCoroutine(nameof(SetDisabled));
|
||||
|
||||
while (highlightCG.alpha < 0.99f)
|
||||
{
|
||||
normalCG.alpha -= Time.unscaledDeltaTime * fadingMultiplier;
|
||||
highlightCG.alpha += Time.unscaledDeltaTime * fadingMultiplier;
|
||||
disabledCG.alpha -= Time.unscaledDeltaTime * fadingMultiplier;
|
||||
yield return null;
|
||||
}
|
||||
|
||||
normalCG.alpha = 0;
|
||||
highlightCG.alpha = 1;
|
||||
disabledCG.alpha = 0;
|
||||
}
|
||||
|
||||
IEnumerator SetDisabled()
|
||||
{
|
||||
StopCoroutine(nameof(SetNormal));
|
||||
StopCoroutine(nameof(SetHighlight));
|
||||
|
||||
while (disabledCG.alpha < 0.99f)
|
||||
{
|
||||
normalCG.alpha -= Time.unscaledDeltaTime * fadingMultiplier;
|
||||
highlightCG.alpha -= Time.unscaledDeltaTime * fadingMultiplier;
|
||||
disabledCG.alpha += Time.unscaledDeltaTime * fadingMultiplier;
|
||||
yield return null;
|
||||
}
|
||||
|
||||
normalCG.alpha = 0;
|
||||
highlightCG.alpha = 0;
|
||||
disabledCG.alpha = 1;
|
||||
}
|
||||
|
||||
IEnumerator CheckForDoubleClick()
|
||||
{
|
||||
yield return new WaitForSecondsRealtime(doubleClickPeriod);
|
||||
waitingForDoubleClickInput = false;
|
||||
}
|
||||
|
||||
IEnumerator InitUINavigation(Navigation nav)
|
||||
{
|
||||
yield return new WaitForSecondsRealtime(navHelper);
|
||||
|
||||
if (selectOnUp != null) { nav.selectOnUp = selectOnUp.GetComponent<Selectable>(); }
|
||||
if (selectOnDown != null) { nav.selectOnDown = selectOnDown.GetComponent<Selectable>(); }
|
||||
if (selectOnLeft != null) { nav.selectOnLeft = selectOnLeft.GetComponent<Selectable>(); }
|
||||
if (selectOnRight != null) { nav.selectOnRight = selectOnRight.GetComponent<Selectable>(); }
|
||||
|
||||
targetButton.navigation = nav;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 5d218e07e7315e243acdfcc700d01eb5
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences:
|
||||
- buttonIcon: {instanceID: 0}
|
||||
- hoverSound: {instanceID: 0}
|
||||
- clickSound: {instanceID: 0}
|
||||
- normalCG: {instanceID: 0}
|
||||
- highlightCG: {instanceID: 0}
|
||||
- disabledCG: {instanceID: 0}
|
||||
- normalText: {instanceID: 0}
|
||||
- highlightedText: {instanceID: 0}
|
||||
- disabledText: {instanceID: 0}
|
||||
- normalImage: {instanceID: 0}
|
||||
- highlightImage: {instanceID: 0}
|
||||
- disabledImage: {instanceID: 0}
|
||||
- soundSource: {instanceID: 0}
|
||||
- rippleParent: {instanceID: 0}
|
||||
- rippleShape: {fileID: 21300000, guid: 100d5727b1babc14bac90fe9c56af800, type: 3}
|
||||
executionOrder: 0
|
||||
icon: {fileID: 2800000, guid: 98d001ce6b3b53242911dcc3d1415f59, type: 3}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
AssetOrigin:
|
||||
serializedVersion: 1
|
||||
productId: 201717
|
||||
packageName: Modern UI Pack
|
||||
packageVersion: 5.5.28
|
||||
assetPath: Assets/Modern UI Pack/Scripts/Button/ButtonManager.cs
|
||||
uploadId: 778406
|
||||
@@ -0,0 +1,357 @@
|
||||
#if UNITY_EDITOR
|
||||
using UnityEngine;
|
||||
using UnityEditor;
|
||||
|
||||
namespace Michsky.MUIP
|
||||
{
|
||||
[CanEditMultipleObjects]
|
||||
[CustomEditor(typeof(ButtonManager))]
|
||||
public class ButtonManagerEditor : Editor
|
||||
{
|
||||
private GUISkin customSkin;
|
||||
private ButtonManager buttonTarget;
|
||||
private UIManagerButton tempUIM;
|
||||
|
||||
private void OnEnable()
|
||||
{
|
||||
buttonTarget = (ButtonManager)target;
|
||||
|
||||
try { tempUIM = buttonTarget.GetComponent<UIManagerButton>(); }
|
||||
catch { }
|
||||
|
||||
if (EditorGUIUtility.isProSkin == true) { customSkin = MUIPEditorHandler.GetDarkEditor(customSkin); }
|
||||
else { customSkin = MUIPEditorHandler.GetLightEditor(customSkin); }
|
||||
}
|
||||
|
||||
public override void OnInspectorGUI()
|
||||
{
|
||||
MUIPEditorHandler.DrawComponentHeader(customSkin, "Button Top Header");
|
||||
|
||||
GUIContent[] toolbarTabs = new GUIContent[3];
|
||||
toolbarTabs[0] = new GUIContent("Content");
|
||||
toolbarTabs[1] = new GUIContent("Resources");
|
||||
toolbarTabs[2] = new GUIContent("Settings");
|
||||
|
||||
buttonTarget.latestTabIndex = MUIPEditorHandler.DrawTabs(buttonTarget.latestTabIndex, toolbarTabs, customSkin);
|
||||
|
||||
if (GUILayout.Button(new GUIContent("Content", "Content"), customSkin.FindStyle("Tab Content")))
|
||||
buttonTarget.latestTabIndex = 0;
|
||||
if (GUILayout.Button(new GUIContent("Resources", "Resources"), customSkin.FindStyle("Tab Resources")))
|
||||
buttonTarget.latestTabIndex = 1;
|
||||
if (GUILayout.Button(new GUIContent("Settings", "Settings"), customSkin.FindStyle("Tab Settings")))
|
||||
buttonTarget.latestTabIndex = 2;
|
||||
|
||||
GUILayout.EndHorizontal();
|
||||
|
||||
var normalCG = serializedObject.FindProperty("normalCG");
|
||||
var highlightCG = serializedObject.FindProperty("highlightCG");
|
||||
var disabledCG = serializedObject.FindProperty("disabledCG");
|
||||
var normalText = serializedObject.FindProperty("normalText");
|
||||
var highlightedText = serializedObject.FindProperty("highlightedText");
|
||||
var disabledText = serializedObject.FindProperty("disabledText");
|
||||
var normalImageObj = serializedObject.FindProperty("normalImage");
|
||||
var highlightImageObj = serializedObject.FindProperty("highlightImage");
|
||||
var disabledImageObj = serializedObject.FindProperty("disabledImage");
|
||||
var rippleParent = serializedObject.FindProperty("rippleParent");
|
||||
var soundSource = serializedObject.FindProperty("soundSource");
|
||||
|
||||
var buttonIcon = serializedObject.FindProperty("buttonIcon");
|
||||
var buttonText = serializedObject.FindProperty("buttonText");
|
||||
var iconScale = serializedObject.FindProperty("iconScale");
|
||||
var textSize = serializedObject.FindProperty("textSize");
|
||||
var hoverSound = serializedObject.FindProperty("hoverSound");
|
||||
var clickSound = serializedObject.FindProperty("clickSound");
|
||||
|
||||
var autoFitContent = serializedObject.FindProperty("autoFitContent");
|
||||
var padding = serializedObject.FindProperty("padding");
|
||||
var spacing = serializedObject.FindProperty("spacing");
|
||||
var disabledLayout = serializedObject.FindProperty("disabledLayout");
|
||||
var normalLayout = serializedObject.FindProperty("normalLayout");
|
||||
var highlightedLayout = serializedObject.FindProperty("highlightedLayout");
|
||||
var mainLayout = serializedObject.FindProperty("mainLayout");
|
||||
var mainFitter = serializedObject.FindProperty("mainFitter");
|
||||
var targetFitter = serializedObject.FindProperty("targetFitter");
|
||||
var targetRect = serializedObject.FindProperty("targetRect");
|
||||
|
||||
var isInteractable = serializedObject.FindProperty("isInteractable");
|
||||
var enableIcon = serializedObject.FindProperty("enableIcon");
|
||||
var enableText = serializedObject.FindProperty("enableText");
|
||||
var useCustomIconSize = serializedObject.FindProperty("useCustomIconSize");
|
||||
var useCustomTextSize = serializedObject.FindProperty("useCustomTextSize");
|
||||
var useUINavigation = serializedObject.FindProperty("useUINavigation");
|
||||
var navigationMode = serializedObject.FindProperty("navigationMode");
|
||||
var wrapAround = serializedObject.FindProperty("wrapAround");
|
||||
var selectOnUp = serializedObject.FindProperty("selectOnUp");
|
||||
var selectOnDown = serializedObject.FindProperty("selectOnDown");
|
||||
var selectOnLeft = serializedObject.FindProperty("selectOnLeft");
|
||||
var selectOnRight = serializedObject.FindProperty("selectOnRight");
|
||||
var checkForDoubleClick = serializedObject.FindProperty("checkForDoubleClick");
|
||||
var enableButtonSounds = serializedObject.FindProperty("enableButtonSounds");
|
||||
var useHoverSound = serializedObject.FindProperty("useHoverSound");
|
||||
var useClickSound = serializedObject.FindProperty("useClickSound");
|
||||
var useRipple = serializedObject.FindProperty("useRipple");
|
||||
var fadingMultiplier = serializedObject.FindProperty("fadingMultiplier");
|
||||
var doubleClickPeriod = serializedObject.FindProperty("doubleClickPeriod");
|
||||
var animationSolution = serializedObject.FindProperty("animationSolution");
|
||||
var useCustomContent = serializedObject.FindProperty("useCustomContent");
|
||||
|
||||
var renderOnTop = serializedObject.FindProperty("renderOnTop");
|
||||
var centered = serializedObject.FindProperty("centered");
|
||||
var rippleUpdateMode = serializedObject.FindProperty("rippleUpdateMode");
|
||||
var targetCanvas = serializedObject.FindProperty("targetCanvas");
|
||||
var rippleShape = serializedObject.FindProperty("rippleShape");
|
||||
var speed = serializedObject.FindProperty("speed");
|
||||
var maxSize = serializedObject.FindProperty("maxSize");
|
||||
var startColor = serializedObject.FindProperty("startColor");
|
||||
var transitionColor = serializedObject.FindProperty("transitionColor");
|
||||
|
||||
var onClick = serializedObject.FindProperty("onClick");
|
||||
var onDoubleClick = serializedObject.FindProperty("onDoubleClick");
|
||||
var onHover = serializedObject.FindProperty("onHover");
|
||||
var onLeave = serializedObject.FindProperty("onLeave");
|
||||
|
||||
switch (buttonTarget.latestTabIndex)
|
||||
{
|
||||
case 0:
|
||||
MUIPEditorHandler.DrawHeader(customSkin, "Content Header", 6);
|
||||
|
||||
if (useCustomContent.boolValue == false)
|
||||
{
|
||||
GUILayout.BeginVertical(EditorStyles.helpBox);
|
||||
GUILayout.Space(-3);
|
||||
|
||||
enableIcon.boolValue = MUIPEditorHandler.DrawTogglePlain(enableIcon.boolValue, customSkin, "Enable Icon");
|
||||
|
||||
GUILayout.Space(4);
|
||||
|
||||
if (enableIcon.boolValue == true)
|
||||
{
|
||||
MUIPEditorHandler.DrawPropertyCW(buttonIcon, customSkin, "Button Icon", 80);
|
||||
MUIPEditorHandler.DrawPropertyCW(iconScale, customSkin, "Icon Scale", 80);
|
||||
}
|
||||
|
||||
GUILayout.EndVertical();
|
||||
|
||||
GUILayout.BeginVertical(EditorStyles.helpBox);
|
||||
GUILayout.Space(-3);
|
||||
|
||||
enableText.boolValue = MUIPEditorHandler.DrawTogglePlain(enableText.boolValue, customSkin, "Enable Text");
|
||||
|
||||
GUILayout.Space(4);
|
||||
|
||||
if (enableText.boolValue == true)
|
||||
{
|
||||
MUIPEditorHandler.DrawPropertyCW(buttonText, customSkin, "Button Text", 80);
|
||||
if (useCustomTextSize.boolValue == false) { MUIPEditorHandler.DrawPropertyCW(textSize, customSkin, "Text Size", 80); }
|
||||
}
|
||||
|
||||
GUILayout.EndVertical();
|
||||
buttonTarget.UpdateUI();
|
||||
}
|
||||
|
||||
else { EditorGUILayout.HelpBox("'Use Custom Content' is enabled. Content is now managed manually.", MessageType.Info); }
|
||||
|
||||
GUILayout.BeginVertical(EditorStyles.helpBox);
|
||||
GUILayout.Space(-3);
|
||||
autoFitContent.boolValue = MUIPEditorHandler.DrawTogglePlain(autoFitContent.boolValue, customSkin, "Auto-Fit Content");
|
||||
GUILayout.Space(4);
|
||||
GUILayout.BeginHorizontal(EditorStyles.helpBox);
|
||||
EditorGUI.indentLevel = 1;
|
||||
EditorGUILayout.PropertyField(padding, new GUIContent(" Padding"), true);
|
||||
EditorGUI.indentLevel = 0;
|
||||
GUILayout.EndHorizontal();
|
||||
MUIPEditorHandler.DrawProperty(spacing, customSkin, "Spacing");
|
||||
GUILayout.EndVertical();
|
||||
|
||||
if (Application.isPlaying == true && GUILayout.Button("Refresh", customSkin.button)) { buttonTarget.UpdateUI(); }
|
||||
|
||||
MUIPEditorHandler.DrawHeader(customSkin, "Events Header", 10);
|
||||
EditorGUILayout.PropertyField(onClick, new GUIContent("On Click"), true);
|
||||
EditorGUILayout.PropertyField(onDoubleClick, new GUIContent("On Double Click"), true);
|
||||
EditorGUILayout.PropertyField(onHover, new GUIContent("On Hover"), true);
|
||||
EditorGUILayout.PropertyField(onLeave, new GUIContent("On Leave"), true);
|
||||
break;
|
||||
|
||||
case 1:
|
||||
MUIPEditorHandler.DrawHeader(customSkin, "Core Header", 6);
|
||||
MUIPEditorHandler.DrawProperty(normalCG, customSkin, "Normal CG");
|
||||
MUIPEditorHandler.DrawProperty(highlightCG, customSkin, "Highlight CG");
|
||||
MUIPEditorHandler.DrawProperty(disabledCG, customSkin, "Disabled CG");
|
||||
|
||||
if (enableText.boolValue == true)
|
||||
{
|
||||
MUIPEditorHandler.DrawProperty(normalText, customSkin, "Normal Text");
|
||||
MUIPEditorHandler.DrawProperty(highlightedText, customSkin, "Highlighted Text");
|
||||
MUIPEditorHandler.DrawProperty(disabledText, customSkin, "Disabled Text");
|
||||
}
|
||||
|
||||
if (enableIcon.boolValue == true)
|
||||
{
|
||||
MUIPEditorHandler.DrawProperty(normalImageObj, customSkin, "Normal Icon");
|
||||
MUIPEditorHandler.DrawProperty(highlightImageObj, customSkin, "Highlight Icon");
|
||||
MUIPEditorHandler.DrawProperty(disabledImageObj, customSkin, "Disabled Icon");
|
||||
}
|
||||
|
||||
MUIPEditorHandler.DrawProperty(disabledLayout, customSkin, "Disabled Layout");
|
||||
MUIPEditorHandler.DrawProperty(normalLayout, customSkin, "Normal Layout");
|
||||
MUIPEditorHandler.DrawProperty(highlightedLayout, customSkin, "Highlighted Layout");
|
||||
|
||||
if (autoFitContent.boolValue == true)
|
||||
{
|
||||
MUIPEditorHandler.DrawProperty(mainLayout, customSkin, "Main Layout");
|
||||
MUIPEditorHandler.DrawProperty(mainFitter, customSkin, "Main Fitter");
|
||||
MUIPEditorHandler.DrawProperty(targetFitter, customSkin, "Target Fitter");
|
||||
MUIPEditorHandler.DrawProperty(targetRect, customSkin, "Target Rect");
|
||||
}
|
||||
|
||||
if (useRipple.boolValue == true) { MUIPEditorHandler.DrawProperty(rippleParent, customSkin, "Ripple Parent"); }
|
||||
break;
|
||||
|
||||
case 2:
|
||||
MUIPEditorHandler.DrawHeader(customSkin, "Options Header", 6);
|
||||
MUIPEditorHandler.DrawProperty(animationSolution, customSkin, "Animation Solution");
|
||||
MUIPEditorHandler.DrawProperty(fadingMultiplier, customSkin, "Fading Multiplier");
|
||||
MUIPEditorHandler.DrawProperty(doubleClickPeriod, customSkin, "Double Click Period");
|
||||
isInteractable.boolValue = MUIPEditorHandler.DrawToggle(isInteractable.boolValue, customSkin, "Is Interactable");
|
||||
if (useCustomContent.boolValue == true || enableText.boolValue == false) { GUI.enabled = false; }
|
||||
useCustomTextSize.boolValue = MUIPEditorHandler.DrawToggle(useCustomTextSize.boolValue, customSkin, "Use Custom Text Size");
|
||||
GUI.enabled = true;
|
||||
useCustomContent.boolValue = MUIPEditorHandler.DrawToggle(useCustomContent.boolValue, customSkin, "Use Custom Content");
|
||||
checkForDoubleClick.boolValue = MUIPEditorHandler.DrawToggle(checkForDoubleClick.boolValue, customSkin, "Check For Double Click");
|
||||
|
||||
GUILayout.BeginVertical(EditorStyles.helpBox);
|
||||
GUILayout.Space(-3);
|
||||
|
||||
useUINavigation.boolValue = MUIPEditorHandler.DrawTogglePlain(useUINavigation.boolValue, customSkin, "Use UI Navigation");
|
||||
|
||||
GUILayout.Space(4);
|
||||
|
||||
if (useUINavigation.boolValue == true)
|
||||
{
|
||||
GUILayout.BeginVertical(EditorStyles.helpBox);
|
||||
MUIPEditorHandler.DrawPropertyPlain(navigationMode, customSkin, "Navigation Mode");
|
||||
|
||||
if (buttonTarget.navigationMode == UnityEngine.UI.Navigation.Mode.Horizontal)
|
||||
{
|
||||
EditorGUI.indentLevel = 1;
|
||||
// GUILayout.Space(-3);
|
||||
wrapAround.boolValue = MUIPEditorHandler.DrawToggle(wrapAround.boolValue, customSkin, "Wrap Around");
|
||||
// GUILayout.Space(4);
|
||||
EditorGUI.indentLevel = 0;
|
||||
}
|
||||
|
||||
else if (buttonTarget.navigationMode == UnityEngine.UI.Navigation.Mode.Vertical)
|
||||
{
|
||||
wrapAround.boolValue = MUIPEditorHandler.DrawTogglePlain(wrapAround.boolValue, customSkin, "Wrap Around");
|
||||
}
|
||||
|
||||
else if (buttonTarget.navigationMode == UnityEngine.UI.Navigation.Mode.Explicit)
|
||||
{
|
||||
EditorGUI.indentLevel = 1;
|
||||
MUIPEditorHandler.DrawPropertyPlain(selectOnUp, customSkin, "Select On Up");
|
||||
MUIPEditorHandler.DrawPropertyPlain(selectOnDown, customSkin, "Select On Down");
|
||||
MUIPEditorHandler.DrawPropertyPlain(selectOnLeft, customSkin, "Select On Left");
|
||||
MUIPEditorHandler.DrawPropertyPlain(selectOnRight, customSkin, "Select On Right");
|
||||
EditorGUI.indentLevel = 0;
|
||||
}
|
||||
|
||||
GUILayout.EndVertical();
|
||||
}
|
||||
|
||||
GUILayout.EndVertical();
|
||||
|
||||
GUILayout.BeginVertical(EditorStyles.helpBox);
|
||||
GUILayout.Space(-3);
|
||||
|
||||
enableButtonSounds.boolValue = MUIPEditorHandler.DrawTogglePlain(enableButtonSounds.boolValue, customSkin, "Enable Button Sounds");
|
||||
|
||||
GUILayout.Space(4);
|
||||
|
||||
if (enableButtonSounds.boolValue == true)
|
||||
{
|
||||
MUIPEditorHandler.DrawProperty(soundSource, customSkin, "Sound Source");
|
||||
if (useHoverSound.boolValue == true) { MUIPEditorHandler.DrawProperty(hoverSound, customSkin, "Hover Sound"); }
|
||||
if (useClickSound.boolValue == true) { MUIPEditorHandler.DrawProperty(clickSound, customSkin, "Click Sound"); }
|
||||
|
||||
useHoverSound.boolValue = MUIPEditorHandler.DrawToggle(useHoverSound.boolValue, customSkin, "Enable Hover Sound");
|
||||
useClickSound.boolValue = MUIPEditorHandler.DrawToggle(useClickSound.boolValue, customSkin, "Enable Click Sound");
|
||||
|
||||
if (buttonTarget.soundSource == null) { EditorGUILayout.HelpBox("'Sound Source' is missing.", MessageType.Warning); }
|
||||
}
|
||||
|
||||
GUILayout.EndVertical();
|
||||
|
||||
MUIPEditorHandler.DrawHeader(customSkin, "Customization Header", 10);
|
||||
GUILayout.BeginVertical(EditorStyles.helpBox);
|
||||
GUILayout.Space(-2);
|
||||
|
||||
useRipple.boolValue = MUIPEditorHandler.DrawTogglePlain(useRipple.boolValue, customSkin, "Use Ripple");
|
||||
|
||||
GUILayout.Space(4);
|
||||
|
||||
if (useRipple.boolValue == true)
|
||||
{
|
||||
renderOnTop.boolValue = MUIPEditorHandler.DrawToggle(renderOnTop.boolValue, customSkin, "Render On Top");
|
||||
centered.boolValue = MUIPEditorHandler.DrawToggle(centered.boolValue, customSkin, "Centered");
|
||||
MUIPEditorHandler.DrawProperty(rippleUpdateMode, customSkin, "Update Mode");
|
||||
MUIPEditorHandler.DrawProperty(targetCanvas, customSkin, "Target Canvas");
|
||||
MUIPEditorHandler.DrawProperty(rippleShape, customSkin, "Shape");
|
||||
MUIPEditorHandler.DrawProperty(speed, customSkin, "Speed");
|
||||
MUIPEditorHandler.DrawProperty(maxSize, customSkin, "Max Size");
|
||||
MUIPEditorHandler.DrawProperty(startColor, customSkin, "Start Color");
|
||||
MUIPEditorHandler.DrawProperty(transitionColor, customSkin, "Transition Color");
|
||||
}
|
||||
|
||||
GUILayout.EndVertical();
|
||||
MUIPEditorHandler.DrawHeader(customSkin, "UIM Header", 10);
|
||||
|
||||
if (tempUIM != null)
|
||||
{
|
||||
MUIPEditorHandler.DrawUIManagerConnectedHeader();
|
||||
tempUIM.overrideColors = MUIPEditorHandler.DrawToggle(tempUIM.overrideColors, customSkin, "Override Colors");
|
||||
tempUIM.overrideFonts = MUIPEditorHandler.DrawToggle(tempUIM.overrideFonts, customSkin, "Override Fonts");
|
||||
|
||||
if (GUILayout.Button("Open UI Manager", customSkin.button))
|
||||
EditorApplication.ExecuteMenuItem(MUIPEditorHandler.UIM_SHORTCUT);
|
||||
|
||||
if (GUILayout.Button("Disable UI Manager Connection", customSkin.button))
|
||||
{
|
||||
if (EditorUtility.DisplayDialog("Modern UI Pack", "Are you sure you want to disable UI Manager connection with the object? " +
|
||||
"This operation cannot be undone.", "Yes", "Cancel"))
|
||||
{
|
||||
try { DestroyImmediate(tempUIM); }
|
||||
catch { Debug.LogError("<b>[Horizontal Selector]</b> Failed to delete UI Manager connection.", this); }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
else if (tempUIM == null)
|
||||
{
|
||||
if (buttonTarget.isPreset == true) { MUIPEditorHandler.DrawUIManagerPresetHeader(); }
|
||||
else
|
||||
{
|
||||
MUIPEditorHandler.DrawUIManagerDisconnectedHeader();
|
||||
|
||||
if (GUILayout.Button("Restore UI Manager", customSkin.button))
|
||||
{
|
||||
UIManagerButton uimb = buttonTarget.gameObject.AddComponent<UIManagerButton>();
|
||||
|
||||
try
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
catch { DestroyImmediate(uimb); Debug.LogError("<b>[Modern UI Pack]</b> Cannot restore the UI Manager connection."); }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
if (Application.isPlaying == false) { this.Repaint(); }
|
||||
serializedObject.ApplyModifiedProperties();
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
+18
@@ -0,0 +1,18 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 4027e626241ca4140aa30808faae978a
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
AssetOrigin:
|
||||
serializedVersion: 1
|
||||
productId: 201717
|
||||
packageName: Modern UI Pack
|
||||
packageVersion: 5.5.28
|
||||
assetPath: Assets/Modern UI Pack/Scripts/Button/ButtonManagerEditor.cs
|
||||
uploadId: 778406
|
||||
Reference in New Issue
Block a user