当前位置:   article > 正文

Unity编辑器(一)-- 自定义UI拖拽_unity 实现ui拖拽 旋转

unity 实现ui拖拽 旋转

没有专门处理UI界面岗位的情况下, 开发者需要自己拼合界面,但是Unity默认的UI创建方式有些不太舒服,需要先Create再选中UI最后才能选中自己想要的UI生成出来,而且创建列表内容较多,没办法直接一眼找到,所以做了一个自定义的UI拖拽,先上示例。

 //视频上传维护中。。

 核心思路:

1.创建一个单独编辑器面板

2.可自定义配置UI资源,包括自定义的UI界面

3.可通过拖拽的方式生成对应UI

技术点梳理:

1.EditorWindow,用于制作单独编辑器面板

2.DragAndDrop,用于实现核心拖拽功能

API分析:

相关API可以在Unity官方文档中查看,这里只是我的个人理解。

DragAndDrop.StartDrag

开始拖动操作,在启动拖动之后才有相关的拖拽数据产生,EventType才会触发DragUpdated等与拖拽相关的事件。

DragAndDrop.GetGenericData:

DragAndDrop.SetGenericData

用于对拖拽数据的赋值和获取。

DragAndDrop.PrepareStartDrag:

清空拖拽数据以及拖拽对象,用于数据初始化。

DragAndDrop.AcceptDrag:

接受拖动操作,相当于一个触发器。

DragAndDrop.visualMode

Unity原生的用于修改拖拽时的鼠标样式。

脚本中并没有写有关于生成的逻辑,只有对数据的储存,操作以及DragAndDrop的逻辑,这是因为在Drag的底层调用了Unity原生的生成逻辑,也就是DragAndDropVisualMode.Copy,猜想这部分的逻辑和对预制体拖拽的逻辑是一套的,目前没有查到相关的信息,大佬可留言指教。

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEditor;
  5. using UnityEngine.EventSystems;
  6. using System.Linq;
  7. public class UITool : EditorWindow
  8. {
  9. class Item
  10. {
  11. public GameObject prefab;
  12. public Texture tex;
  13. }
  14. [MenuItem("Tools/UITool")]
  15. public static void OpenUITool()
  16. {
  17. UITool tool = EditorWindow.GetWindow<UITool>(false,"UITool",true);
  18. tool.autoRepaintOnSceneChange = true;
  19. tool.Show();
  20. }
  21. private Dictionary<string, Item> creatItemDic = new Dictionary<string, Item>();
  22. private List<Item> creatItemLst = new List<Item>();
  23. private void OnEnable()
  24. {
  25. creatItemDic.Clear();
  26. creatItemLst.Clear();
  27. object[] uiPrefabs = Resources.LoadAll("UIPrefab");
  28. string prefabName = string.Empty;
  29. for (int i = 0; i < uiPrefabs.Length; i++)
  30. {
  31. Item item = new Item();
  32. item.prefab = uiPrefabs[i] as GameObject;
  33. prefabName = item.prefab.name;
  34. Texture tex = Resources.Load<Texture>("UITexture/" + prefabName);
  35. if (tex == null)
  36. {
  37. tex = Resources.Load<Texture>("Unity");
  38. }
  39. item.tex = tex;
  40. creatItemDic.Add(prefabName+i, item);
  41. creatItemLst.Add(item);
  42. }
  43. //可增加自定义路径
  44. ; }
  45. private Vector2 mPos = Vector2.zero;
  46. private int cellSize = 65;
  47. const int cellPaddingX = 10;
  48. const int cellPaddingY = 15;
  49. Event curEvent;
  50. EventType eventType;
  51. int curIndex = -1;
  52. int spacingX = 0;
  53. int spacingY = 0;
  54. private void OnGUI()
  55. {
  56. RefreshGridPos();
  57. curEvent = Event.current;
  58. eventType = curEvent.type;
  59. curIndex = GetCellUnderMouse(spacingX, spacingY);
  60. switch (eventType)
  61. {
  62. case EventType.MouseDown:
  63. if (curIndex < creatItemLst.Count)
  64. {
  65. Item item = creatItemLst[curIndex];
  66. DraggedObject = item.prefab;
  67. Debug.Log(curIndex);
  68. }
  69. else
  70. {
  71. DraggedObject = null;
  72. Debug.Log("越界");
  73. }
  74. break;
  75. case EventType.MouseDrag:
  76. if (isDragObjectInOur)
  77. DragAndDrop.StartDrag("UITool");
  78. curEvent.Use();
  79. break;
  80. case EventType.DragUpdated:
  81. UpdateVisual();
  82. curEvent.Use();
  83. break;
  84. default:
  85. break;
  86. }
  87. }
  88. void RefreshGridPos()
  89. {
  90. GUILayout.BeginVertical();
  91. mPos = EditorGUILayout.BeginScrollView(mPos);
  92. Color normal = new Color(1f, 1f, 1f, 0.5f);
  93. Rect texRect;
  94. Rect labelRect;
  95. int x = cellPaddingX, y = cellPaddingY;
  96. int width = Screen.width - cellPaddingX;
  97. spacingX = cellSize + cellPaddingX;
  98. spacingY = cellSize + cellPaddingY;
  99. int offsetCellSize = cellSize - 10;
  100. var labelStyle = EditorStyles.label;
  101. labelStyle.alignment = TextAnchor.LowerCenter;
  102. labelStyle.fontStyle = FontStyle.Bold;
  103. labelStyle.wordWrap = true;
  104. foreach (var item in creatItemDic)
  105. {
  106. texRect = new Rect(x ,y, offsetCellSize, offsetCellSize);
  107. Rect inner = texRect;
  108. inner.xMin -= 8f;
  109. inner.xMax += 8f;
  110. GUI.DrawTexture(texRect, item.Value.tex);
  111. inner.y += 14.5f;
  112. GUI.Label(inner, item.Value.prefab.name, labelStyle);
  113. GUI.backgroundColor = Color.black;
  114. x += spacingX;
  115. if (x + spacingX > width)
  116. {
  117. y += spacingY;
  118. x = cellPaddingX;
  119. }
  120. }
  121. GUILayout.Space(y + spacingY);
  122. EditorGUILayout.EndScrollView();
  123. GUILayout.EndHorizontal();
  124. }
  125. int GetCellUnderMouse(int spacingX, int spacingY)
  126. {
  127. Vector2 pos = Event.current.mousePosition + mPos;
  128. int x = cellPaddingX, y = cellPaddingY;
  129. if (pos.y < y) return -1;
  130. float width = Screen.width - cellPaddingX + mPos.x;
  131. float height = Screen.height - cellPaddingY + mPos.y;
  132. int index = 0;
  133. for (; ; ++index)
  134. {
  135. Rect rect = new Rect(x, y, spacingX, spacingY);
  136. if (rect.Contains(pos)) break;
  137. x += spacingX;
  138. if (x + spacingX > width)
  139. {
  140. if (pos.x > x) return -1;
  141. y += spacingY;
  142. x = cellPaddingX;
  143. if (y + spacingY > height) return -1;
  144. }
  145. }
  146. return index;
  147. }
  148. bool isDragObjectInOur
  149. {
  150. get
  151. {
  152. object obj = DragAndDrop.GetGenericData("UITool");
  153. if (obj == null) return false;
  154. return (bool)obj;
  155. }
  156. set
  157. {
  158. DragAndDrop.SetGenericData("UITool", value);
  159. }
  160. }
  161. GameObject draggedObject;
  162. GameObject DraggedObject
  163. {
  164. get
  165. {
  166. return draggedObject;
  167. }
  168. set { draggedObject = value;
  169. if (draggedObject != null)
  170. {
  171. DragAndDrop.PrepareStartDrag();
  172. DragAndDrop.objectReferences = new Object[] { draggedObject };
  173. isDragObjectInOur = true;
  174. }
  175. else
  176. {
  177. DragAndDrop.AcceptDrag();
  178. }
  179. }
  180. }
  181. void UpdateVisual()
  182. {
  183. if (DraggedObject == null) DragAndDrop.visualMode = DragAndDropVisualMode.Rejected;
  184. else if (isDragObjectInOur) DragAndDrop.visualMode = DragAndDropVisualMode.Move;
  185. else DragAndDrop.visualMode = DragAndDropVisualMode.Copy;
  186. }
  187. }

Github上有个不错的插件:https://github.com/liuhaopen/UGUI-Editor

 Over!

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小蓝xlanll/article/detail/108880?site
推荐阅读
相关标签
  

闽ICP备14008679号