unity编辑模式下创建若干子物体父物体

来源:互联网 发布:nginx加tomcat 编辑:程序博客网 时间:2024/05/22 06:10

在编辑模式下,创建若干个物体并且标注谁的谁的子物体,谁是谁的父物体

1 首先使用脚本创建空物体,在菜单中显示出来


using UnityEngine;using System.Collections;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;    }    // 显示图标    void OnDrawGizmos(){        Gizmos.DrawIcon(this.transform.position,"Node.tif");    }}


2 然后 设置每个物体的层级关系



using UnityEngine;using System.Collections;using UnityEditor;public class PathTool : ScriptableObject {//父路点    static PathNode m_parent = null;    static int num = 0;    [MenuItem("PathTools/Create PathNode")]    static void CreatePathNode() {        GameObject go = new GameObject();        go.AddComponent<PathNode>();        go.name = "pathnode"+num++;        go.tag = "pathnode";        Selection.activeTransform = go.transform;    }    [MenuItem("PathTools/set Parent %q")]    static void SetParent() {        if (!Selection.activeObject || Selection.GetTransforms(SelectionMode.Unfiltered).Length > 1) return;//编辑状态下没有选中物体        if (Selection.activeGameObject.tag.CompareTo("pathnode") == 0) {            m_parent = Selection.activeGameObject.GetComponent<PathNode>();                }    }    [MenuItem("PathTools/Set Child %w")]    static void setChild() {        if (!Selection.activeGameObject || Selection.GetTransforms(SelectionMode.Unfiltered).Length > 1) return;        if (Selection.activeGameObject.tag.CompareTo("pathnode") == 0) {            if (m_parent == null) {                Debug.LogError("先设置子节点");                return;            }            m_parent.setNext(Selection.activeGameObject.GetComponent<PathNode>());//父节点上面保存了, 将当前的节点作为上一个父节点的子节点            m_parent = null;        }    }    }


原创粉丝点击