1.在Assets文件下建立一个Gizmos文件夹,保存一张名叫gizmos01.tif图片
2.创建一个PathNode脚本用来设置父路点子路点,脚本代码如下:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PathNode : MonoBehaviour {
//父路点
public PathNode m_parent;
//子路点
public PathNode m_next;
public void SetNext(PathNode node)
{
if(m_next!=null)
{
m_next.m_parent = null;
}
//设置当前路点的子路点和父路点
m_next = node;
node.m_parent = this;
}
private void OnDrawGizmos()
{
//设置当前路点的显示图片信息(游戏运行时不显示)
Gizmos.DrawIcon(this.transform.position, "gizmos01.tif");
}
}
3.在Assets文件下建立一个Editor(注意大小写)文件夹,用来存放设置编辑器菜单栏脚本.
4.在Editor文件夹下创建一个PathTool脚本,代码如下:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
public class PathTool : ScriptableObject
{
static PathNode tool_parent = null;
//设置菜单栏--父路点设置 快捷键Ctrl+q
[MenuItem("PathTool/Set Parent %q")]
static void SetParent()
{
//没有选择路点或者选择路点的个数大于1就直接退出
if (!Selection.activeGameObject || Selection.GetTransforms(SelectionMode.Unfiltered).Length > 1)
{
return;
}
//如果选择的物体标签是pathnode
if (Selection.activeGameObject.tag == "pathnode")
{
//就保存物体上的路点
tool_parent = Selection.activeGameObject.GetComponent<PathNode>();
}
}
[MenuItem("PathTool/Set Parent %q", true)]
static bool ValidateSelection()
{
//如果没有选择物体或者tool_parent没有保存路点或者选择物体个数大于1,父路点设置不可用
if (Selection.activeGameObject==null || Selection.activeGameObject.tag!="pathnode")
{
return false;
}
else
{
return true;
}
}
//设置菜单栏--子路点菜单,快捷键Ctrl+w
[MenuItem("PathTool/Set next %w")]
static void SetNextNode()
{
if (!Selection.activeGameObject || Selection.GetTransforms(SelectionMode.Unfiltered).Length > 1 || tool_parent == null)
{
return;
}
if (Selection.activeGameObject.tag == "pathnode")
{
tool_parent.SetNext(Selection.activeGameObject.GetComponent<PathNode>());
tool_parent = null;
}
}
//选择的物体不是路点时禁用
[MenuItem("PathTool/Set next %w", true)]
static bool ValidateSelection1()
{
//如果没有选择物体或者tool_parent没有保存路点或者选择物体个数大于1,父路点设置不可用
if (Selection.activeGameObject == null || tool_parent == null || Selection.activeGameObject.tag!="pathnode")
{
return false;
}
else
{
return true;
}
}
}
具体操作步骤如下:
1.给所有路点设置标签为pathnode.
2.选择当前路点(例如:A),菜单栏PathTool—setParent.
3.选择下一个路点(例如:B),菜单栏PathTool—setNext.