1
0
mirror of https://github.com/Siccity/xNode.git synced 2025-12-20 01:06:01 +08:00

Basic grid background

This commit is contained in:
Thor Brigsted 2017-09-15 00:46:21 +02:00
parent 0bfef8cf53
commit 23c1d87988
24 changed files with 626 additions and 0 deletions

View File

@ -0,0 +1,9 @@
fileFormatVersion: 2
guid: cf36ba91f22a17e44aa9069b5dd55922
folderAsset: yes
timeCreated: 1505419550
licenseType: Free
DefaultImporter:
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,9 @@
fileFormatVersion: 2
guid: 4703e560073f94443811e277ed07e219
folderAsset: yes
timeCreated: 1505419559
licenseType: Free
DefaultImporter:
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,205 @@
using System;
using UnityEditor;
using UnityEngine;
using System.Collections.Generic;
public class BuiltInResourcesWindow : EditorWindow {
[MenuItem("Window/Built-in styles and icons")]
public static void ShowWindow() {
BuiltInResourcesWindow w = (BuiltInResourcesWindow)EditorWindow.GetWindow<BuiltInResourcesWindow>();
w.Show();
}
private struct Drawing {
public Rect Rect;
public Action Draw;
}
private List<Drawing> Drawings;
private List<UnityEngine.Object> _objects;
private float _scrollPos;
private float _maxY;
private Rect _oldPosition;
private bool _showingStyles = true;
private bool _showingIcons = false;
private string _search = "";
void OnGUI() {
if (position.width != _oldPosition.width && Event.current.type == EventType.Layout) {
Drawings = null;
_oldPosition = position;
}
GUILayout.BeginHorizontal();
if (GUILayout.Toggle(_showingStyles, "Styles", EditorStyles.toolbarButton) != _showingStyles) {
_showingStyles = !_showingStyles;
_showingIcons = !_showingStyles;
Drawings = null;
}
if (GUILayout.Toggle(_showingIcons, "Icons", EditorStyles.toolbarButton) != _showingIcons) {
_showingIcons = !_showingIcons;
_showingStyles = !_showingIcons;
Drawings = null;
}
GUILayout.EndHorizontal();
string newSearch = GUILayout.TextField(_search);
if (newSearch != _search) {
_search = newSearch;
Drawings = null;
}
float top = 36;
if (Drawings == null) {
string lowerSearch = _search.ToLower();
Drawings = new List<Drawing>();
GUIContent inactiveText = new GUIContent("inactive");
GUIContent activeText = new GUIContent("active");
float x = 5.0f;
float y = 5.0f;
if (_showingStyles) {
foreach (GUIStyle ss in GUI.skin.customStyles) {
if (lowerSearch != "" && !ss.name.ToLower().Contains(lowerSearch))
continue;
GUIStyle thisStyle = ss;
Drawing draw = new Drawing();
float width = Mathf.Max(
100.0f,
GUI.skin.button.CalcSize(new GUIContent(ss.name)).x,
ss.CalcSize(inactiveText).x + ss.CalcSize(activeText).x
) + 16.0f;
float height = 60.0f;
if (x + width > position.width - 32 && x > 5.0f) {
x = 5.0f;
y += height + 10.0f;
}
draw.Rect = new Rect(x, y, width, height);
width -= 8.0f;
draw.Draw = () => {
if (GUILayout.Button(thisStyle.name, GUILayout.Width(width)))
CopyText("(GUIStyle)\"" + thisStyle.name + "\"");
GUILayout.BeginHorizontal();
GUILayout.Toggle(false, inactiveText, thisStyle, GUILayout.Width(width / 2));
GUILayout.Toggle(false, activeText, thisStyle, GUILayout.Width(width / 2));
GUILayout.EndHorizontal();
};
x += width + 18.0f;
Drawings.Add(draw);
}
}
else if (_showingIcons) {
if (_objects == null) {
_objects = new List<UnityEngine.Object>(Resources.FindObjectsOfTypeAll(typeof(Texture)));
_objects.Sort((pA, pB) => System.String.Compare(pA.name, pB.name, System.StringComparison.OrdinalIgnoreCase));
}
float rowHeight = 0.0f;
foreach (UnityEngine.Object oo in _objects) {
Texture texture = (Texture)oo;
if (texture.name == "")
continue;
if (lowerSearch != "" && !texture.name.ToLower().Contains(lowerSearch))
continue;
Drawing draw = new Drawing();
float width = Mathf.Max(
GUI.skin.button.CalcSize(new GUIContent(texture.name)).x,
texture.width
) + 8.0f;
float height = texture.height + GUI.skin.button.CalcSize(new GUIContent(texture.name)).y + 8.0f;
if (x + width > position.width - 32.0f) {
x = 5.0f;
y += rowHeight + 8.0f;
rowHeight = 0.0f;
}
draw.Rect = new Rect(x, y, width, height);
rowHeight = Mathf.Max(rowHeight, height);
width -= 8.0f;
draw.Draw = () => {
if (GUILayout.Button(texture.name, GUILayout.Width(width)))
CopyText("EditorGUIUtility.FindTexture( \"" + texture.name + "\" )");
Rect textureRect = GUILayoutUtility.GetRect(texture.width, texture.width, texture.height, texture.height, GUILayout.ExpandHeight(false), GUILayout.ExpandWidth(false));
EditorGUI.DrawTextureTransparent(textureRect, texture);
};
x += width + 8.0f;
Drawings.Add(draw);
}
}
_maxY = y;
}
Rect r = position;
r.y = top;
r.height -= r.y;
r.x = r.width - 16;
r.width = 16;
float areaHeight = position.height - top;
_scrollPos = GUI.VerticalScrollbar(r, _scrollPos, areaHeight, 0.0f, _maxY);
Rect area = new Rect(0, top, position.width - 16.0f, areaHeight);
GUILayout.BeginArea(area);
int count = 0;
foreach (Drawing draw in Drawings) {
Rect newRect = draw.Rect;
newRect.y -= _scrollPos;
if (newRect.y + newRect.height > 0 && newRect.y < areaHeight) {
GUILayout.BeginArea(newRect, GUI.skin.textField);
draw.Draw();
GUILayout.EndArea();
count++;
}
}
GUILayout.EndArea();
}
void CopyText(string pText) {
TextEditor editor = new TextEditor();
//editor.content = new GUIContent(pText); // Unity 4.x code
editor.text = pText; // Unity 5.x code
editor.SelectAll();
editor.Copy();
}
}

View File

@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: 597c4ff91568b244a849f38dc2ee3f4e
timeCreated: 1505418688
licenseType: Free
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

9
Examples.meta Normal file
View File

@ -0,0 +1,9 @@
fileFormatVersion: 2
guid: 3cfe6eabeed0aa44e8d9d54b308a461f
folderAsset: yes
timeCreated: 1505418316
licenseType: Free
DefaultImporter:
userData:
assetBundleName:
assetBundleVariant:

8
LICENSE.md.meta Normal file
View File

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 4cf6e5838bfdc1e46866ef31ac6cd822
timeCreated: 1505418267
licenseType: Free
DefaultImporter:
userData:
assetBundleName:
assetBundleVariant:

8
README.md.meta Normal file
View File

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: e239dd85f6aaf594989e993fcb7cba6c
timeCreated: 1505418267
licenseType: Free
DefaultImporter:
userData:
assetBundleName:
assetBundleVariant:

9
Scripts.meta Normal file
View File

@ -0,0 +1,9 @@
fileFormatVersion: 2
guid: 657b15cb3ec32a24ca80faebf094d0f4
folderAsset: yes
timeCreated: 1505418321
licenseType: Free
DefaultImporter:
userData:
assetBundleName:
assetBundleVariant:

9
Scripts/Editor.meta Normal file
View File

@ -0,0 +1,9 @@
fileFormatVersion: 2
guid: 94d4fd78d9120634ebe0e8717610c412
folderAsset: yes
timeCreated: 1505418345
licenseType: Free
DefaultImporter:
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,27 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
public static class NodeEditorAction {
public static void Controls(NodeEditorWindow window) {
Event e = Event.current;
switch (e.type) {
case EventType.ScrollWheel:
if (e.delta.y > 0) window.zoom += 0.1f * window.zoom;
else window.zoom -= 0.1f * window.zoom;
break;
case EventType.MouseDrag:
if (e.button == 1) window.panOffset += e.delta*window.zoom;
break;
case EventType.KeyDown:
if (e.keyCode == KeyCode.F) {
window.zoom = 2;
window.panOffset = Vector2.zero;
}
break;
}
}
}

View File

@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: aa7d4286bf0ad2e4086252f2893d2cf5
timeCreated: 1505426655
licenseType: Free
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,34 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
namespace UNEC {
/// <summary> Contains GUI methods </summary>
public static class NodeEditorGUI {
public static void DrawGrid(Rect rect, float zoom, Vector2 panOffset) {
rect.position = Vector2.zero;
Vector2 center = rect.size / 2f;
Texture2D gridTex = NodeEditorResources.gridTexture;
Texture2D crossTex = NodeEditorResources.crossTexture;
// Offset from origin in tile units
float xOffset = -(center.x * zoom + panOffset.x) / gridTex.width;
float yOffset = ((center.y - rect.size.y) * zoom + panOffset.y) / gridTex.height;
Vector2 tileOffset = new Vector2(xOffset, yOffset);
// Amount of tiles
float tileAmountX = Mathf.Round(rect.size.x * zoom) / gridTex.width;
float tileAmountY = Mathf.Round(rect.size.y * zoom) / gridTex.height;
Vector2 tileAmount = new Vector2(tileAmountX, tileAmountY);
// Draw tiled background
GUI.DrawTextureWithTexCoords(rect, gridTex, new Rect(tileOffset, tileAmount));
GUI.DrawTextureWithTexCoords(rect, crossTex, new Rect(tileOffset + new Vector2(0.5f,0.5f), tileAmount));
}
}
}

View File

@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: 756276bfe9a0c2f4da3930ba1964f58d
timeCreated: 1505420917
licenseType: Free
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,22 @@
using System.Reflection;
using System.Linq;
using System;
namespace UNEC {
/// <summary> Contains reflection-related info </summary>
public static class NodeEditorReflection {
public static Type[] nodeTypes { get { return _nodeTypes != null ? _nodeTypes : _nodeTypes = GetNodeTypes(); } }
public static Type[] _nodeTypes;
public static Type[] GetNodeTypes() {
//Get all classes deriving from Node via reflection
Type derivedType = typeof(Node);
Assembly assembly = Assembly.GetAssembly(derivedType);
return assembly.GetTypes().Where(t =>
t != derivedType &&
derivedType.IsAssignableFrom(t)
).ToArray();
}
}
}

View File

@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: c78a0fa4a13abcd408ebe73006b7b1bb
timeCreated: 1505419458
licenseType: Free
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,55 @@
using UnityEngine;
using UnityEditor;
namespace UNEC {
public static class NodeEditorResources {
public static Texture2D gridTexture { get { return _gridTexture != null ? _gridTexture : _gridTexture = GenerateGridTexture(); } }
private static Texture2D _gridTexture;
public static Texture2D crossTexture { get { return _crossTexture != null ? _crossTexture : _crossTexture = GenerateCrossTexture(); } }
private static Texture2D _crossTexture;
private static Color backgroundColor = new Color(0.15f, 0.15f, 0.15f);
private static Color veinColor = new Color(0.2f, 0.2f, 0.2f);
private static Color arteryColor = new Color(0.28f, 0.28f, 0.28f);
private static Color crossColor = new Color(0.4f, 0.4f, 0.4f);
public static Texture2D GenerateGridTexture() {
Texture2D tex = new Texture2D(64,64);
Color[] cols = new Color[64 * 64];
for (int y = 0; y < 64; y++) {
for (int x = 0; x < 64; x++) {
Color col = backgroundColor;
if (y % 16 == 0 || x % 16 == 0) col = veinColor;
if (y == 63 || x == 63) col = arteryColor;
cols[(y * 64) + x] = col;
}
}
tex.SetPixels(cols);
tex.wrapMode = TextureWrapMode.Repeat;
tex.filterMode = FilterMode.Bilinear;
tex.name = "Grid";
tex.Apply();
return tex;
}
public static Texture2D GenerateCrossTexture() {
Texture2D tex = new Texture2D(64, 64);
Color[] cols = new Color[64 * 64];
for (int y = 0; y < 64; y++) {
for (int x = 0; x < 64; x++) {
Color col = crossColor;
if (y != 31 && x != 31) col.a = 0;
cols[(y * 64) + x] = col;
}
}
tex.SetPixels(cols);
tex.wrapMode = TextureWrapMode.Clamp;
tex.filterMode = FilterMode.Bilinear;
tex.name = "Grid";
tex.Apply();
return tex;
}
}
}

View File

@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: 69f55d341299026489b29443c3dd13d1
timeCreated: 1505418919
licenseType: Free
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,28 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using UNEC;
public class NodeEditorWindow : EditorWindow {
public Vector2 panOffset { get { return _panOffset; } set { _panOffset = value; Repaint(); } }
private Vector2 _panOffset;
public float zoom { get { return _zoom; } set { _zoom = Mathf.Clamp(value, 1f, 5f); Repaint(); } }
private float _zoom = 1;
[MenuItem("Window/UNEC")]
static void Init() {
NodeEditorWindow w = CreateInstance<NodeEditorWindow>();
w.Show();
}
private void OnGUI() {
NodeEditorAction.Controls(this);
BeginWindows();
NodeEditorGUI.DrawGrid(position, zoom, panOffset);
EndWindows();
}
}

View File

@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: 5ce2bf59ec7a25c4ba691cad7819bf38
timeCreated: 1505418450
licenseType: Free
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

19
Scripts/Node.cs Normal file
View File

@ -0,0 +1,19 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace UNEC {
/// <summary> Base class for all nodes </summary>
public abstract class Node : MonoBehaviour {
// Use this for initialization
void Start() {
}
// Update is called once per frame
void Update() {
}
}
}

12
Scripts/Node.cs.meta Normal file
View File

@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: f26231e5ab9368746948d0ea49e8178a
timeCreated: 1505419984
licenseType: Free
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

9
Textures.meta Normal file
View File

@ -0,0 +1,9 @@
fileFormatVersion: 2
guid: 003638a12027abc46ba0fa1719d11246
folderAsset: yes
timeCreated: 1505424851
licenseType: Free
DefaultImporter:
userData:
assetBundleName:
assetBundleVariant:

BIN
Textures/Grid.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 810 B

82
Textures/Grid.png.meta Normal file
View File

@ -0,0 +1,82 @@
fileFormatVersion: 2
guid: 00e76aa739b5f474fabc717677698eef
timeCreated: 1505424840
licenseType: Free
TextureImporter:
fileIDToRecycleName: {}
serializedVersion: 4
mipmaps:
mipMapMode: 0
enableMipMap: 0
sRGBTexture: 0
linearTexture: 0
fadeOut: 0
borderMipMap: 0
mipMapsPreserveCoverage: 0
alphaTestReferenceValue: 0.5
mipMapFadeDistanceStart: 1
mipMapFadeDistanceEnd: 3
bumpmap:
convertToNormalMap: 0
externalNormalMap: 0
heightScale: 0.25
normalMapFilter: 0
isReadable: 0
grayScaleToAlpha: 0
generateCubemap: 6
cubemapConvolution: 0
seamlessCubemap: 0
textureFormat: 1
maxTextureSize: 2048
textureSettings:
serializedVersion: 2
filterMode: -1
aniso: 1
mipBias: -1
wrapU: 1
wrapV: 1
wrapW: 1
nPOTScale: 0
lightmap: 0
compressionQuality: 50
spriteMode: 1
spriteExtrude: 1
spriteMeshType: 1
alignment: 0
spritePivot: {x: 0.5, y: 0.5}
spriteBorder: {x: 0, y: 0, z: 0, w: 0}
spritePixelsToUnits: 100
alphaUsage: 1
alphaIsTransparency: 1
spriteTessellationDetail: -1
textureType: 2
textureShape: 1
maxTextureSizeSet: 0
compressionQualitySet: 0
textureFormatSet: 0
platformSettings:
- buildTarget: DefaultTexturePlatform
maxTextureSize: 2048
textureFormat: -1
textureCompression: 1
compressionQuality: 50
crunchedCompression: 0
allowsAlphaSplitting: 0
overridden: 0
- buildTarget: Standalone
maxTextureSize: 2048
textureFormat: -1
textureCompression: 1
compressionQuality: 50
crunchedCompression: 0
allowsAlphaSplitting: 0
overridden: 0
spriteSheet:
serializedVersion: 2
sprites: []
outline: []
physicsShape: []
spritePackingTag:
userData:
assetBundleName:
assetBundleVariant: