Selection.activeGameObject

切换到手册
public static GameObject activeGameObject ;

描述

返回处于活动状态的游戏对象。(显示在检视面板中的对象)。

它还将返回可能为预制件或不可修改对象的游戏对象。

    // Rotates the selected Game Object +45 degrees if the user presses 'g'
    // or -45 degrees if the user presses 'Shift + g'
    // If no object is selected, the Menus are grayed out.

@MenuItem ("Example/Rotate Green +45 _g") static function RotateGreenPlus45() { var obj = Selection.activeGameObject; obj.transform.Rotate(Vector3.up*45); }

@MenuItem ("Example/Rotate Green +45 _g", true) static function ValidatePlus45() { return Selection.activeGameObject != null; }

@MenuItem ("Example/Rotate green -45 #g") static function RotateGreenMinus45() { var obj = Selection.activeGameObject; obj.transform.Rotate(Vector3.down*45); }

@MenuItem ("Example/Rotate green -45 #g", true) static function ValidateMinus45() { return Selection.activeGameObject != null; }
Copyright © 2023 Unity Technologies
优美缔软件(上海)有限公司 版权所有
"Unity"、Unity 徽标及其他 Unity 商标是 Unity Technologies 或其附属机构在美国及其他地区的商标或注册商标。其他名称或品牌是其各自所有者的商标。
公安部备案号:
31010902002961