首页 > 其他 > 详细

Unity 添加鼠标右键事件

时间:2014-09-16 10:33:40      阅读:1131      评论:0      收藏:0      [点我收藏+]

 

把此类放到 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 }

 

Unity 添加鼠标右键事件

原文:http://www.cnblogs.com/GameDeveloper/p/3974223.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!