基础内容
必要插件安装 缓动曲线和动画基础 ElementFolder,Track与其次级模块,PathNode重构
This commit is contained in:
138
Assets/Modern UI Pack/Scripts/Tooltip/TooltipContent.cs
Normal file
138
Assets/Modern UI Pack/Scripts/Tooltip/TooltipContent.cs
Normal file
@@ -0,0 +1,138 @@
|
||||
using System.Collections;
|
||||
using UnityEngine;
|
||||
using UnityEngine.EventSystems;
|
||||
using UnityEngine.UI;
|
||||
using TMPro;
|
||||
|
||||
namespace Michsky.MUIP
|
||||
{
|
||||
[AddComponentMenu("Modern UI Pack/Tooltip/Tooltip Content")]
|
||||
public class TooltipContent : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler
|
||||
{
|
||||
[Header("Content")]
|
||||
[TextArea] public string description;
|
||||
public float delay;
|
||||
|
||||
[Header("Resources")]
|
||||
public GameObject tooltipRect;
|
||||
public TextMeshProUGUI descriptionText;
|
||||
|
||||
[Header("Settings")]
|
||||
public bool forceToUpdate = false;
|
||||
public bool useIn3D = false;
|
||||
|
||||
TooltipManager tpManager;
|
||||
[HideInInspector] public Animator tooltipAnimator;
|
||||
|
||||
void Start()
|
||||
{
|
||||
if (tooltipRect == null || descriptionText == null)
|
||||
{
|
||||
try
|
||||
{
|
||||
tooltipRect = GameObject.Find("Tooltip Rect");
|
||||
descriptionText = tooltipRect.transform.GetComponentInChildren<TextMeshProUGUI>();
|
||||
}
|
||||
|
||||
catch { Debug.LogError("<b>[Tooltip Content]</b> Tooltip Rect is missing.", this); return; }
|
||||
}
|
||||
|
||||
if (tooltipRect != null)
|
||||
{
|
||||
tpManager = tooltipRect.GetComponentInParent<TooltipManager>();
|
||||
tooltipAnimator = tooltipRect.GetComponentInParent<Animator>();
|
||||
}
|
||||
|
||||
if (tpManager.contentLE == null)
|
||||
tpManager.contentLE = descriptionText.GetComponent<LayoutElement>();
|
||||
}
|
||||
|
||||
public void ProcessEnter()
|
||||
{
|
||||
if (tooltipRect == null)
|
||||
return;
|
||||
|
||||
descriptionText.text = description;
|
||||
|
||||
tpManager.allowUpdate = true;
|
||||
tpManager.currentTooltip = this;
|
||||
|
||||
CheckForContentWidth();
|
||||
|
||||
StopCoroutine("DisableAnimator");
|
||||
tooltipAnimator.gameObject.SetActive(false);
|
||||
tooltipAnimator.gameObject.SetActive(true);
|
||||
|
||||
if (delay == 0) { tooltipAnimator.Play("In"); }
|
||||
else { StartCoroutine("ShowTooltip"); }
|
||||
|
||||
if (forceToUpdate == true)
|
||||
StartCoroutine("UpdateLayoutPosition");
|
||||
}
|
||||
|
||||
public void ProcessExit()
|
||||
{
|
||||
if (tooltipRect == null)
|
||||
return;
|
||||
|
||||
if (delay != 0)
|
||||
{
|
||||
StopCoroutine("ShowTooltip");
|
||||
|
||||
if (tooltipAnimator.GetCurrentAnimatorStateInfo(0).IsName("In"))
|
||||
tooltipAnimator.Play("Out");
|
||||
}
|
||||
|
||||
else { tooltipAnimator.Play("Out"); }
|
||||
|
||||
tpManager.allowUpdate = false;
|
||||
}
|
||||
|
||||
public void OnPointerEnter(PointerEventData eventData) { ProcessEnter(); }
|
||||
public void OnPointerExit(PointerEventData eventData) { ProcessExit(); }
|
||||
|
||||
#if !UNITY_IOS && !UNITY_ANDROID
|
||||
public void OnMouseEnter() { if (useIn3D == true) { ProcessEnter(); } }
|
||||
public void OnMouseExit() { if (useIn3D == true) { ProcessExit(); } }
|
||||
#endif
|
||||
|
||||
public void CheckForContentWidth() { LayoutElementCreator(); StartCoroutine("CalculateContentWidth"); }
|
||||
|
||||
private void LayoutElementCreator()
|
||||
{
|
||||
if (tpManager.contentLE == null)
|
||||
{
|
||||
descriptionText.gameObject.AddComponent<LayoutElement>();
|
||||
tpManager.contentLE = descriptionText.GetComponent<LayoutElement>();
|
||||
}
|
||||
|
||||
tpManager.contentLE.preferredWidth = tpManager.preferredWidth;
|
||||
tpManager.contentLE.enabled = false;
|
||||
}
|
||||
|
||||
IEnumerator CalculateContentWidth()
|
||||
{
|
||||
yield return new WaitForSecondsRealtime(0.05f);
|
||||
float tempWidth = descriptionText.GetComponent<RectTransform>().sizeDelta.x;
|
||||
|
||||
if (tempWidth >= tpManager.preferredWidth + 1)
|
||||
tpManager.contentLE.enabled = true;
|
||||
|
||||
LayoutRebuilder.ForceRebuildLayoutImmediate(tpManager.contentLE.gameObject.GetComponent<RectTransform>());
|
||||
tpManager.contentLE.preferredWidth = tpManager.preferredWidth;
|
||||
}
|
||||
|
||||
IEnumerator ShowTooltip()
|
||||
{
|
||||
yield return new WaitForSecondsRealtime(delay);
|
||||
tooltipAnimator.Play("In");
|
||||
StopCoroutine("ShowTooltip");
|
||||
}
|
||||
|
||||
IEnumerator UpdateLayoutPosition()
|
||||
{
|
||||
yield return new WaitForSecondsRealtime(0.05f);
|
||||
LayoutRebuilder.ForceRebuildLayoutImmediate(tooltipAnimator.gameObject.GetComponent<RectTransform>());
|
||||
}
|
||||
}
|
||||
}
|
||||
18
Assets/Modern UI Pack/Scripts/Tooltip/TooltipContent.cs.meta
Normal file
18
Assets/Modern UI Pack/Scripts/Tooltip/TooltipContent.cs.meta
Normal file
@@ -0,0 +1,18 @@
|
||||
fileFormatVersion: 2
|
||||
guid: a81a9e081a4afa64dae6b9d42bc38c9f
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {fileID: 2800000, guid: 146ad7c4429fc764088d2a5139a2537d, type: 3}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
AssetOrigin:
|
||||
serializedVersion: 1
|
||||
productId: 201717
|
||||
packageName: Modern UI Pack
|
||||
packageVersion: 5.5.19
|
||||
assetPath: Assets/Modern UI Pack/Scripts/Tooltip/TooltipContent.cs
|
||||
uploadId: 628721
|
||||
143
Assets/Modern UI Pack/Scripts/Tooltip/TooltipManager.cs
Normal file
143
Assets/Modern UI Pack/Scripts/Tooltip/TooltipManager.cs
Normal file
@@ -0,0 +1,143 @@
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
#if ENABLE_INPUT_SYSTEM
|
||||
using UnityEngine.InputSystem;
|
||||
#endif
|
||||
|
||||
namespace Michsky.MUIP
|
||||
{
|
||||
public class TooltipManager : MonoBehaviour
|
||||
{
|
||||
// Resources
|
||||
public Canvas mainCanvas;
|
||||
public GameObject tooltipObject;
|
||||
public GameObject tooltipContent;
|
||||
public Camera targetCamera;
|
||||
|
||||
// Settings
|
||||
[Range(0.01f, 0.5f)] public float tooltipSmoothness = 0.1f;
|
||||
[Range(5, 10)] public float dampSpeed = 10;
|
||||
public float preferredWidth = 375;
|
||||
public bool allowUpdate = true;
|
||||
public bool checkDispose = true;
|
||||
public CameraSource cameraSource = CameraSource.Main;
|
||||
public TransitionMode transitionMode = TransitionMode.Damp;
|
||||
|
||||
// Content Bounds
|
||||
[Range(-50, 50)] public int vBorderTop = -15;
|
||||
[Range(-50, 50)] public int vBorderBottom = 10;
|
||||
[Range(-50, 50)] public int hBorderLeft = 20;
|
||||
[Range(-50, 50)] public int hBorderRight = -15;
|
||||
|
||||
// Border Bounds
|
||||
[SerializeField] private int xLeft = -400;
|
||||
[SerializeField] private int xRight = 400;
|
||||
[SerializeField] private int yTop = -325;
|
||||
[SerializeField] private int yBottom = 325;
|
||||
|
||||
[HideInInspector] public LayoutElement contentLE;
|
||||
[HideInInspector] public TooltipContent currentTooltip;
|
||||
|
||||
Vector2 uiPos;
|
||||
Vector3 cursorPos;
|
||||
Vector3 contentPos = new Vector3(0, 0, 0);
|
||||
Vector3 tooltipVelocity = Vector3.zero;
|
||||
|
||||
RectTransform contentRect;
|
||||
RectTransform tooltipRect;
|
||||
|
||||
public enum CameraSource { Main, Custom }
|
||||
public enum TransitionMode { Damp, Snap }
|
||||
|
||||
void Awake()
|
||||
{
|
||||
RectTransform sourceRect = gameObject.GetComponent<RectTransform>();
|
||||
|
||||
if (sourceRect == null)
|
||||
{
|
||||
Debug.LogError("<b>[Tooltip]</b> Rect Transform is missing from the object.", this);
|
||||
return;
|
||||
}
|
||||
|
||||
sourceRect.anchorMin = new Vector2(0, 0);
|
||||
sourceRect.anchorMax = new Vector2(1, 1);
|
||||
sourceRect.offsetMin = new Vector2(0, 0);
|
||||
sourceRect.offsetMax = new Vector2(0, 0);
|
||||
|
||||
tooltipContent.GetComponent<RectTransform>().pivot = new Vector2(0f, tooltipContent.GetComponent<RectTransform>().pivot.y);
|
||||
tooltipContent.GetComponent<RectTransform>().pivot = new Vector2(tooltipContent.GetComponent<RectTransform>().pivot.x, 0f);
|
||||
|
||||
if (mainCanvas == null) { mainCanvas = gameObject.GetComponentInParent<Canvas>(); }
|
||||
if (cameraSource == CameraSource.Main) { targetCamera = Camera.main; }
|
||||
|
||||
contentRect = tooltipContent.GetComponentInParent<RectTransform>();
|
||||
tooltipRect = tooltipObject.GetComponent<RectTransform>();
|
||||
|
||||
contentPos = new Vector3(vBorderTop, hBorderLeft, 0);
|
||||
gameObject.transform.SetAsLastSibling();
|
||||
}
|
||||
|
||||
void Update()
|
||||
{
|
||||
if (!allowUpdate) { return; }
|
||||
if (checkDispose && currentTooltip != null && !currentTooltip.gameObject.activeInHierarchy) { currentTooltip.ProcessExit(); }
|
||||
|
||||
CheckForPosition();
|
||||
}
|
||||
|
||||
void CheckForPosition()
|
||||
{
|
||||
#if ENABLE_LEGACY_INPUT_MANAGER
|
||||
cursorPos = Input.mousePosition;
|
||||
#elif ENABLE_INPUT_SYSTEM
|
||||
cursorPos = Mouse.current.position.ReadValue();
|
||||
#endif
|
||||
uiPos = tooltipRect.anchoredPosition;
|
||||
CheckForBounds();
|
||||
|
||||
if (mainCanvas.renderMode == RenderMode.ScreenSpaceCamera || mainCanvas.renderMode == RenderMode.WorldSpace)
|
||||
{
|
||||
RectTransformUtility.ScreenPointToLocalPointInRectangle(tooltipRect.parent.GetComponent<RectTransform>(), cursorPos, targetCamera, out Vector2 outPoint);
|
||||
tooltipRect.localPosition = outPoint;
|
||||
|
||||
if (transitionMode == TransitionMode.Damp) { tooltipContent.transform.localPosition = Vector3.SmoothDamp(tooltipContent.transform.localPosition, contentPos, ref tooltipVelocity, tooltipSmoothness, dampSpeed * 1000, Time.unscaledDeltaTime); }
|
||||
else { tooltipContent.transform.localPosition = contentPos; }
|
||||
}
|
||||
|
||||
else if (mainCanvas.renderMode == RenderMode.ScreenSpaceOverlay)
|
||||
{
|
||||
tooltipRect.position = cursorPos;
|
||||
|
||||
if (transitionMode == TransitionMode.Damp) { tooltipContent.transform.position = Vector3.SmoothDamp(tooltipContent.transform.position, cursorPos + contentPos, ref tooltipVelocity, tooltipSmoothness, dampSpeed * 1000, Time.unscaledDeltaTime); }
|
||||
else { tooltipContent.transform.position = cursorPos + contentPos; }
|
||||
}
|
||||
}
|
||||
|
||||
void CheckForBounds()
|
||||
{
|
||||
if (uiPos.x <= xLeft)
|
||||
{
|
||||
contentPos = new Vector3(hBorderLeft, contentPos.y, 0);
|
||||
contentRect.pivot = new Vector2(0f, contentRect.pivot.y);
|
||||
}
|
||||
|
||||
else if (uiPos.x >= xRight)
|
||||
{
|
||||
contentPos = new Vector3(hBorderRight, contentPos.y, 0);
|
||||
contentRect.pivot = new Vector2(1f, contentRect.pivot.y);
|
||||
}
|
||||
|
||||
if (uiPos.y <= yTop)
|
||||
{
|
||||
contentPos = new Vector3(contentPos.x, vBorderBottom, 0);
|
||||
contentRect.pivot = new Vector2(contentRect.pivot.x, 0f);
|
||||
}
|
||||
|
||||
else if (uiPos.y >= yBottom)
|
||||
{
|
||||
contentPos = new Vector3(contentPos.x, vBorderTop, 0);
|
||||
contentRect.pivot = new Vector2(contentRect.pivot.x, 1f);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
18
Assets/Modern UI Pack/Scripts/Tooltip/TooltipManager.cs.meta
Normal file
18
Assets/Modern UI Pack/Scripts/Tooltip/TooltipManager.cs.meta
Normal file
@@ -0,0 +1,18 @@
|
||||
fileFormatVersion: 2
|
||||
guid: f74f8e3213a5a144d801c9a0e3a84d80
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {fileID: 2800000, guid: 146ad7c4429fc764088d2a5139a2537d, type: 3}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
AssetOrigin:
|
||||
serializedVersion: 1
|
||||
productId: 201717
|
||||
packageName: Modern UI Pack
|
||||
packageVersion: 5.5.19
|
||||
assetPath: Assets/Modern UI Pack/Scripts/Tooltip/TooltipManager.cs
|
||||
uploadId: 628721
|
||||
141
Assets/Modern UI Pack/Scripts/Tooltip/TooltipManagerEditor.cs
Normal file
141
Assets/Modern UI Pack/Scripts/Tooltip/TooltipManagerEditor.cs
Normal file
@@ -0,0 +1,141 @@
|
||||
#if UNITY_EDITOR
|
||||
using UnityEngine;
|
||||
using UnityEditor;
|
||||
using UnityEditor.SceneManagement;
|
||||
|
||||
namespace Michsky.MUIP
|
||||
{
|
||||
[CustomEditor(typeof(TooltipManager))]
|
||||
public class TooltipManagerEditor : Editor
|
||||
{
|
||||
private GUISkin customSkin;
|
||||
private TooltipManager tooltipTarget;
|
||||
private UIManagerTooltip tempUIM;
|
||||
private int currentTab;
|
||||
|
||||
private void OnEnable()
|
||||
{
|
||||
tooltipTarget = (TooltipManager)target;
|
||||
|
||||
try { tempUIM = tooltipTarget.GetComponent<UIManagerTooltip>(); }
|
||||
catch { }
|
||||
|
||||
if (EditorGUIUtility.isProSkin == true) { customSkin = MUIPEditorHandler.GetDarkEditor(customSkin); }
|
||||
else { customSkin = MUIPEditorHandler.GetLightEditor(customSkin); }
|
||||
}
|
||||
|
||||
public override void OnInspectorGUI()
|
||||
{
|
||||
MUIPEditorHandler.DrawComponentHeader(customSkin, "Tooltip Top Header");
|
||||
|
||||
GUIContent[] toolbarTabs = new GUIContent[3];
|
||||
toolbarTabs[0] = new GUIContent("Content");
|
||||
toolbarTabs[1] = new GUIContent("Resources");
|
||||
toolbarTabs[2] = new GUIContent("Settings");
|
||||
|
||||
currentTab = MUIPEditorHandler.DrawTabs(currentTab, toolbarTabs, customSkin);
|
||||
|
||||
if (GUILayout.Button(new GUIContent("Content", "Content"), customSkin.FindStyle("Tab Content")))
|
||||
currentTab = 0;
|
||||
if (GUILayout.Button(new GUIContent("Resources", "Resources"), customSkin.FindStyle("Tab Resources")))
|
||||
currentTab = 1;
|
||||
if (GUILayout.Button(new GUIContent("Settings", "Settings"), customSkin.FindStyle("Tab Settings")))
|
||||
currentTab = 2;
|
||||
|
||||
GUILayout.EndHorizontal();
|
||||
|
||||
var vBorderTop = serializedObject.FindProperty("vBorderTop");
|
||||
var vBorderBottom = serializedObject.FindProperty("vBorderBottom");
|
||||
var hBorderLeft = serializedObject.FindProperty("hBorderLeft");
|
||||
var hBorderRight = serializedObject.FindProperty("hBorderRight");
|
||||
var mainCanvas = serializedObject.FindProperty("mainCanvas");
|
||||
var tooltipObject = serializedObject.FindProperty("tooltipObject");
|
||||
var tooltipContent = serializedObject.FindProperty("tooltipContent");
|
||||
var tooltipSmoothness = serializedObject.FindProperty("tooltipSmoothness");
|
||||
var dampSpeed = serializedObject.FindProperty("dampSpeed");
|
||||
var preferredWidth = serializedObject.FindProperty("preferredWidth");
|
||||
var targetCamera = serializedObject.FindProperty("targetCamera");
|
||||
var cameraSource = serializedObject.FindProperty("cameraSource");
|
||||
var transitionMode = serializedObject.FindProperty("transitionMode");
|
||||
var checkDispose = serializedObject.FindProperty("checkDispose");
|
||||
|
||||
switch (currentTab)
|
||||
{
|
||||
case 0:
|
||||
MUIPEditorHandler.DrawHeader(customSkin, "Content Header", 6);
|
||||
MUIPEditorHandler.DrawProperty(vBorderTop, customSkin, "Top Bound");
|
||||
MUIPEditorHandler.DrawProperty(vBorderBottom, customSkin, "Bottom Bound");
|
||||
MUIPEditorHandler.DrawProperty(hBorderLeft, customSkin, "Left Bound");
|
||||
MUIPEditorHandler.DrawProperty(hBorderRight, customSkin, "Right Bound");
|
||||
|
||||
if (tooltipTarget.tooltipObject != null && tooltipTarget.tooltipObject.GetComponent<CanvasGroup>().alpha == 0)
|
||||
{
|
||||
if (GUILayout.Button("Make It Visible", customSkin.button))
|
||||
{
|
||||
tooltipTarget.tooltipObject.GetComponent<CanvasGroup>().alpha = 1;
|
||||
EditorSceneManager.MarkSceneDirty(EditorSceneManager.GetActiveScene());
|
||||
}
|
||||
}
|
||||
|
||||
else
|
||||
{
|
||||
if (GUILayout.Button("Make It Invisible", customSkin.button))
|
||||
{
|
||||
tooltipTarget.tooltipObject.GetComponent<CanvasGroup>().alpha = 0;
|
||||
EditorSceneManager.MarkSceneDirty(EditorSceneManager.GetActiveScene());
|
||||
}
|
||||
}
|
||||
|
||||
EditorGUILayout.HelpBox("In order to use the tooltip system, you should add the 'Tooltip Content' component to your objects.", MessageType.Info);
|
||||
break;
|
||||
|
||||
case 1:
|
||||
MUIPEditorHandler.DrawHeader(customSkin, "Core Header", 6);
|
||||
MUIPEditorHandler.DrawProperty(tooltipObject, customSkin, "Tooltip Object");
|
||||
MUIPEditorHandler.DrawProperty(tooltipContent, customSkin, "Tooltip Content");
|
||||
MUIPEditorHandler.DrawProperty(mainCanvas, customSkin, "Main Canvas");
|
||||
break;
|
||||
|
||||
case 2:
|
||||
MUIPEditorHandler.DrawHeader(customSkin, "Options Header", 6);
|
||||
checkDispose.boolValue = MUIPEditorHandler.DrawToggle(checkDispose.boolValue, customSkin, "Check Dispose/Null");
|
||||
MUIPEditorHandler.DrawProperty(preferredWidth, customSkin, "Preferred Width");
|
||||
MUIPEditorHandler.DrawProperty(tooltipSmoothness, customSkin, "Smoothness");
|
||||
MUIPEditorHandler.DrawProperty(dampSpeed, customSkin, "Damp Speed");
|
||||
MUIPEditorHandler.DrawProperty(transitionMode, customSkin, "Transition Mode");
|
||||
MUIPEditorHandler.DrawProperty(cameraSource, customSkin, "Camera Source");
|
||||
|
||||
if (tooltipTarget.cameraSource == TooltipManager.CameraSource.Custom)
|
||||
MUIPEditorHandler.DrawProperty(targetCamera, customSkin, "Target Camera");
|
||||
|
||||
MUIPEditorHandler.DrawHeader(customSkin, "UIM Header", 10);
|
||||
|
||||
if (tempUIM != null)
|
||||
{
|
||||
MUIPEditorHandler.DrawUIManagerConnectedHeader();
|
||||
|
||||
if (GUILayout.Button("Open UI Manager", customSkin.button))
|
||||
EditorApplication.ExecuteMenuItem("Tools/Modern UI Pack/Show UI Manager");
|
||||
|
||||
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) { MUIPEditorHandler.DrawUIManagerDisconnectedHeader(); }
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
if (Application.isPlaying == false) { this.Repaint(); }
|
||||
serializedObject.ApplyModifiedProperties();
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,18 @@
|
||||
fileFormatVersion: 2
|
||||
guid: a94e92bd62020d54ebad68de9ce276bc
|
||||
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.19
|
||||
assetPath: Assets/Modern UI Pack/Scripts/Tooltip/TooltipManagerEditor.cs
|
||||
uploadId: 628721
|
||||
Reference in New Issue
Block a user