InfoText.cs 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. /*
  2. Copyright (c) 2025 Omar Duarte
  3. Unauthorized copying of this file, via any medium is strictly prohibited.
  4. Writen by Omar Duarte, 2025.
  5. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  6. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  7. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  8. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  9. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  10. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  11. THE SOFTWARE.
  12. */
  13. using UnityEngine;
  14. namespace PluginMaster
  15. {
  16. public static class InfoText
  17. {
  18. private static GUIStyle labelStyle = new GUIStyle(UnityEditor.EditorStyles.label)
  19. {
  20. normal = { textColor = new Color(1, 1, 1, 0.7f) },
  21. alignment = TextAnchor.UpperLeft
  22. };
  23. public static void Draw(UnityEditor.SceneView sceneView, string[] texts)
  24. {
  25. UnityEditor.Handles.BeginGUI();
  26. Vector2 mousePos = Event.current.mousePosition;
  27. const float lineHeight = 18f;
  28. const float topMargin = 4f;
  29. const float leftMargin = 4f;
  30. var numberOfLines = texts.Length;
  31. var totalHeight = numberOfLines * lineHeight + topMargin;
  32. var maxWidth = 0f;
  33. for (int i = 0; i < texts.Length; i++)
  34. {
  35. var width = labelStyle.CalcSize(new GUIContent(texts[i])).x;
  36. maxWidth = Mathf.Max(maxWidth, width);
  37. }
  38. var rectWidth = maxWidth + leftMargin * 2;
  39. var offsetX = 10f;
  40. if (mousePos.x + offsetX + rectWidth > sceneView.position.width - 50) offsetX = -(rectWidth + 10f);
  41. var offsetY = 10f;
  42. if (mousePos.y + offsetY + totalHeight > sceneView.position.height - 30) offsetY = -(totalHeight + 10f);
  43. var rect = new Rect(mousePos.x + offsetX, mousePos.y + offsetY, rectWidth, totalHeight);
  44. UnityEditor.EditorGUI.DrawRect(rect, new Color(0, 0, 0, 0.3f));
  45. var y = rect.y + topMargin;
  46. for (int i = 0; i < texts.Length; i++)
  47. {
  48. var lineRect = new Rect(rect.x + leftMargin, y, rectWidth, lineHeight);
  49. GUI.Label(lineRect, texts[i], labelStyle);
  50. y += lineHeight;
  51. }
  52. UnityEditor.Handles.EndGUI();
  53. }
  54. }
  55. }