PRHelper.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*
  2. http://www.cgsoso.com/forum-211-1.html
  3. CG搜搜 Unity3d 每日Unity3d插件免费更新 更有VIP资源!
  4. CGSOSO 主打游戏开发,影视设计等CG资源素材。
  5. 插件如若商用,请务必官网购买!
  6. daily assets update for try.
  7. U should buy the asset from home store if u use it in your project!
  8. */
  9. using UnityEngine;
  10. using System.Collections;
  11. public class PRHelper : MonoBehaviour
  12. {
  13. static PRHelper()
  14. {
  15. s_Styles = new Styles();
  16. }
  17. public static bool GroupHeader(string text, bool isExpanded)
  18. {
  19. Rect rect = GUILayoutUtility.GetRect(20f, 18f, s_Styles.header);
  20. s_Styles.Backup();
  21. s_Styles.Apply();
  22. if (Event.current.type == EventType.Repaint)
  23. s_Styles.header.Draw(rect, text, isExpanded, isExpanded, isExpanded, isExpanded);
  24. Event e = Event.current;
  25. if (e.type == EventType.MouseDown)
  26. {
  27. if (rect.Contains(e.mousePosition))
  28. {
  29. isExpanded = !isExpanded;
  30. e.Use();
  31. }
  32. }
  33. s_Styles.Revert();
  34. return isExpanded;
  35. }
  36. private static Styles s_Styles;
  37. private class Styles
  38. {
  39. public GUIStyle header = "ShurikenModuleTitle";
  40. internal Styles()
  41. {
  42. header.font = (new GUIStyle("Label")).font;
  43. }
  44. RectOffset m_Border;
  45. float m_FixedHeight;
  46. Vector2 m_ContentOffset;
  47. TextAnchor m_TextAlign;
  48. FontStyle m_TextStyle;
  49. int m_FontSize;
  50. RectOffset m_Margin;
  51. public void Backup()
  52. {
  53. m_Border = s_Styles.header.border;
  54. m_FixedHeight = s_Styles.header.fixedHeight;
  55. m_ContentOffset = s_Styles.header.contentOffset;
  56. m_TextAlign = s_Styles.header.alignment;
  57. m_TextStyle = s_Styles.header.fontStyle;
  58. m_FontSize = s_Styles.header.fontSize;
  59. m_Margin = s_Styles.header.margin;
  60. }
  61. public void Apply()
  62. {
  63. s_Styles.header.border = new RectOffset(7, 7, 4, 4);
  64. s_Styles.header.fixedHeight = 20;
  65. s_Styles.header.contentOffset = new Vector2(8f, -2f);
  66. s_Styles.header.alignment = TextAnchor.MiddleLeft;
  67. s_Styles.header.fontStyle = FontStyle.Normal;
  68. s_Styles.header.fontSize = 12;
  69. s_Styles.header.margin = new RectOffset(0, 0, 0, 5);
  70. }
  71. public void Revert()
  72. {
  73. s_Styles.header.border = m_Border;
  74. s_Styles.header.fixedHeight = m_FixedHeight;
  75. s_Styles.header.contentOffset = m_ContentOffset;
  76. s_Styles.header.alignment = m_TextAlign;
  77. s_Styles.header.fontStyle = m_TextStyle;
  78. s_Styles.header.fontSize = m_FontSize;
  79. s_Styles.header.margin = m_Margin;
  80. }
  81. }
  82. }