Unity 添加鼠标右键事件

 

把此类放到 Editor下使用就OK

 1 using UnityEngine;
 2 using System.Collections;
 3 using System.Collections.Generic;
 4 using UnityEditor;
 5 
 6 /// <summary>
 7 /// 添加鼠标右键事件
 8 /// </summary>
 9 [InitializeOnLoad]
10 [ExecuteInEditMode]
11 public static class AddMouseRight
12 {
13 
14     static AddMouseRight()
15     {
16         SceneView.onSceneGUIDelegate = OnSceneFunc;
17     }
18 
19     private class Item
20     {
21         public string MenuName { get; set; }
22         public GenericMenu.MenuFunction2 Call { get; set; }
23     }
24     static List<Item> S_MenuList = new List<Item>();
25     public static void AddMenu(string menuName, GenericMenu.MenuFunction2 call)
26     {
27         Item item = new Item();
28         item.MenuName = menuName;
29         item.Call = call;
30         S_MenuList.Add(item);
31     }
32 
33     static void OnSceneFunc(SceneView sceneView)
34     {
35         if (S_MenuList.Count == 0)
36         {
37             return;
38         }
39 
40         if (Event.current.isMouse && Event.current.button == 1)
41         {
42             Vector3 p = Event.current.mousePosition;
43 
44             GenericMenu menu = new GenericMenu();
45 
46             foreach(Item i in S_MenuList)
47             {
48                 menu.AddItem(new GUIContent(i.MenuName), false, i.Call, p);
49             }
50             menu.ShowAsContext();
51 
52             Event.current.Use();
53         }
54     }
55 
56     public static void Reset()
57     {
58         while(S_MenuList.Count > 0)
59         {
60             S_MenuList.RemoveAt(0);
61         }
62     }
63 
64 
65 }

 

转载于:https://www.cnblogs.com/GameDeveloper/p/3974223.html

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值