NodeEditorResources.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. using UnityEditor;
  2. using UnityEngine;
  3. namespace XNodeEditor {
  4. public static class NodeEditorResources {
  5. // Textures
  6. public static Texture2D dot { get { return _dot != null ? _dot : _dot = Resources.Load<Texture2D>("xnode_dot"); } }
  7. private static Texture2D _dot;
  8. public static Texture2D dotOuter { get { return _dotOuter != null ? _dotOuter : _dotOuter = Resources.Load<Texture2D>("xnode_dot_outer"); } }
  9. private static Texture2D _dotOuter;
  10. public static Texture2D nodeBody { get { return _nodeBody != null ? _nodeBody : _nodeBody = Resources.Load<Texture2D>("xnode_node"); } }
  11. private static Texture2D _nodeBody;
  12. public static Texture2D nodeHighlight { get { return _nodeHighlight != null ? _nodeHighlight : _nodeHighlight = Resources.Load<Texture2D>("xnode_node_highlight"); } }
  13. private static Texture2D _nodeHighlight;
  14. // Styles
  15. public static Styles styles { get { return _styles != null ? _styles : _styles = new Styles(); } }
  16. public static Styles _styles = null;
  17. public static GUIStyle OutputPort { get { return new GUIStyle(EditorStyles.label) { alignment = TextAnchor.UpperRight }; } }
  18. public class Styles {
  19. public GUIStyle inputPort, nodeHeader, nodeBody, tooltip, nodeHighlight;
  20. public Styles() {
  21. GUIStyle baseStyle = new GUIStyle("Label");
  22. baseStyle.fixedHeight = 18;
  23. inputPort = new GUIStyle(baseStyle);
  24. inputPort.alignment = TextAnchor.UpperLeft;
  25. inputPort.padding.left = 10;
  26. nodeHeader = new GUIStyle();
  27. nodeHeader.alignment = TextAnchor.MiddleCenter;
  28. nodeHeader.fontStyle = FontStyle.Bold;
  29. nodeHeader.normal.textColor = Color.white;
  30. nodeBody = new GUIStyle();
  31. nodeBody.normal.background = NodeEditorResources.nodeBody;
  32. nodeBody.border = new RectOffset(32, 32, 32, 32);
  33. nodeBody.padding = new RectOffset(16, 16, 4, 16);
  34. nodeHighlight = new GUIStyle();
  35. nodeHighlight.normal.background = NodeEditorResources.nodeHighlight;
  36. nodeHighlight.border = new RectOffset(32, 32, 32, 32);
  37. tooltip = new GUIStyle("helpBox");
  38. tooltip.alignment = TextAnchor.MiddleCenter;
  39. }
  40. }
  41. public static Texture2D GenerateGridTexture(Color line, Color bg) {
  42. Texture2D tex = new Texture2D(64, 64);
  43. Color[] cols = new Color[64 * 64];
  44. for (int y = 0; y < 64; y++) {
  45. for (int x = 0; x < 64; x++) {
  46. Color col = bg;
  47. if (y % 16 == 0 || x % 16 == 0) col = Color.Lerp(line, bg, 0.65f);
  48. if (y == 63 || x == 63) col = Color.Lerp(line, bg, 0.35f);
  49. cols[(y * 64) + x] = col;
  50. }
  51. }
  52. tex.SetPixels(cols);
  53. tex.wrapMode = TextureWrapMode.Repeat;
  54. tex.filterMode = FilterMode.Bilinear;
  55. tex.name = "Grid";
  56. tex.Apply();
  57. return tex;
  58. }
  59. public static Texture2D GenerateCrossTexture(Color line) {
  60. Texture2D tex = new Texture2D(64, 64);
  61. Color[] cols = new Color[64 * 64];
  62. for (int y = 0; y < 64; y++) {
  63. for (int x = 0; x < 64; x++) {
  64. Color col = line;
  65. if (y != 31 && x != 31) col.a = 0;
  66. cols[(y * 64) + x] = col;
  67. }
  68. }
  69. tex.SetPixels(cols);
  70. tex.wrapMode = TextureWrapMode.Clamp;
  71. tex.filterMode = FilterMode.Bilinear;
  72. tex.name = "Grid";
  73. tex.Apply();
  74. return tex;
  75. }
  76. }
  77. }