Unity Editor Script 指定组件右键添加功能按钮

    //在指定脚本中右键添加按钮  CONTEXT/组件名/按钮名
    [MenuItem("CONTEXT/PlayerHealth/InitHealthAndSpeed")]
    //MenuCommand : 当前右键点击的组件
    static void InitHealthAndSpeed(MenuCommand cmd)
    {
        PlayerHealth health = cmd.context as PlayerHealth;
        health.startingHealth = 200;
        Debug.Log("Init");
    }
    [MenuItem("CONTEXT/Rigidbody/Clear")]
    static void ClearMassAndGravity(MenuCommand cmd)
    {
        Rigidbody rgd = cmd.context as Rigidbody;
        rgd.mass = 0.1f;
        rgd.useGravity = false;
    }
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerHealth : MonoBehaviour
{
    public int startingHealth;
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        
    }
}

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Unity3D中Console控制台右键功能可以通过编写自定义的Editor扩展来进行添加。以下是添加右键功能的步骤: 1. 创建Editor扩展脚本:在Unity项目中创建一个Editor文件夹,然后在文件夹中创建一个C#脚本,命名为"ConsoleContextMenu.cs"。 2. 实现OnGUI函数:在ConsoleContextMenu.cs中实现OnGUI函数,该函数将在右键菜单中显示。以下是一个示例: ```csharp using UnityEditor; using UnityEngine; public class ConsoleContextMenu : Editor { [MenuItem("CONTEXT/LogEntry/Copy Log Message")] private static void CopyLogMessage(MenuCommand menuCommand) { LogEntry logEntry = (LogEntry)menuCommand.context; GUIUtility.systemCopyBuffer = logEntry.message; } private void OnGUI() { if (Event.current.type == EventType.MouseUp && Event.current.button == 1) { GenericMenu menu = new GenericMenu(); menu.AddItem(new GUIContent("Copy Log Message"), false, CopyLogMessage, LogEntries.GetEntryFromInstanceID(Selection.activeInstanceID)); menu.ShowAsContext(); } } } ``` 在上述代码中,我们为Console控制台添加了一个"Copy Log Message"的右键菜单选项。当用户右键点击Console控制台中的日志条目时,该选项将在右键菜单中显示,并且用户可以选择该选项以将日志消息复制到剪贴板中。 3. 将Editor扩展脚本添加到项目中:将ConsoleContextMenu.cs文件拖放到Editor文件夹中,然后保存脚本。 4. 测试右键功能:在Unity编辑器中按下Ctrl+Shift+C键打开Console控制台,并在控制台中添加一些日志消息。然后右键单击任意日志条目,应该会看到"Copy Log Message"选项在右键菜单中显示。 以上是为Unity3D中的Console控制台添加右键功能的步骤。您可以根据需要添加其他右键菜单选项,并使用相应的函数来实现它们。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值