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

unity3D自定义菜单项

2015-10-16 14:25 435 查看
有时候 我们想在菜单项中添加自己定义的快速添加组件的选项

其实方法很简单只要在脚本中添加

以下语句就可以

一定要记住加的命名空间 using UnityEditor;

[MenuItem("第一级菜单/第二级菜单")]

在我们的菜单中就有了这个选项

那么该如何添加需要的组件呢??

private static void EnableRotate(MenuCommand command)

{

Debug.Log("测试");

GameObject obj = Selection.activeGameObject;

if (obj==null)

{

Debug.log("null ");

}

else

{

obj.AddComponent<ParticleSystem>();

obj.GetComponent<ParticleSystem>().startColor = Color.red;

}

}

我们就顺利的添加了想要的组件 粒子

完整代码:

using System.Collections;

using UnityEditor;

using UnityEngine;

public class ConfigTest :MonoBehaviour {

[MenuItem("第一/第二")]

private static void EnableRotate(MenuCommand command)

{

Debug.Log("测试");

GameObject obj = Selection.activeGameObject;

if (obj==null)

{

Debug.Log("null");

}

else

{

obj.AddComponent<ParticleSystem>();

obj.GetComponent<ParticleSystem>().startColor = Color.red;

}

}

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