Unity编辑模式下创建若干子物体父物体
发表于2018-10-29
在unity编辑模式下,想要创建若干个物体并且标注谁的谁的子物体,谁是谁的父物体该怎么办,下面就给大家介绍下实现方法,步骤如下:
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; } } }
如社区发表内容存在侵权行为,您可以点击这里查看侵权投诉指引