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

Unity脚本与编辑器

2013-09-11 22:38 260 查看
Unity中的脚本能够直接在Unity编辑器中修改一些界面,及添加功能菜单,今天写游戏Demo用到如下:

1.在Component中添加子菜单项

[AddComponentMenu("Defend Homeland/GridNode")]
public class GridNode : MonoBehaviour
{
......
}




,相应地在Unity的Component菜单下就会有我添加的脚本



2.可以在编辑状态下执行函数





//构建地图
[ContextMenu("BuildMap")]
void BuildMap ()
{
.....
}


当把脚本绑定在GameObject上面是,点击右上角的就能出现在代码中添加的函数



3.公共类型,但不在Inspector显示





//不在Inspector显示
[HideInInspector]
public int m_life = 3;//生命
public int m_wave = 1;//波数
public int m_point = 10;//点数


而这样在检视面板中就不会显示出m_life这个public类型,只有其它两个





4.自定义菜单栏+功能键

using UnityEngine;
using UnityEditor;
using System.Collections;

/// <summary>
///
/// 作用:
/// 日期:2013-09-11
/// </summary>

[AddComponentMenu("Defend Homeland/PathTool")]
public class PathTool : ScriptableObject
{
static PathNode m_parent = null;

//新建一个菜单项【PathTool】,子菜单项(Set Parent)快捷键为 Ctrl + Q
[MenuItem("PathTool/Set Parent %q")]
static void SetParent ()
{
//如果没有选中任何物体,或选择物体数量大于1,则返回
if (!Selection.activeGameObject || Selection.GetTransforms (SelectionMode.Unfiltered).Length > 1)
return;

//如果选中,将选中的物体的tag设为pathnode
if (Selection.activeGameObject.tag.CompareTo ("pathnode") == 0) {
//设置父节点
m_parent = Selection.activeGameObject.GetComponent<PathNode> ();
}
}

//新建菜单项[PathTool/Set NextChild] ,快捷键为Ctrl+w
[MenuItem("PathTool/Set NextChild %w")]
static void SetNextChild ()
{
//如果没有选中任何物体,或选择的物体数量大于1,则返回
if (!Selection.activeGameObject || Selection.GetTransforms (SelectionMode.Unfiltered).Length > 1)
return;

if (Selection.activeGameObject.tag.CompareTo ("pathnode") == 0) {
//设置子节点
m_parent.SetNext (Selection.activeGameObject.GetComponent<PathNode> ());
m_parent = null;
}
}
}
回到Unity编辑器界面,界面发生了如下变化




注:这些经验及小技巧来自我读的《Unity3D手机游戏开发》一书,感谢编者!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: