您的位置:首页 > 移动开发 > Unity3D

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

2017-05-29 19:10 489 查看
在编辑模式下,创建若干个物体并且标注谁的谁的子物体,谁是谁的父物体
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;
}
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: