using System.Collections;
using System.Collections.Generic;using UnityEngine;
using UnityEditor;
using System.IO;
using System.Reflection;
using System;
public class NodeEditor : EditorWindow
{
static NodeEditor _window;
public static float zoomDelta = 0.01f;
public static float minZoom = 1f;
public static float maxZoom = 8f;
public static float panSpeed = 1.2f;
// To keep track of zooming.
private Vector2 _zoomAdjustment;
private Vector2 _zoom = Vector2.one;
public Vector2 panOffset = Vector2.zero;
public void Pan(Vector2 delta)
{
panOffset += delta * ZoomScale * panSpeed;
}
public void Zoom(float zoomDirection)
{
float scale = (zoomDirection < 0f) ? (1f - zoomDelta) : (1f + zoomDelta);
_zoom *= scale;
float cap = Mathf.Clamp(_zoom.x, minZoom, maxZoom);
_zoom.Set(cap, cap);
}
public float ZoomScale
{
get { return _zoom.x; }
set
{
float z = Mathf.Clamp(value, minZoom, maxZoom);
_zoom.Set(z, z);
}
}
/// <summary>
/// The size of the window.
/// </summary>
public Rect Size
{
get { return new Rect(Vector2.zero, position.size); }
}
[MenuItem("Scene/NodeEditor #")]
static void Init()
{
if (_window == null)
{
_window = EditorWindow.GetWindow(typeof(NodeEditor)) as NodeEditor;
GUIScaleUtility.CheckInit();
}
}
private static string getFullPath(string root, string targetFolderName)
{
string[] dirs = Directory.GetDirectories(root, targetFolderName, SearchOption.AllDirectories);
// Return first occurance containing targetFolderName.
if (dirs.Length != 0)
{
return dirs[0];
}
// Could not find anything.
return "";
}
public static string GetTextureFolderPath()
{
string fullpath = Application.dataPath;// getFullPath(Application.dataPath, "");
if (!string.IsNullOrEmpty(fullpath))
{
// Return the texture folder path relative to Unity's Asset folder.
int index = fullpath.IndexOf("Assets");
string localPath = fullpath.Substring(index);
return localPath;
}
Debug.LogError("Could not find folder: " + "");
return "";
}
Texture2D gridTex;
Texture2D _gridTex
{
get
{
if(gridTex==null)
{
string path = GetTextureFolderPath().Dir("Grid.png");
gridTex = AssetDatabase.LoadAssetAtPath<Texture2D>(path);
// Debug.Log(path);
if(gridTex==null)
{
Debug.Log("gridTex is null");
}
}
return gridTex;
}
}
Texture2D circleTex;
Texture2D _circleTex
{
get
{
if (circleTex == null)
{
string path = GetTextureFolderPath().Dir("Circle.png");
circleTex = AssetDatabase.LoadAssetAtPath<Texture2D>(path);
// Debug.Log(path);
if (circleTex == null)
{
Debug.Log("circleTex is null");
}
}
return circleTex;
}
}
private void OnGUI()
{
if (_window)
{
if (Event.current.type == EventType.Repaint)
{
if (bDrawGuide)
{ DrawGrid(); }
}
Rect graphRect = _window.Size;
var center = graphRect.size / 2f;
_zoomAdjustme