説明なし
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

Drawer.cs 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. using System.Collections.Generic;
  2. using UnityEngine;
  3. using UnityEditor;
  4. using UnityEditor.U2D.Path.GUIFramework;
  5. namespace UnityEditor.U2D.Path
  6. {
  7. public class Drawer : IDrawer
  8. {
  9. internal class Styles
  10. {
  11. public readonly GUIStyle pointNormalStyle;
  12. public readonly GUIStyle pointHoveredStyle;
  13. public readonly GUIStyle pointSelectedStyle;
  14. public readonly GUIStyle pointPreviewStyle;
  15. public readonly GUIStyle tangentNormalStyle;
  16. public readonly GUIStyle tangentHoveredStyle;
  17. public Styles()
  18. {
  19. var pointNormal = AssetDatabase.LoadAssetAtPath<Texture2D>("Packages/com.unity.2d.path/Editor/Handles/Path/pointNormal.png");
  20. var pointHovered = AssetDatabase.LoadAssetAtPath<Texture2D>("Packages/com.unity.2d.path/Editor/Handles/Path/pointHovered.png");
  21. var pointSelected = AssetDatabase.LoadAssetAtPath<Texture2D>("Packages/com.unity.2d.path/Editor/Handles/Path/pointSelected.png");
  22. var pointPreview = AssetDatabase.LoadAssetAtPath<Texture2D>("Packages/com.unity.2d.path/Editor/Handles/Path/pointPreview.png");
  23. var tangentNormal = AssetDatabase.LoadAssetAtPath<Texture2D>("Packages/com.unity.2d.path/Editor/Handles/Path/tangentNormal.png");
  24. pointNormalStyle = CreateStyle(AssetDatabase.LoadAssetAtPath<Texture2D>("Packages/com.unity.2d.path/Editor/Handles/Path/pointNormal.png"), Vector2.one * 12f);
  25. pointHoveredStyle = CreateStyle(AssetDatabase.LoadAssetAtPath<Texture2D>("Packages/com.unity.2d.path/Editor/Handles/Path/pointHovered.png"), Vector2.one * 12f);
  26. pointSelectedStyle = CreateStyle(AssetDatabase.LoadAssetAtPath<Texture2D>("Packages/com.unity.2d.path/Editor/Handles/Path/pointSelected.png"), Vector2.one * 12f);
  27. pointPreviewStyle = CreateStyle(AssetDatabase.LoadAssetAtPath<Texture2D>("Packages/com.unity.2d.path/Editor/Handles/Path/pointPreview.png"), Vector2.one * 12f);
  28. tangentNormalStyle = CreateStyle(AssetDatabase.LoadAssetAtPath<Texture2D>("Packages/com.unity.2d.path/Editor/Handles/Path/tangentNormal.png"), Vector2.one * 8f);
  29. tangentHoveredStyle = CreateStyle(AssetDatabase.LoadAssetAtPath<Texture2D>("Packages/com.unity.2d.path/Editor/Handles/Path/pointHovered.png"), Vector2.one * 10f);
  30. }
  31. private GUIStyle CreateStyle(Texture2D texture, Vector2 size)
  32. {
  33. var guiStyle = new GUIStyle();
  34. guiStyle.normal.background = texture;
  35. guiStyle.fixedWidth = size.x;
  36. guiStyle.fixedHeight = size.y;
  37. return guiStyle;
  38. }
  39. }
  40. private IGUIState m_GUIState = new GUIState();
  41. private Styles m_Styles;
  42. private Styles styles
  43. {
  44. get
  45. {
  46. if (m_Styles == null)
  47. m_Styles = new Styles();
  48. return m_Styles;
  49. }
  50. }
  51. public void DrawCreatePointPreview(Vector3 position)
  52. {
  53. DrawGUIStyleCap(0, position, Quaternion.identity, m_GUIState.GetHandleSize(position), styles.pointPreviewStyle);
  54. }
  55. public void DrawPoint(Vector3 position)
  56. {
  57. DrawGUIStyleCap(0, position, Quaternion.identity, m_GUIState.GetHandleSize(position), styles.pointNormalStyle);
  58. }
  59. public void DrawPointHovered(Vector3 position)
  60. {
  61. DrawGUIStyleCap(0, position, Quaternion.identity, m_GUIState.GetHandleSize(position), styles.pointHoveredStyle);
  62. }
  63. public void DrawPointSelected(Vector3 position)
  64. {
  65. DrawGUIStyleCap(0, position, Quaternion.identity, m_GUIState.GetHandleSize(position), styles.pointSelectedStyle);
  66. }
  67. public void DrawLine(Vector3 p1, Vector3 p2, float width, Color color)
  68. {
  69. Handles.color = color;
  70. Handles.DrawAAPolyLine(width, new Vector3[] { p1, p2 });
  71. }
  72. public void DrawBezier(Vector3 p1, Vector3 p2, Vector3 p3, Vector3 p4, float width, Color color)
  73. {
  74. Handles.color = color;
  75. Handles.DrawBezier(p1, p4, p2, p3, color, null, width);
  76. }
  77. public void DrawTangent(Vector3 position, Vector3 tangent)
  78. {
  79. DrawLine(position, tangent, 3f, Color.yellow);
  80. DrawGUIStyleCap(0, tangent, Quaternion.identity, m_GUIState.GetHandleSize(tangent), styles.tangentNormalStyle);
  81. }
  82. private void DrawGUIStyleCap(int controlID, Vector3 position, Quaternion rotation, float size, GUIStyle guiStyle)
  83. {
  84. if (Camera.current && Vector3.Dot(position - Camera.current.transform.position, Camera.current.transform.forward) < 0f)
  85. return;
  86. Handles.BeginGUI();
  87. guiStyle.Draw(GetGUIStyleRect(guiStyle, position), GUIContent.none, controlID);
  88. Handles.EndGUI();
  89. }
  90. private Rect GetGUIStyleRect(GUIStyle style, Vector3 position)
  91. {
  92. Vector2 vector = HandleUtility.WorldToGUIPoint(position);
  93. float fixedWidth = style.fixedWidth;
  94. float fixedHeight = style.fixedHeight;
  95. return new Rect(vector.x - fixedWidth / 2f, vector.y - fixedHeight / 2f, fixedWidth, fixedHeight);
  96. }
  97. }
  98. }