unity編輯模式下建立若干子物體父物體
阿新 • • 發佈:2019-02-04
在編輯模式下,建立若干個物體並且標註誰的誰的子物體,誰是誰的父物體
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; } } }