unity3D自定义菜单项

有时候 我们想在菜单项中添加自己定义的快速添加组件的选项

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

以下语句就可以

一定要记住加的命名空间 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;
         
        } 
    }

}

猜你喜欢

转载自blog.csdn.net/u012842807/article/details/49177943