码迷,mamicode.com
首页 > 其他好文 > 详细

Menu学习

时间:2016-01-07 11:38:30      阅读:156      评论:0      收藏:0      [点我收藏+]

标签:

using UnityEditor;
using UnityEngine;
public class Menu : MonoBehaviour {

    // Add a menu item named "Do Something" to MyMenu in the menu bar.
    [MenuItem ("MyMenu/Do Something")]
    static void DoSomething () {
        Debug.Log ("Doing Something...");
    }

    // Validated menu item.
    // Add a menu item named "Log Selected Transform Name" to MyMenu in the menu bar.
    // We use a second function to validate the menu item
    // so it will only be enabled if we have a transform selected.
    [MenuItem ("MyMenu/Log Selected Transform Name")]
    static void LogSelectedTransformName ()
    {
        Debug.Log ("Selected Transform is on " + Selection.activeTransform.gameObject.name + ".");
    }

    // Validate the menu item defined by the function above.
    // The menu item will be disabled if this function returns false.
    [MenuItem ("MyMenu/Log Selected Transform Name", true)]
    static bool ValidateLogSelectedTransformName () {
        // Return false if no transform is selected.
        return Selection.activeTransform != null;
    }


    // Add a menu item named "Do Something with a Shortcut Key" to MyMenu in the menu bar
    // and give it a shortcut (ctrl-g on Windows, cmd-g on OS X).
    [MenuItem ("MyMenu/Do Something with a Shortcut Key %g")]
    static void DoSomethingWithAShortcutKey () {
        Debug.Log ("Doing something with a Shortcut Key...");
    }

    // Add a menu item called "Double Mass" to a Rigidbody‘s context menu.
    [MenuItem ("CONTEXT/Rigidbody/Double Mass")]
    static void DoubleMass (MenuCommand command) {
        Rigidbody body = (Rigidbody)command.context;
        body.mass = body.mass * 2;
        Debug.Log ("Doubled Rigidbody‘s Mass to " + body.mass + " from Context Menu.");
    }

    // Add a menu item to create custom GameObjects.
    // Priority 1 ensures it is grouped with the other menu items of the same kind
    // and propagated to the hierarchy dropdown and hierarch context menus. 
    [MenuItem("GameObject/MyCategory/Custom Game Object", false, 10)]
    static void CreateCustomGameObject(MenuCommand menuCommand) {
        // Create a custom game object
        GameObject go = new GameObject("Custom Game Object");
        // Ensure it gets reparented if this was a context click (otherwise does nothing)
        GameObjectUtility.SetParentAndAlign(go, menuCommand.context as GameObject);
        // Register the creation in the undo system
        Undo.RegisterCreatedObjectUndo(go, "Create " + go.name);
        Selection.activeObject = go;
    }
}
技术分享

Menu学习

标签:

原文地址:http://www.cnblogs.com/panyingying/p/5109051.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!