PageRenderTime 68ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 1ms

/Assets/NGUI/Scripts/Editor/NGUIEditorTools.cs

https://bitbucket.org/chiphuc113/unknowproject
C# | 2188 lines | 1441 code | 383 blank | 364 comment | 386 complexity | 156341315f9809967bcb2f0527c1a257 MD5 | raw file

Large files files are truncated, but you can click here to view the full file

  1. //----------------------------------------------
  2. // NGUI: Next-Gen UI kit
  3. // Copyright Š 2011-2014 Tasharen Entertainment
  4. //----------------------------------------------
  5. using UnityEditor;
  6. using UnityEngine;
  7. using System.Collections.Generic;
  8. using System.Reflection;
  9. /// <summary>
  10. /// Tools for the editor
  11. /// </summary>
  12. public static class NGUIEditorTools
  13. {
  14. static Texture2D mBackdropTex;
  15. static Texture2D mContrastTex;
  16. static Texture2D mGradientTex;
  17. static GameObject mPrevious;
  18. /// <summary>
  19. /// Returns a blank usable 1x1 white texture.
  20. /// </summary>
  21. static public Texture2D blankTexture
  22. {
  23. get
  24. {
  25. return EditorGUIUtility.whiteTexture;
  26. }
  27. }
  28. /// <summary>
  29. /// Returns a usable texture that looks like a dark checker board.
  30. /// </summary>
  31. static public Texture2D backdropTexture
  32. {
  33. get
  34. {
  35. if (mBackdropTex == null) mBackdropTex = CreateCheckerTex(
  36. new Color(0.1f, 0.1f, 0.1f, 0.5f),
  37. new Color(0.2f, 0.2f, 0.2f, 0.5f));
  38. return mBackdropTex;
  39. }
  40. }
  41. /// <summary>
  42. /// Returns a usable texture that looks like a high-contrast checker board.
  43. /// </summary>
  44. static public Texture2D contrastTexture
  45. {
  46. get
  47. {
  48. if (mContrastTex == null) mContrastTex = CreateCheckerTex(
  49. new Color(0f, 0.0f, 0f, 0.5f),
  50. new Color(1f, 1f, 1f, 0.5f));
  51. return mContrastTex;
  52. }
  53. }
  54. /// <summary>
  55. /// Gradient texture is used for title bars / headers.
  56. /// </summary>
  57. static public Texture2D gradientTexture
  58. {
  59. get
  60. {
  61. if (mGradientTex == null) mGradientTex = CreateGradientTex();
  62. return mGradientTex;
  63. }
  64. }
  65. /// <summary>
  66. /// Create a white dummy texture.
  67. /// </summary>
  68. static Texture2D CreateDummyTex ()
  69. {
  70. Texture2D tex = new Texture2D(1, 1);
  71. tex.name = "[Generated] Dummy Texture";
  72. tex.hideFlags = HideFlags.DontSave;
  73. tex.filterMode = FilterMode.Point;
  74. tex.SetPixel(0, 0, Color.white);
  75. tex.Apply();
  76. return tex;
  77. }
  78. /// <summary>
  79. /// Create a checker-background texture
  80. /// </summary>
  81. static Texture2D CreateCheckerTex (Color c0, Color c1)
  82. {
  83. Texture2D tex = new Texture2D(16, 16);
  84. tex.name = "[Generated] Checker Texture";
  85. tex.hideFlags = HideFlags.DontSave;
  86. for (int y = 0; y < 8; ++y) for (int x = 0; x < 8; ++x) tex.SetPixel(x, y, c1);
  87. for (int y = 8; y < 16; ++y) for (int x = 0; x < 8; ++x) tex.SetPixel(x, y, c0);
  88. for (int y = 0; y < 8; ++y) for (int x = 8; x < 16; ++x) tex.SetPixel(x, y, c0);
  89. for (int y = 8; y < 16; ++y) for (int x = 8; x < 16; ++x) tex.SetPixel(x, y, c1);
  90. tex.Apply();
  91. tex.filterMode = FilterMode.Point;
  92. return tex;
  93. }
  94. /// <summary>
  95. /// Create a gradient texture
  96. /// </summary>
  97. static Texture2D CreateGradientTex ()
  98. {
  99. Texture2D tex = new Texture2D(1, 16);
  100. tex.name = "[Generated] Gradient Texture";
  101. tex.hideFlags = HideFlags.DontSave;
  102. Color c0 = new Color(1f, 1f, 1f, 0f);
  103. Color c1 = new Color(1f, 1f, 1f, 0.4f);
  104. for (int i = 0; i < 16; ++i)
  105. {
  106. float f = Mathf.Abs((i / 15f) * 2f - 1f);
  107. f *= f;
  108. tex.SetPixel(0, i, Color.Lerp(c0, c1, f));
  109. }
  110. tex.Apply();
  111. tex.filterMode = FilterMode.Bilinear;
  112. return tex;
  113. }
  114. /// <summary>
  115. /// Draws the tiled texture. Like GUI.DrawTexture() but tiled instead of stretched.
  116. /// </summary>
  117. static public void DrawTiledTexture (Rect rect, Texture tex)
  118. {
  119. GUI.BeginGroup(rect);
  120. {
  121. int width = Mathf.RoundToInt(rect.width);
  122. int height = Mathf.RoundToInt(rect.height);
  123. for (int y = 0; y < height; y += tex.height)
  124. {
  125. for (int x = 0; x < width; x += tex.width)
  126. {
  127. GUI.DrawTexture(new Rect(x, y, tex.width, tex.height), tex);
  128. }
  129. }
  130. }
  131. GUI.EndGroup();
  132. }
  133. /// <summary>
  134. /// Draw a single-pixel outline around the specified rectangle.
  135. /// </summary>
  136. static public void DrawOutline (Rect rect)
  137. {
  138. if (Event.current.type == EventType.Repaint)
  139. {
  140. Texture2D tex = contrastTexture;
  141. GUI.color = Color.white;
  142. DrawTiledTexture(new Rect(rect.xMin, rect.yMax, 1f, -rect.height), tex);
  143. DrawTiledTexture(new Rect(rect.xMax, rect.yMax, 1f, -rect.height), tex);
  144. DrawTiledTexture(new Rect(rect.xMin, rect.yMin, rect.width, 1f), tex);
  145. DrawTiledTexture(new Rect(rect.xMin, rect.yMax, rect.width, 1f), tex);
  146. }
  147. }
  148. /// <summary>
  149. /// Draw a single-pixel outline around the specified rectangle.
  150. /// </summary>
  151. static public void DrawOutline (Rect rect, Color color)
  152. {
  153. if (Event.current.type == EventType.Repaint)
  154. {
  155. Texture2D tex = blankTexture;
  156. GUI.color = color;
  157. GUI.DrawTexture(new Rect(rect.xMin, rect.yMin, 1f, rect.height), tex);
  158. GUI.DrawTexture(new Rect(rect.xMax, rect.yMin, 1f, rect.height), tex);
  159. GUI.DrawTexture(new Rect(rect.xMin, rect.yMin, rect.width, 1f), tex);
  160. GUI.DrawTexture(new Rect(rect.xMin, rect.yMax, rect.width, 1f), tex);
  161. GUI.color = Color.white;
  162. }
  163. }
  164. /// <summary>
  165. /// Draw a selection outline around the specified rectangle.
  166. /// </summary>
  167. static public void DrawOutline (Rect rect, Rect relative, Color color)
  168. {
  169. if (Event.current.type == EventType.Repaint)
  170. {
  171. // Calculate where the outer rectangle would be
  172. float x = rect.xMin + rect.width * relative.xMin;
  173. float y = rect.yMax - rect.height * relative.yMin;
  174. float width = rect.width * relative.width;
  175. float height = -rect.height * relative.height;
  176. relative = new Rect(x, y, width, height);
  177. // Draw the selection
  178. DrawOutline(relative, color);
  179. }
  180. }
  181. /// <summary>
  182. /// Draw a selection outline around the specified rectangle.
  183. /// </summary>
  184. static public void DrawOutline (Rect rect, Rect relative)
  185. {
  186. if (Event.current.type == EventType.Repaint)
  187. {
  188. // Calculate where the outer rectangle would be
  189. float x = rect.xMin + rect.width * relative.xMin;
  190. float y = rect.yMax - rect.height * relative.yMin;
  191. float width = rect.width * relative.width;
  192. float height = -rect.height * relative.height;
  193. relative = new Rect(x, y, width, height);
  194. // Draw the selection
  195. DrawOutline(relative);
  196. }
  197. }
  198. /// <summary>
  199. /// Draw a 9-sliced outline.
  200. /// </summary>
  201. static public void DrawOutline (Rect rect, Rect outer, Rect inner)
  202. {
  203. if (Event.current.type == EventType.Repaint)
  204. {
  205. Color green = new Color(0.4f, 1f, 0f, 1f);
  206. DrawOutline(rect, new Rect(outer.x, inner.y, outer.width, inner.height));
  207. DrawOutline(rect, new Rect(inner.x, outer.y, inner.width, outer.height));
  208. DrawOutline(rect, outer, green);
  209. }
  210. }
  211. /// <summary>
  212. /// Draw a checkered background for the specified texture.
  213. /// </summary>
  214. static public Rect DrawBackground (Texture2D tex, float ratio)
  215. {
  216. Rect rect = GUILayoutUtility.GetRect(0f, 0f);
  217. rect.width = Screen.width - rect.xMin;
  218. rect.height = rect.width * ratio;
  219. GUILayout.Space(rect.height);
  220. if (Event.current.type == EventType.Repaint)
  221. {
  222. Texture2D blank = blankTexture;
  223. Texture2D check = backdropTexture;
  224. // Lines above and below the texture rectangle
  225. GUI.color = new Color(0f, 0f, 0f, 0.2f);
  226. GUI.DrawTexture(new Rect(rect.xMin, rect.yMin - 1, rect.width, 1f), blank);
  227. GUI.DrawTexture(new Rect(rect.xMin, rect.yMax, rect.width, 1f), blank);
  228. GUI.color = Color.white;
  229. // Checker background
  230. DrawTiledTexture(rect, check);
  231. }
  232. return rect;
  233. }
  234. /// <summary>
  235. /// Draw a visible separator in addition to adding some padding.
  236. /// </summary>
  237. static public void DrawSeparator ()
  238. {
  239. GUILayout.Space(12f);
  240. if (Event.current.type == EventType.Repaint)
  241. {
  242. Texture2D tex = blankTexture;
  243. Rect rect = GUILayoutUtility.GetLastRect();
  244. GUI.color = new Color(0f, 0f, 0f, 0.25f);
  245. GUI.DrawTexture(new Rect(0f, rect.yMin + 6f, Screen.width, 4f), tex);
  246. GUI.DrawTexture(new Rect(0f, rect.yMin + 6f, Screen.width, 1f), tex);
  247. GUI.DrawTexture(new Rect(0f, rect.yMin + 9f, Screen.width, 1f), tex);
  248. GUI.color = Color.white;
  249. }
  250. }
  251. /// <summary>
  252. /// Convenience function that displays a list of sprites and returns the selected value.
  253. /// </summary>
  254. static public string DrawList (string field, string[] list, string selection, params GUILayoutOption[] options)
  255. {
  256. if (list != null && list.Length > 0)
  257. {
  258. int index = 0;
  259. if (string.IsNullOrEmpty(selection)) selection = list[0];
  260. // We need to find the sprite in order to have it selected
  261. if (!string.IsNullOrEmpty(selection))
  262. {
  263. for (int i = 0; i < list.Length; ++i)
  264. {
  265. if (selection.Equals(list[i], System.StringComparison.OrdinalIgnoreCase))
  266. {
  267. index = i;
  268. break;
  269. }
  270. }
  271. }
  272. // Draw the sprite selection popup
  273. index = string.IsNullOrEmpty(field) ?
  274. EditorGUILayout.Popup(index, list, options) :
  275. EditorGUILayout.Popup(field, index, list, options);
  276. return list[index];
  277. }
  278. return null;
  279. }
  280. /// <summary>
  281. /// Convenience function that displays a list of sprites and returns the selected value.
  282. /// </summary>
  283. static public string DrawAdvancedList (string field, string[] list, string selection, params GUILayoutOption[] options)
  284. {
  285. if (list != null && list.Length > 0)
  286. {
  287. int index = 0;
  288. if (string.IsNullOrEmpty(selection)) selection = list[0];
  289. // We need to find the sprite in order to have it selected
  290. if (!string.IsNullOrEmpty(selection))
  291. {
  292. for (int i = 0; i < list.Length; ++i)
  293. {
  294. if (selection.Equals(list[i], System.StringComparison.OrdinalIgnoreCase))
  295. {
  296. index = i;
  297. break;
  298. }
  299. }
  300. }
  301. // Draw the sprite selection popup
  302. index = string.IsNullOrEmpty(field) ?
  303. DrawPrefixList(index, list, options) :
  304. DrawPrefixList(field, index, list, options);
  305. return list[index];
  306. }
  307. return null;
  308. }
  309. /// <summary>
  310. /// Helper function that returns the selected root object.
  311. /// </summary>
  312. static public GameObject SelectedRoot () { return SelectedRoot(false); }
  313. /// <summary>
  314. /// Helper function that returns the selected root object.
  315. /// </summary>
  316. static public GameObject SelectedRoot (bool createIfMissing)
  317. {
  318. GameObject go = Selection.activeGameObject;
  319. // Only use active objects
  320. if (go != null && !NGUITools.GetActive(go)) go = null;
  321. // Try to find a panel
  322. UIPanel p = (go != null) ? NGUITools.FindInParents<UIPanel>(go) : null;
  323. // No selection? Try to find the root automatically
  324. if (p == null)
  325. {
  326. UIPanel[] panels = NGUITools.FindActive<UIPanel>();
  327. if (panels.Length > 0) go = panels[0].gameObject;
  328. }
  329. if (createIfMissing && go == null)
  330. {
  331. // No object specified -- find the first panel
  332. if (go == null)
  333. {
  334. UIPanel panel = GameObject.FindObjectOfType(typeof(UIPanel)) as UIPanel;
  335. if (panel != null) go = panel.gameObject;
  336. }
  337. // No UI present -- create a new one
  338. if (go == null) go = UICreateNewUIWizard.CreateNewUI(UICreateNewUIWizard.CameraType.Simple2D);
  339. }
  340. return go;
  341. }
  342. /// <summary>
  343. /// Helper function that checks to see if this action would break the prefab connection.
  344. /// </summary>
  345. static public bool WillLosePrefab (GameObject root)
  346. {
  347. if (root == null) return false;
  348. if (root.transform != null)
  349. {
  350. // Check if the selected object is a prefab instance and display a warning
  351. PrefabType type = PrefabUtility.GetPrefabType(root);
  352. if (type == PrefabType.PrefabInstance)
  353. {
  354. return EditorUtility.DisplayDialog("Losing prefab",
  355. "This action will lose the prefab connection. Are you sure you wish to continue?",
  356. "Continue", "Cancel");
  357. }
  358. }
  359. return true;
  360. }
  361. /// <summary>
  362. /// Change the import settings of the specified texture asset, making it readable.
  363. /// </summary>
  364. static public bool MakeTextureReadable (string path, bool force)
  365. {
  366. if (string.IsNullOrEmpty(path)) return false;
  367. TextureImporter ti = AssetImporter.GetAtPath(path) as TextureImporter;
  368. if (ti == null) return false;
  369. TextureImporterSettings settings = new TextureImporterSettings();
  370. ti.ReadTextureSettings(settings);
  371. if (force || !settings.readable || settings.npotScale != TextureImporterNPOTScale.None
  372. #if !UNITY_3_5 && !UNITY_4_0 && !UNITY_4_1
  373. || settings.alphaIsTransparency
  374. #endif
  375. )
  376. {
  377. settings.readable = true;
  378. settings.textureFormat = TextureImporterFormat.ARGB32;
  379. settings.npotScale = TextureImporterNPOTScale.None;
  380. #if !UNITY_3_5 && !UNITY_4_0 && !UNITY_4_1
  381. settings.alphaIsTransparency = false;
  382. #endif
  383. ti.SetTextureSettings(settings);
  384. AssetDatabase.ImportAsset(path, ImportAssetOptions.ForceUpdate | ImportAssetOptions.ForceSynchronousImport);
  385. }
  386. return true;
  387. }
  388. /// <summary>
  389. /// Change the import settings of the specified texture asset, making it suitable to be used as a texture atlas.
  390. /// </summary>
  391. static bool MakeTextureAnAtlas (string path, bool force, bool alphaTransparency)
  392. {
  393. if (string.IsNullOrEmpty(path)) return false;
  394. TextureImporter ti = AssetImporter.GetAtPath(path) as TextureImporter;
  395. if (ti == null) return false;
  396. TextureImporterSettings settings = new TextureImporterSettings();
  397. ti.ReadTextureSettings(settings);
  398. if (force ||
  399. settings.readable ||
  400. settings.maxTextureSize < 4096 ||
  401. settings.wrapMode != TextureWrapMode.Clamp ||
  402. settings.npotScale != TextureImporterNPOTScale.ToNearest)
  403. {
  404. settings.readable = false;
  405. settings.maxTextureSize = 4096;
  406. settings.wrapMode = TextureWrapMode.Clamp;
  407. settings.npotScale = TextureImporterNPOTScale.ToNearest;
  408. settings.textureFormat = TextureImporterFormat.ARGB32;
  409. settings.filterMode = FilterMode.Trilinear;
  410. settings.aniso = 4;
  411. #if !UNITY_3_5 && !UNITY_4_0 && !UNITY_4_1
  412. settings.alphaIsTransparency = alphaTransparency;
  413. #endif
  414. ti.SetTextureSettings(settings);
  415. AssetDatabase.ImportAsset(path, ImportAssetOptions.ForceUpdate | ImportAssetOptions.ForceSynchronousImport);
  416. }
  417. return true;
  418. }
  419. /// <summary>
  420. /// Fix the import settings for the specified texture, re-importing it if necessary.
  421. /// </summary>
  422. static public Texture2D ImportTexture (string path, bool forInput, bool force, bool alphaTransparency)
  423. {
  424. if (!string.IsNullOrEmpty(path))
  425. {
  426. if (forInput) { if (!MakeTextureReadable(path, force)) return null; }
  427. else if (!MakeTextureAnAtlas(path, force, alphaTransparency)) return null;
  428. //return AssetDatabase.LoadAssetAtPath(path, typeof(Texture2D)) as Texture2D;
  429. Texture2D tex = AssetDatabase.LoadAssetAtPath(path, typeof(Texture2D)) as Texture2D;
  430. AssetDatabase.Refresh(ImportAssetOptions.ForceSynchronousImport);
  431. return tex;
  432. }
  433. return null;
  434. }
  435. /// <summary>
  436. /// Fix the import settings for the specified texture, re-importing it if necessary.
  437. /// </summary>
  438. static public Texture2D ImportTexture (Texture tex, bool forInput, bool force, bool alphaTransparency)
  439. {
  440. if (tex != null)
  441. {
  442. string path = AssetDatabase.GetAssetPath(tex.GetInstanceID());
  443. return ImportTexture(path, forInput, force, alphaTransparency);
  444. }
  445. return null;
  446. }
  447. /// <summary>
  448. /// Figures out the saveable filename for the texture of the specified atlas.
  449. /// </summary>
  450. static public string GetSaveableTexturePath (UIAtlas atlas)
  451. {
  452. // Path where the texture atlas will be saved
  453. string path = "";
  454. // If the atlas already has a texture, overwrite its texture
  455. if (atlas.texture != null)
  456. {
  457. path = AssetDatabase.GetAssetPath(atlas.texture.GetInstanceID());
  458. if (!string.IsNullOrEmpty(path))
  459. {
  460. int dot = path.LastIndexOf('.');
  461. return path.Substring(0, dot) + ".png";
  462. }
  463. }
  464. // No texture to use -- figure out a name using the atlas
  465. path = AssetDatabase.GetAssetPath(atlas.GetInstanceID());
  466. path = string.IsNullOrEmpty(path) ? "Assets/" + atlas.name + ".png" : path.Replace(".prefab", ".png");
  467. return path;
  468. }
  469. /// <summary>
  470. /// Helper function that returns the folder where the current selection resides.
  471. /// </summary>
  472. static public string GetSelectionFolder ()
  473. {
  474. if (Selection.activeObject != null)
  475. {
  476. string path = AssetDatabase.GetAssetPath(Selection.activeObject.GetInstanceID());
  477. if (!string.IsNullOrEmpty(path))
  478. {
  479. int dot = path.LastIndexOf('.');
  480. int slash = Mathf.Max(path.LastIndexOf('/'), path.LastIndexOf('\\'));
  481. if (slash > 0) return (dot > slash) ? path.Substring(0, slash + 1) : path + "/";
  482. }
  483. }
  484. return "Assets/";
  485. }
  486. /// <summary>
  487. /// Struct type for the integer vector field below.
  488. /// </summary>
  489. public struct IntVector
  490. {
  491. public int x;
  492. public int y;
  493. }
  494. /// <summary>
  495. /// Integer vector field.
  496. /// </summary>
  497. static public IntVector IntPair (string prefix, string leftCaption, string rightCaption, int x, int y)
  498. {
  499. GUILayout.BeginHorizontal();
  500. if (string.IsNullOrEmpty(prefix))
  501. {
  502. GUILayout.Space(82f);
  503. }
  504. else
  505. {
  506. GUILayout.Label(prefix, GUILayout.Width(74f));
  507. }
  508. NGUIEditorTools.SetLabelWidth(48f);
  509. IntVector retVal;
  510. retVal.x = EditorGUILayout.IntField(leftCaption, x, GUILayout.MinWidth(30f));
  511. retVal.y = EditorGUILayout.IntField(rightCaption, y, GUILayout.MinWidth(30f));
  512. NGUIEditorTools.SetLabelWidth(80f);
  513. GUILayout.EndHorizontal();
  514. return retVal;
  515. }
  516. /// <summary>
  517. /// Integer rectangle field.
  518. /// </summary>
  519. static public Rect IntRect (string prefix, Rect rect)
  520. {
  521. int left = Mathf.RoundToInt(rect.xMin);
  522. int top = Mathf.RoundToInt(rect.yMin);
  523. int width = Mathf.RoundToInt(rect.width);
  524. int height = Mathf.RoundToInt(rect.height);
  525. NGUIEditorTools.IntVector a = NGUIEditorTools.IntPair(prefix, "Left", "Top", left, top);
  526. NGUIEditorTools.IntVector b = NGUIEditorTools.IntPair(null, "Width", "Height", width, height);
  527. return new Rect(a.x, a.y, b.x, b.y);
  528. }
  529. /// <summary>
  530. /// Integer vector field.
  531. /// </summary>
  532. static public Vector4 IntPadding (string prefix, Vector4 v)
  533. {
  534. int left = Mathf.RoundToInt(v.x);
  535. int top = Mathf.RoundToInt(v.y);
  536. int right = Mathf.RoundToInt(v.z);
  537. int bottom = Mathf.RoundToInt(v.w);
  538. NGUIEditorTools.IntVector a = NGUIEditorTools.IntPair(prefix, "Left", "Top", left, top);
  539. NGUIEditorTools.IntVector b = NGUIEditorTools.IntPair(null, "Right", "Bottom", right, bottom);
  540. return new Vector4(a.x, a.y, b.x, b.y);
  541. }
  542. /// <summary>
  543. /// Find all scene components, active or inactive.
  544. /// </summary>
  545. static public List<T> FindAll<T> () where T : Component
  546. {
  547. T[] comps = Resources.FindObjectsOfTypeAll(typeof(T)) as T[];
  548. List<T> list = new List<T>();
  549. foreach (T comp in comps)
  550. {
  551. if (comp.gameObject.hideFlags == 0)
  552. {
  553. string path = AssetDatabase.GetAssetPath(comp.gameObject);
  554. if (string.IsNullOrEmpty(path)) list.Add(comp);
  555. }
  556. }
  557. return list;
  558. }
  559. static public bool DrawPrefixButton (string text)
  560. {
  561. return GUILayout.Button(text, "DropDown", GUILayout.Width(76f));
  562. }
  563. static public bool DrawPrefixButton (string text, params GUILayoutOption[] options)
  564. {
  565. return GUILayout.Button(text, "DropDown", options);
  566. }
  567. static public int DrawPrefixList (int index, string[] list, params GUILayoutOption[] options)
  568. {
  569. return EditorGUILayout.Popup(index, list, "DropDown", options);
  570. }
  571. static public int DrawPrefixList (string text, int index, string[] list, params GUILayoutOption[] options)
  572. {
  573. return EditorGUILayout.Popup(text, index, list, "DropDown", options);
  574. }
  575. /// <summary>
  576. /// Draw a sprite preview.
  577. /// </summary>
  578. static public void DrawSprite (Texture2D tex, Rect rect, UISpriteData sprite, Color color)
  579. {
  580. DrawSprite(tex, rect, sprite, color, null);
  581. }
  582. /// <summary>
  583. /// Draw a sprite preview.
  584. /// </summary>
  585. static public void DrawSprite (Texture2D tex, Rect drawRect, UISpriteData sprite, Color color, Material mat)
  586. {
  587. if (!tex || sprite == null) return;
  588. DrawSprite(tex, drawRect, color, mat, sprite.x, sprite.y, sprite.width, sprite.height,
  589. sprite.borderLeft, sprite.borderBottom, sprite.borderRight, sprite.borderTop);
  590. }
  591. /// <summary>
  592. /// Draw a sprite preview.
  593. /// </summary>
  594. static public void DrawSprite (Texture2D tex, Rect drawRect, Color color, Rect textureRect, Vector4 border)
  595. {
  596. NGUIEditorTools.DrawSprite(tex, drawRect, color, null,
  597. Mathf.RoundToInt(textureRect.x),
  598. Mathf.RoundToInt(tex.height - textureRect.y - textureRect.height),
  599. Mathf.RoundToInt(textureRect.width),
  600. Mathf.RoundToInt(textureRect.height),
  601. Mathf.RoundToInt(border.x),
  602. Mathf.RoundToInt(border.y),
  603. Mathf.RoundToInt(border.z),
  604. Mathf.RoundToInt(border.w));
  605. }
  606. /// <summary>
  607. /// Draw a sprite preview.
  608. /// </summary>
  609. static public void DrawSprite (Texture2D tex, Rect drawRect, Color color, Material mat,
  610. int x, int y, int width, int height, int borderLeft, int borderBottom, int borderRight, int borderTop)
  611. {
  612. if (!tex) return;
  613. // Create the texture rectangle that is centered inside rect.
  614. Rect outerRect = drawRect;
  615. outerRect.width = width;
  616. outerRect.height = height;
  617. if (width > 0)
  618. {
  619. float f = drawRect.width / outerRect.width;
  620. outerRect.width *= f;
  621. outerRect.height *= f;
  622. }
  623. if (drawRect.height > outerRect.height)
  624. {
  625. outerRect.y += (drawRect.height - outerRect.height) * 0.5f;
  626. }
  627. else if (outerRect.height > drawRect.height)
  628. {
  629. float f = drawRect.height / outerRect.height;
  630. outerRect.width *= f;
  631. outerRect.height *= f;
  632. }
  633. if (drawRect.width > outerRect.width) outerRect.x += (drawRect.width - outerRect.width) * 0.5f;
  634. // Draw the background
  635. NGUIEditorTools.DrawTiledTexture(outerRect, NGUIEditorTools.backdropTexture);
  636. // Draw the sprite
  637. GUI.color = color;
  638. if (mat == null)
  639. {
  640. Rect uv = new Rect(x, y, width, height);
  641. uv = NGUIMath.ConvertToTexCoords(uv, tex.width, tex.height);
  642. GUI.DrawTextureWithTexCoords(outerRect, tex, uv, true);
  643. }
  644. else
  645. {
  646. // NOTE: There is an issue in Unity that prevents it from clipping the drawn preview
  647. // using BeginGroup/EndGroup, and there is no way to specify a UV rect... le'suq.
  648. UnityEditor.EditorGUI.DrawPreviewTexture(outerRect, tex, mat);
  649. }
  650. if (Selection.activeGameObject == null || Selection.gameObjects.Length == 1)
  651. {
  652. // Draw the border indicator lines
  653. GUI.BeginGroup(outerRect);
  654. {
  655. tex = NGUIEditorTools.contrastTexture;
  656. GUI.color = Color.white;
  657. if (borderLeft > 0)
  658. {
  659. float x0 = (float)borderLeft / width * outerRect.width - 1;
  660. NGUIEditorTools.DrawTiledTexture(new Rect(x0, 0f, 1f, outerRect.height), tex);
  661. }
  662. if (borderRight > 0)
  663. {
  664. float x1 = (float)(width - borderRight) / width * outerRect.width - 1;
  665. NGUIEditorTools.DrawTiledTexture(new Rect(x1, 0f, 1f, outerRect.height), tex);
  666. }
  667. if (borderBottom > 0)
  668. {
  669. float y0 = (float)(height - borderBottom) / height * outerRect.height - 1;
  670. NGUIEditorTools.DrawTiledTexture(new Rect(0f, y0, outerRect.width, 1f), tex);
  671. }
  672. if (borderTop > 0)
  673. {
  674. float y1 = (float)borderTop / height * outerRect.height - 1;
  675. NGUIEditorTools.DrawTiledTexture(new Rect(0f, y1, outerRect.width, 1f), tex);
  676. }
  677. }
  678. GUI.EndGroup();
  679. // Draw the lines around the sprite
  680. Handles.color = Color.black;
  681. Handles.DrawLine(new Vector3(outerRect.xMin, outerRect.yMin), new Vector3(outerRect.xMin, outerRect.yMax));
  682. Handles.DrawLine(new Vector3(outerRect.xMax, outerRect.yMin), new Vector3(outerRect.xMax, outerRect.yMax));
  683. Handles.DrawLine(new Vector3(outerRect.xMin, outerRect.yMin), new Vector3(outerRect.xMax, outerRect.yMin));
  684. Handles.DrawLine(new Vector3(outerRect.xMin, outerRect.yMax), new Vector3(outerRect.xMax, outerRect.yMax));
  685. // Sprite size label
  686. string text = string.Format("Sprite Size: {0}x{1}", Mathf.RoundToInt(width), Mathf.RoundToInt(height));
  687. EditorGUI.DropShadowLabel(GUILayoutUtility.GetRect(Screen.width, 18f), text);
  688. }
  689. }
  690. /// <summary>
  691. /// Draw the specified sprite.
  692. /// </summary>
  693. public static void DrawTexture (Texture2D tex, Rect rect, Rect uv, Color color)
  694. {
  695. DrawTexture(tex, rect, uv, color, null);
  696. }
  697. /// <summary>
  698. /// Draw the specified sprite.
  699. /// </summary>
  700. public static void DrawTexture (Texture2D tex, Rect rect, Rect uv, Color color, Material mat)
  701. {
  702. int w = Mathf.RoundToInt(tex.width * uv.width);
  703. int h = Mathf.RoundToInt(tex.height * uv.height);
  704. // Create the texture rectangle that is centered inside rect.
  705. Rect outerRect = rect;
  706. outerRect.width = w;
  707. outerRect.height = h;
  708. if (outerRect.width > 0f)
  709. {
  710. float f = rect.width / outerRect.width;
  711. outerRect.width *= f;
  712. outerRect.height *= f;
  713. }
  714. if (rect.height > outerRect.height)
  715. {
  716. outerRect.y += (rect.height - outerRect.height) * 0.5f;
  717. }
  718. else if (outerRect.height > rect.height)
  719. {
  720. float f = rect.height / outerRect.height;
  721. outerRect.width *= f;
  722. outerRect.height *= f;
  723. }
  724. if (rect.width > outerRect.width) outerRect.x += (rect.width - outerRect.width) * 0.5f;
  725. // Draw the background
  726. NGUIEditorTools.DrawTiledTexture(outerRect, NGUIEditorTools.backdropTexture);
  727. // Draw the sprite
  728. GUI.color = color;
  729. if (mat == null)
  730. {
  731. GUI.DrawTextureWithTexCoords(outerRect, tex, uv, true);
  732. }
  733. else
  734. {
  735. // NOTE: There is an issue in Unity that prevents it from clipping the drawn preview
  736. // using BeginGroup/EndGroup, and there is no way to specify a UV rect... le'suq.
  737. UnityEditor.EditorGUI.DrawPreviewTexture(outerRect, tex, mat);
  738. }
  739. GUI.color = Color.white;
  740. // Draw the lines around the sprite
  741. Handles.color = Color.black;
  742. Handles.DrawLine(new Vector3(outerRect.xMin, outerRect.yMin), new Vector3(outerRect.xMin, outerRect.yMax));
  743. Handles.DrawLine(new Vector3(outerRect.xMax, outerRect.yMin), new Vector3(outerRect.xMax, outerRect.yMax));
  744. Handles.DrawLine(new Vector3(outerRect.xMin, outerRect.yMin), new Vector3(outerRect.xMax, outerRect.yMin));
  745. Handles.DrawLine(new Vector3(outerRect.xMin, outerRect.yMax), new Vector3(outerRect.xMax, outerRect.yMax));
  746. // Sprite size label
  747. string text = string.Format("Texture Size: {0}x{1}", w, h);
  748. EditorGUI.DropShadowLabel(GUILayoutUtility.GetRect(Screen.width, 18f), text);
  749. }
  750. /// <summary>
  751. /// Draw a sprite selection field.
  752. /// </summary>
  753. static public void DrawSpriteField (string label, UIAtlas atlas, string spriteName, SpriteSelector.Callback callback, params GUILayoutOption[] options)
  754. {
  755. GUILayout.BeginHorizontal();
  756. GUILayout.Label(label, GUILayout.Width(76f));
  757. if (GUILayout.Button(spriteName, "MiniPullDown", options))
  758. {
  759. NGUISettings.atlas = atlas;
  760. NGUISettings.selectedSprite = spriteName;
  761. SpriteSelector.Show(callback);
  762. }
  763. GUILayout.EndHorizontal();
  764. }
  765. /// <summary>
  766. /// Draw a sprite selection field.
  767. /// </summary>
  768. static public void DrawPaddedSpriteField (string label, UIAtlas atlas, string spriteName, SpriteSelector.Callback callback, params GUILayoutOption[] options)
  769. {
  770. GUILayout.BeginHorizontal();
  771. GUILayout.Label(label, GUILayout.Width(76f));
  772. if (GUILayout.Button(spriteName, "MiniPullDown", options))
  773. {
  774. NGUISettings.atlas = atlas;
  775. NGUISettings.selectedSprite = spriteName;
  776. SpriteSelector.Show(callback);
  777. }
  778. NGUIEditorTools.DrawPadding();
  779. GUILayout.EndHorizontal();
  780. }
  781. /// <summary>
  782. /// Draw a sprite selection field.
  783. /// </summary>
  784. static public void DrawSpriteField (string label, string caption, UIAtlas atlas, string spriteName, SpriteSelector.Callback callback, params GUILayoutOption[] options)
  785. {
  786. GUILayout.BeginHorizontal();
  787. GUILayout.Label(label, GUILayout.Width(76f));
  788. if (atlas.GetSprite(spriteName) == null)
  789. spriteName = "";
  790. if (GUILayout.Button(spriteName, "MiniPullDown", options))
  791. {
  792. NGUISettings.atlas = atlas;
  793. NGUISettings.selectedSprite = spriteName;
  794. SpriteSelector.Show(callback);
  795. }
  796. if (!string.IsNullOrEmpty(caption))
  797. {
  798. GUILayout.Space(20f);
  799. GUILayout.Label(caption);
  800. }
  801. GUILayout.EndHorizontal();
  802. }
  803. /// <summary>
  804. /// Draw a simple sprite selection button.
  805. /// </summary>
  806. static public bool DrawSpriteField (UIAtlas atlas, string spriteName, SpriteSelector.Callback callback, params GUILayoutOption[] options)
  807. {
  808. if (atlas.GetSprite(spriteName) == null)
  809. spriteName = "";
  810. if (NGUIEditorTools.DrawPrefixButton(spriteName, options))
  811. {
  812. NGUISettings.atlas = atlas;
  813. NGUISettings.selectedSprite = spriteName;
  814. SpriteSelector.Show(callback);
  815. return true;
  816. }
  817. return false;
  818. }
  819. static string mEditedName = null;
  820. static string mLastSprite = null;
  821. /// <summary>
  822. /// Draw a sprite selection field.
  823. /// </summary>
  824. static public void DrawSpriteField (string label, SerializedObject ob, string spriteField, params GUILayoutOption[] options)
  825. {
  826. DrawSpriteField(label, ob, ob.FindProperty("atlas"), ob.FindProperty(spriteField), 82f, false, false, options);
  827. }
  828. /// <summary>
  829. /// Draw a sprite selection field.
  830. /// </summary>
  831. static public void DrawSpriteField (string label, SerializedObject ob, SerializedProperty atlas, SerializedProperty sprite, params GUILayoutOption[] options)
  832. {
  833. DrawSpriteField(label, ob, atlas, sprite, 72f, false, false, options);
  834. }
  835. /// <summary>
  836. /// Draw a sprite selection field.
  837. /// </summary>
  838. static public void DrawSpriteField (string label, SerializedObject ob, SerializedProperty atlas, SerializedProperty sprite, bool removable, params GUILayoutOption[] options)
  839. {
  840. DrawSpriteField(label, ob, atlas, sprite, 72f, false, removable, options);
  841. }
  842. /// <summary>
  843. /// Draw a sprite selection field.
  844. /// </summary>
  845. static public void DrawSpriteField (string label, SerializedObject ob, SerializedProperty atlas, SerializedProperty sprite, float width, bool padded, bool removable, params GUILayoutOption[] options)
  846. {
  847. if (atlas != null && atlas.objectReferenceValue != null)
  848. {
  849. GUILayout.BeginHorizontal();
  850. GUILayout.Label(label, GUILayout.Width(width));
  851. if (sprite == null)
  852. {
  853. GUILayout.Label("Invalid field name");
  854. }
  855. else
  856. {
  857. string spriteName = sprite.hasMultipleDifferentValues ? "-" : sprite.stringValue;
  858. GUILayout.BeginHorizontal();
  859. EditorGUI.BeginDisabledGroup(atlas.hasMultipleDifferentValues);
  860. {
  861. if (GUILayout.Button(spriteName, "MiniPullDown", options))
  862. SpriteSelector.Show(ob, sprite, atlas.objectReferenceValue as UIAtlas);
  863. }
  864. EditorGUI.EndDisabledGroup();
  865. EditorGUI.BeginDisabledGroup(!removable);
  866. if (GUILayout.Button("", "ToggleMixed", GUILayout.Width(20f))) sprite.stringValue = "";
  867. EditorGUI.EndDisabledGroup();
  868. if (padded) GUILayout.Space(12f);
  869. else GUILayout.Space(-6f);
  870. GUILayout.EndHorizontal();
  871. }
  872. GUILayout.EndHorizontal();
  873. }
  874. }
  875. /// <summary>
  876. /// Convenience function that displays a list of sprites and returns the selected value.
  877. /// </summary>
  878. static public void DrawAdvancedSpriteField (UIAtlas atlas, string spriteName, SpriteSelector.Callback callback, bool editable,
  879. params GUILayoutOption[] options)
  880. {
  881. if (atlas == null) return;
  882. // Give the user a warning if there are no sprites in the atlas
  883. if (atlas.spriteList.Count == 0)
  884. {
  885. EditorGUILayout.HelpBox("No sprites found", MessageType.Warning);
  886. return;
  887. }
  888. // Sprite selection drop-down list
  889. GUILayout.BeginHorizontal();
  890. {
  891. if (NGUIEditorTools.DrawPrefixButton("Sprite"))
  892. {
  893. NGUISettings.atlas = atlas;
  894. NGUISettings.selectedSprite = spriteName;
  895. SpriteSelector.Show(callback);
  896. }
  897. if (editable)
  898. {
  899. if (!string.Equals(spriteName, mLastSprite))
  900. {
  901. mLastSprite = spriteName;
  902. mEditedName = null;
  903. }
  904. string newName = GUILayout.TextField(string.IsNullOrEmpty(mEditedName) ? spriteName : mEditedName);
  905. if (newName != spriteName)
  906. {
  907. mEditedName = newName;
  908. if (GUILayout.Button("Rename", GUILayout.Width(60f)))
  909. {
  910. UISpriteData sprite = atlas.GetSprite(spriteName);
  911. if (sprite != null)
  912. {
  913. NGUIEditorTools.RegisterUndo("Edit Sprite Name", atlas);
  914. sprite.name = newName;
  915. List<UISprite> sprites = FindAll<UISprite>();
  916. for (int i = 0; i < sprites.Count; ++i)
  917. {
  918. UISprite sp = sprites[i];
  919. if (sp.atlas == atlas && sp.spriteName == spriteName)
  920. {
  921. NGUIEditorTools.RegisterUndo("Edit Sprite Name", sp);
  922. sp.spriteName = newName;
  923. }
  924. }
  925. mLastSprite = newName;
  926. spriteName = newName;
  927. mEditedName = null;
  928. NGUISettings.atlas = atlas;
  929. NGUISettings.selectedSprite = spriteName;
  930. }
  931. }
  932. }
  933. }
  934. else
  935. {
  936. GUILayout.BeginHorizontal();
  937. GUILayout.Label(spriteName, "HelpBox", GUILayout.Height(18f));
  938. NGUIEditorTools.DrawPadding();
  939. GUILayout.EndHorizontal();
  940. if (GUILayout.Button("Edit", GUILayout.Width(40f)))
  941. {
  942. NGUISettings.atlas = atlas;
  943. NGUISettings.selectedSprite = spriteName;
  944. Select(atlas.gameObject);
  945. }
  946. }
  947. }
  948. GUILayout.EndHorizontal();
  949. }
  950. /// <summary>
  951. /// Repaints all inspector windows related to sprite drawing.
  952. /// </summary>
  953. static public void RepaintSprites ()
  954. {
  955. if (UIAtlasInspector.instance != null)
  956. UIAtlasInspector.instance.Repaint();
  957. if (UIAtlasMaker.instance != null)
  958. UIAtlasMaker.instance.Repaint();
  959. if (SpriteSelector.instance != null)
  960. SpriteSelector.instance.Repaint();
  961. }
  962. /// <summary>
  963. /// Select the specified sprite within the currently selected atlas.
  964. /// </summary>
  965. static public void SelectSprite (string spriteName)
  966. {
  967. if (NGUISettings.atlas != null)
  968. {
  969. NGUISettings.selectedSprite = spriteName;
  970. NGUIEditorTools.Select(NGUISettings.atlas.gameObject);
  971. RepaintSprites();
  972. }
  973. }
  974. /// <summary>
  975. /// Select the specified atlas and sprite.
  976. /// </summary>
  977. static public void SelectSprite (UIAtlas atlas, string spriteName)
  978. {
  979. if (atlas != null)
  980. {
  981. NGUISettings.atlas = atlas;
  982. NGUISettings.selectedSprite = spriteName;
  983. NGUIEditorTools.Select(atlas.gameObject);
  984. RepaintSprites();
  985. }
  986. }
  987. /// <summary>
  988. /// Select the specified game object and remember what was selected before.
  989. /// </summary>
  990. static public void Select (GameObject go)
  991. {
  992. mPrevious = Selection.activeGameObject;
  993. Selection.activeGameObject = go;
  994. }
  995. /// <summary>
  996. /// Select the previous game object.
  997. /// </summary>
  998. static public void SelectPrevious ()
  999. {
  1000. if (mPrevious != null)
  1001. {
  1002. Selection.activeGameObject = mPrevious;
  1003. mPrevious = null;
  1004. }
  1005. }
  1006. /// <summary>
  1007. /// Previously selected game object.
  1008. /// </summary>
  1009. static public GameObject previousSelection { get { return mPrevious; } }
  1010. /// <summary>
  1011. /// Helper function that checks to see if the scale is uniform.
  1012. /// </summary>
  1013. static public bool IsUniform (Vector3 scale)
  1014. {
  1015. return Mathf.Approximately(scale.x, scale.y) && Mathf.Approximately(scale.x, scale.z);
  1016. }
  1017. /// <summary>
  1018. /// Check to see if the specified game object has a uniform scale.
  1019. /// </summary>
  1020. static public bool IsUniform (GameObject go)
  1021. {
  1022. if (go == null) return true;
  1023. if (go.GetComponent<UIWidget>() != null)
  1024. {
  1025. Transform parent = go.transform.parent;
  1026. return parent == null || IsUniform(parent.gameObject);
  1027. }
  1028. return IsUniform(go.transform.lossyScale);
  1029. }
  1030. /// <summary>
  1031. /// Fix uniform scaling of the specified object.
  1032. /// </summary>
  1033. static public void FixUniform (GameObject go)
  1034. {
  1035. Transform t = go.transform;
  1036. while (t != null && t.gameObject.GetComponent<UIRoot>() == null)
  1037. {
  1038. if (!NGUIEditorTools.IsUniform(t.localScale))
  1039. {
  1040. NGUIEditorTools.RegisterUndo("Uniform scaling fix", t);
  1041. t.localScale = Vector3.one;
  1042. EditorUtility.SetDirty(t);
  1043. }
  1044. t = t.parent;
  1045. }
  1046. }
  1047. /// <summary>
  1048. /// Draw a distinctly different looking header label
  1049. /// </summary>
  1050. static public bool DrawMinimalisticHeader (string text) { return DrawHeader(text, text, false, true); }
  1051. /// <summary>
  1052. /// Draw a distinctly different looking header label
  1053. /// </summary>
  1054. static public bool DrawHeader (string text) { return DrawHeader(text, text, false, NGUISettings.minimalisticLook); }
  1055. /// <summary>
  1056. /// Draw a distinctly different looking header label
  1057. /// </summary>
  1058. static public bool DrawHeader (string text, string key) { return DrawHeader(text, key, false, NGUISettings.minimalisticLook); }
  1059. /// <summary>
  1060. /// Draw a distinctly different looking header label
  1061. /// </summary>
  1062. static public bool DrawHeader (string text, bool detailed) { return DrawHeader(text, text, detailed, !detailed); }
  1063. /// <summary>
  1064. /// Draw a distinctly different looking header label
  1065. /// </summary>
  1066. static public bool DrawHeader (string text, string key, bool forceOn, bool minimalistic)
  1067. {
  1068. bool state = EditorPrefs.GetBool(key, true);
  1069. if (!minimalistic) GUILayout.Space(3f);
  1070. if (!forceOn && !state) GUI.backgroundColor = new Color(0.8f, 0.8f, 0.8f);
  1071. GUILayout.BeginHorizontal();
  1072. GUI.changed = false;
  1073. if (minimalistic)
  1074. {
  1075. if (state) text = "\u25BC" + (char)0x200a + text;
  1076. else text = "\u25BA" + (char)0x200a + text;
  1077. GUILayout.BeginHorizontal();
  1078. GUI.contentColor = EditorGUIUtility.isProSkin ? new Color(1f, 1f, 1f, 0.7f) : new Color(0f, 0f, 0f, 0.7f);
  1079. if (!GUILayout.Toggle(true, text, "PreToolbar2", GUILayout.MinWidth(20f))) state = !state;
  1080. GUI.contentColor = Color.white;
  1081. GUILayout.EndHorizontal();
  1082. }
  1083. else
  1084. {
  1085. text = "<b><size=11>" + text + "</size></b>";
  1086. if (state) text = "\u25BC " + text;
  1087. else text = "\u25BA " + text;
  1088. if (!GUILayout.Toggle(true, text, "dragtab", GUILayout.MinWidth(20f))) state = !state;
  1089. }
  1090. if (GUI.changed) EditorPrefs.SetBool(key, state);
  1091. if (!minimalistic) GUILayout.Space(2f);
  1092. GUILayout.EndHorizontal();
  1093. GUI.backgroundColor = Color.white;
  1094. if (!forceOn && !state) GUILayout.Space(3f);
  1095. return state;
  1096. }
  1097. /// <summary>
  1098. /// Begin drawing the content area.
  1099. /// </summary>
  1100. static public void BeginContents () { BeginContents(NGUISettings.minimalisticLook); }
  1101. static bool mEndHorizontal = false;
  1102. /// <summary>
  1103. /// Begin drawing the content area.
  1104. /// </summary>
  1105. static public void BeginContents (bool minimalistic)
  1106. {
  1107. if (!minimalistic)
  1108. {
  1109. mEndHorizontal = true;
  1110. GUILayout.BeginHorizontal();
  1111. EditorGUILayout.BeginHorizontal("AS TextArea", GUILayout.MinHeight(10f));
  1112. }
  1113. else
  1114. {
  1115. mEndHorizontal = false;
  1116. EditorGUILayout.BeginHorizontal(GUILayout.MinHeight(10f));
  1117. GUILayout.Space(10f);
  1118. }
  1119. GUILayout.BeginVertical();
  1120. GUILayout.Space(2f);
  1121. }
  1122. /// <summary>
  1123. /// End drawing the content area.
  1124. /// </summary>
  1125. static public void EndContents ()
  1126. {
  1127. GUILayout.Space(3f);
  1128. GUILayout.EndVertical();
  1129. EditorGUILayout.EndHorizontal();
  1130. if (mEndHorizontal)
  1131. {
  1132. GUILayout.Space(3f);
  1133. GUILayout.EndHorizontal();
  1134. }
  1135. GUILayout.Space(3f);
  1136. }
  1137. /// <summary>
  1138. /// Draw a list of fields for the specified list of delegates.
  1139. /// </summary>
  1140. static public void DrawEvents (string text, Object undoObject, List<EventDelegate> list)
  1141. {
  1142. DrawEvents(text, undoObject, list, null, null, false);
  1143. }
  1144. /// <summary>
  1145. /// Draw a list of fields for the specified list of delegates.
  1146. /// </summary>
  1147. static public void DrawEvents (string text, Object undoObject, List<EventDelegate> list, bool minimalistic)
  1148. {
  1149. DrawEvents(text, undoObject, list, null, null, minimalistic);
  1150. }
  1151. /// <summary>
  1152. /// Draw a list of fields for the specified list of delegates.
  1153. /// </summary>
  1154. static public void DrawEvents (string text, Object undoObject, List<EventDelegate> list, string noTarget, string notValid, bool minimalistic)
  1155. {
  1156. if (!NGUIEditorTools.DrawHeader(text, text, false, minimalistic)) return;
  1157. if (!minimalistic)
  1158. {
  1159. NGUIEditorTools.BeginContents(minimalistic);
  1160. GUILayout.BeginHorizontal();
  1161. GUILayout.BeginVertical();
  1162. EventDelegateEditor.Field(undoObject, list, notValid, notValid, minimalistic);
  1163. GUILayout.EndVertical();
  1164. GUILayout.EndHorizontal();
  1165. NGUIEditorTools.EndContents();
  1166. }
  1167. else EventDelegateEditor.Field(undoObject, list, notValid, notValid, minimalistic);
  1168. }
  1169. /// <summary>
  1170. /// Helper function that draws a serialized property.
  1171. /// </summary>
  1172. static public SerializedProperty DrawProperty (SerializedObject serializedObject, string property, params GUILayoutOption[] options)
  1173. {
  1174. return DrawProperty(null, serializedObject, property, false, options);
  1175. }
  1176. /// <summary>
  1177. /// Helper function that draws a serialized property.
  1178. /// </summary>
  1179. static public SerializedProperty DrawProperty (string label, SerializedObject serializedObject, string property, params GUILayoutOption[] options)
  1180. {
  1181. return DrawProperty(label, serializedObject, property, false, options);
  1182. }
  1183. /// <summary>
  1184. /// Helper function that draws a serialized property.
  1185. /// </summary>
  1186. static public SerializedProperty DrawPaddedProperty (SerializedObject serializedObject, string property, params GUILayoutOption[] options)
  1187. {
  1188. return DrawProperty(null, serializedObject, property, true, options);
  1189. }
  1190. /// <summary>
  1191. /// Helper function that draws a serialized property.
  1192. /// </summary>
  1193. static public SerializedProperty DrawPaddedProperty (string label, SerializedObject serializedObject, string property, params GUILayoutOption[] options)
  1194. {
  1195. return DrawProperty(label, serializedObject, property, true, options);
  1196. }
  1197. /// <summary>
  1198. /// Helper function that draws a serialized property.
  1199. /// </summary>
  1200. static public SerializedProperty DrawProperty (string label, SerializedObject serializedObject, string property, bool padding, params GUILayoutOption[] options)
  1201. {
  1202. SerializedProperty sp = serializedObject.FindProperty(property);
  1203. if (sp != null)
  1204. {
  1205. if (NGUISettings.minimalisticLook) padding = false;
  1206. if (padding) EditorGUILayout.BeginHorizontal();
  1207. if (label != null) EditorGUILayout.PropertyField(sp, new GUIContent(label), options);
  1208. else EditorGUILayout.PropertyField(sp, options);
  1209. if (padding)
  1210. {
  1211. NGUIEditorTools.DrawPadding();
  1212. EditorGUILayout.EndHorizontal();
  1213. }
  1214. }
  1215. return sp;
  1216. }
  1217. /// <summary>
  1218. /// Helper function that draws a serialized property.
  1219. /// </summary>
  1220. static public void DrawProperty (string label, SerializedProperty sp, params GUILayoutOption[] options)
  1221. {
  1222. DrawProperty(label, sp, true, options);
  1223. }
  1224. /// <summary>
  1225. /// Helper function that draws a serialized property.
  1226. /// </summary>
  1227. static public void DrawProperty (string label, SerializedProperty sp, bool padding, params GUILayoutOption[] options)
  1228. {
  1229. if (sp != null)
  1230. {
  1231. if (padding) EditorGUILayout.BeginHorizontal();
  1232. if (label != null) EditorGUILayout.PropertyField(sp, new GUIContent(label), options);
  1233. else EditorGUILayout.PropertyField(sp, options);
  1234. if (padding)
  1235. {
  1236. NGUIEditorTools.DrawPadding();
  1237. EditorGUILayout.EndHorizontal();
  1238. }
  1239. }
  1240. }
  1241. /// <summary>
  1242. /// Helper function that draws a compact Vector4.
  1243. /// </summary>
  1244. static public void DrawBorderProperty (string name, SerializedObject serializedObject, string field)
  1245. {
  1246. if (serializedObject.FindProperty(field) != null)
  1247. {
  1248. GUILayout.BeginHorizontal();
  1249. {
  1250. GUILayout.Label(name, GUILayout.Width(75f));
  1251. NGUIEditorTools.SetLabelWidth(50f);
  1252. GUILayout.BeginVertical();
  1253. NGUIEditorTools.DrawProperty("Left", serializedObject, field + ".x", GUILayout.MinWidth(80f));
  1254. NGUIEditorTools.DrawProperty("Bottom", serializedObject, field + ".y", GUILayout.MinWidth(80f));
  1255. GUILayout.EndVertical();
  1256. GUILayout.BeginVertical();
  1257. NGUIEditorTools.DrawProperty("Right", serializedObject, field + ".z", GUILayout.MinWidth(80f));
  1258. NGUIEditorTools.DrawProperty("Top", serializedObject, field + ".w", GUILayout.MinWidth(80f));
  1259. GUILayout.EndVertical();
  1260. NGUIEditorTools.SetLabelWidth(80f);
  1261. }
  1262. GUILayout.EndHorizontal();
  1263. }
  1264. }
  1265. /// <summary>
  1266. /// Helper function that draws a compact Rect.
  1267. /// </summary>
  1268. static public void DrawRectProperty (string name, SerializedObject serializedObject, string field)
  1269. {
  1270. DrawRectProperty(name, serializedObject, field, 56f, 18f);
  1271. }
  1272. /// <summary>
  1273. /// Helper function that draws a compact Rect.
  1274. /// </summary>
  1275. static public void DrawRectProperty (string name, SerializedObject serializedObject, string field, float labelWidth, float spacing)
  1276. {
  1277. if (serializedObject.FindProperty(field) != null)
  1278. {
  1279. GUILayout.BeginHorizontal();
  1280. {
  1281. GUILayout.Label(name, GUILayout.Width(labelWidth));
  1282. NGUIEditorTools.SetLabelWidth(20f);
  1283. GUILayout.BeginVertical();
  1284. NGUIEditorTools.DrawProperty("X", serializedObject, field + ".x", GUILayout.MinWidth(50f));
  1285. NGUIEditorTools.DrawProperty("Y", serializedObject, field + ".y", GUILayout.MinWidth(50f));
  1286. GUILayout.EndVertical();
  1287. NGUIEditorTools.SetLabelWidth(50f);
  1288. GUILayout.BeginVertical();
  1289. NGUIEditorTools.DrawProperty("Width", serializedObject, field + ".width", GUILayout.MinWidth(80f));
  1290. NGUIEditorTools.DrawProperty("Height", serializedObject, field + ".height", GUILayout.MinWidth(80f));
  1291. GUILayout.EndVertical();
  1292. NGUIEditorTools.SetLabelWidth(80f);
  1293. if (spacing != 0f) GUILayout.Space(spacing);
  1294. }
  1295. GUILayout.EndHorizontal();
  1296. }
  1297. }
  1298. /// <summary>
  1299. /// Determine the distance from the mouse position to the world rectangle specified by the 4 points.
  1300. /// </summary>
  1301. static public float SceneViewDistanceToRectangle (Vector3[] worldPoints, Vector2 mousePos)
  1302. {
  1303. Vector2[] screenPoints = new Vector2[4];
  1304. for (int i = 0; i < 4; ++i)
  1305. screenPoints[i] = HandleUtility.WorldToGUIPoint(worldPoints[i]);
  1306. return NGUIMath.DistanceToRectangle(screenPoints, mousePos);
  1307. }
  1308. /// <summary>
  1309. /// Raycast into the specified panel, returning a list of widgets.
  1310. /// Just like NGUIMath.Raycast, but doesn't rely on having a camera.
  1311. /// </summary>
  1312. static public BetterList<UIWidget> SceneViewRaycast (Vector2 mousePos)
  1313. {
  1314. BetterList<UIWidget> list = new BetterList<UIWidget>();
  1315. for (int i = 0; i < UIPanel.list.size; ++i)
  1316. {
  1317. UIPanel p = UIPanel.list.buffer[i];
  1318. for (int b = 0; b < p.widgets.size; ++b)
  1319. {
  1320. UIWidget w = p.widgets.buffer[b];
  1321. Vector3[] corners = w.worldCorners;
  1322. if (SceneViewDistanceToRectangle(corners, mousePos) == 0f)
  1323. list.Add(w);
  1324. }
  1325. }
  1326. list.Sort(UIWidget.FullCompareFunc);
  1327. return list;
  1328. }
  1329. /// <summary>
  1330. /// Select the topmost widget underneath the specified screen coordinate.
  1331. /// </summary>
  1332. static public bool SelectWidget (Vector2 pos) { return SelectWidget(null, pos, true); }
  1333. /// <summary>
  1334. /// Select the next widget in line.
  1335. /// </summary>
  1336. static public bool SelectWidget (GameObject start, Vector2 pos, bool inFront)
  1337. {
  1338. GameObject go = null;
  1339. BetterList<UIWidget> widgets = SceneViewRaycast(pos);
  1340. if (widgets == null || widgets.size == 0) return false;
  1341. bool found = false;
  1342. if (!inFront)
  1343. {
  1344. if (start != null)
  1345. {
  1346. for (int i = 0; i < widgets.size; ++i)
  1347. {
  1348. UIWidget w = widgets[i];
  1349. if (w.cachedGameObject == start)
  1350. {
  1351. found = true;
  1352. break;
  1353. }
  1354. go = w.cachedGameObject;
  1355. }
  1356. }
  1357. if (!found) go = widgets[0].cachedGameObject;
  1358. }
  1359. else
  1360. {
  1361. if (start != null)
  1362. {
  1363. for (int i = widgets.size; i > 0; )
  1364. {
  1365. UIWidget w = widgets[--i];
  1366. if (w.cachedGameObject == start)
  1367. {
  1368. found = true;
  1369. break;
  1370. }
  1371. go = w.cachedGameObject;
  1372. }
  1373. }
  1374. if (!found) go = widgets[widgets.size - 1].cachedGameObject;
  1375. }
  1376. if (go != null && go != start)
  1377. {
  1378. Selection.activeGameObject = go;
  1379. return true;
  1380. }
  1381. return false;
  1382. }
  1383. /// <summary>
  1384. /// Unity 4.3 changed the way LookLikeControls works.
  1385. /// </summary>
  1386. static public void SetLabelWidth (float width)
  1387. {
  1388. #if UNITY_3_5 || UNITY_4_0 || UNITY_4_1 || UNITY_4_2
  1389. EditorGUIUtility.LookLikeControls(width);
  1390. #else
  1391. EditorGUIUtility.labelWidth = width;
  1392. #endif
  1393. }
  1394. /// <summary>
  1395. /// Create an undo point for the specified objects.
  1396. /// </summary>
  1397. static public void RegisterUndo (string name, params Object[] objects)
  1398. {
  1399. if (objects != null && objects.Length > 0)
  1400. {
  1401. #if UNITY_3_5 || UNITY_4_0 || UNITY_4_1 || UNITY_4_2
  1402. UnityEditor.Undo.RegisterUndo(objects, name);
  1403. #else
  1404. UnityEditor.Undo.RecordObjects(objects, name);
  1405. #endif
  1406. foreach (Object obj in objects)
  1407. {
  1408. if (obj == null) continue;
  1409. EditorUtility.SetDirty(obj);
  1410. }
  1411. }
  1412. }
  1413. /// <summary>
  1414. /// Unity 4.5+ makes it possible to hide the move tool.
  1415. /// </summary>
  1416. static public void HideMoveTool (bool hide)
  1417. {
  1418. #if !UNITY_3_5 && !UNITY_4_0 && !UNITY_4_1 && !UNITY_4_2 && !UNITY_4_3
  1419. UnityEditor.Tools.hidden = hide && (UnityEditor.Tools.current == UnityEditor.Tool.Move) && UIWidget.showHandlesWithMoveTool;
  1420. #endif
  1421. }
  1422. /// <summary>
  1423. /// Gets the internal class ID of the specified type.
  1424. /// </summary>
  1425. static public int GetClassID (System.Type type)
  1426. {
  1427. GameObject go = EditorUtility.CreateGameObjectWithHideFlags("Temp", HideFlags.HideAndDontSave);
  1428. Component uiSprite = go.AddComponent(type);
  1429. SerializedObject ob = new SerializedObject(uiSprite);
  1430. int classID = ob.FindProperty("m_Script").objectReferenceInstanceIDValue;
  1431. NGUITools.DestroyImmediate(go);
  1432. return classID;
  1433. }
  1434. /// <summary>
  1435. /// Gets the internal class ID of the specified type.
  1436. /// </summary>
  1437. static public int GetClassID<T> () where T : MonoBehaviour { return GetClassID(typeof(T)); }
  1438. /// <summary>
  1439. /// Convenience function that replaces the specified MonoBehaviour with one of specified type.
  1440. /// </summary>
  1441. static public SerializedObject ReplaceClass (MonoBehaviour mb, System.Type type)
  1442. {
  1443. int id = GetClassID(type);
  1444. SerializedObject ob = new SerializedObject(mb);
  1445. ob.Update();
  1446. ob.FindProperty("m_Script").objectReferenceInstanceIDValue = id;
  1447. ob.ApplyModifiedProperties();
  1448. ob.Update();
  1449. return ob;
  1450. }
  1451. /// <summary>
  1452. /// Convenience function that replaces the specified MonoBehaviour with one of specified class ID.
  1453. /// </summary>
  1454. static public SerializedObject ReplaceClass (MonoBehaviour mb, int classID)
  1455. {
  1456. SerializedObject ob = new SerializedObject(mb);
  1457. ob.Update();
  1458. ob.FindProperty("m_Script").objectReferenceInstanceIDValue = classID;
  1459. ob.ApplyModifiedProperties();
  1460. ob.Update();
  1461. return ob;
  1462. }
  1463. /// <summary>
  1464. /// Convenience function that replaces the specified MonoBehaviour with one of specified class ID.
  1465. /// </summary>
  1466. static public void ReplaceClass (SerializedObject ob, int classID)
  1467. {
  1468. ob.FindProperty("m_Script").objectReferenceInstanceIDValue = classID;
  1469. ob.ApplyModifiedProperties();
  1470. ob.Update();
  1471. }
  1472. /// <summary>
  1473. /// Convenience function that replaces the specified MonoBehaviour with one of specified class ID.
  1474. /// </summary>
  1475. static public void ReplaceClass (SerializedObject ob, System.Type type)
  1476. {
  1477. ob.FindProperty("m_Script").objectReferenceInstanceIDValue = GetClassID(type);
  1478. ob.Ap

Large files files are truncated, but you can click here to view the full file