Unity 自定义NodeEditor 开发插件基础

本文将介绍如何在Unity引擎中开发自定义的NodeEditor插件,帮助你理解和实现游戏逻辑或流程的可视化编辑。通过这个教程,你将学习到如何创建和配置NodeEditor,以及如何使用它来增强项目的开发效率。
摘要由CSDN通过智能技术生成


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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值