UIConfig.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. namespace FairyGUI
  5. {
  6. /// <summary>
  7. /// Global configs. These options should be set before any UI construction.
  8. /// </summary>
  9. [AddComponentMenu("FairyGUI/UI Config")]
  10. public class UIConfig : MonoBehaviour
  11. {
  12. /// <summary>
  13. /// Dynamic Font Support.
  14. /// 4.x: Put the xxx.ttf into /Resources or /Resources/Fonts, and set defaultFont="xxx".
  15. /// 5.x: set defaultFont to system font name(or names joint with comma). e.g. defaultFont="Microsoft YaHei, SimHei"
  16. /// </summary>
  17. public static string defaultFont = "";
  18. [Obsolete("No use anymore")]
  19. public static bool renderingTextBrighterOnDesktop = true;
  20. /// <summary>
  21. /// Resource using in Window.ShowModalWait for locking the window.
  22. /// </summary>
  23. public static string windowModalWaiting;
  24. /// <summary>
  25. /// Resource using in GRoot.ShowModalWait for locking the screen.
  26. /// </summary>
  27. public static string globalModalWaiting;
  28. /// <summary>
  29. /// When a modal window is in front, the background becomes dark.
  30. /// </summary>
  31. public static Color modalLayerColor = new Color(0f, 0f, 0f, 0.4f);
  32. /// <summary>
  33. /// Default button click sound.
  34. /// </summary>
  35. public static NAudioClip buttonSound;
  36. /// <summary>
  37. /// Default button click sound volume.
  38. /// </summary>
  39. public static float buttonSoundVolumeScale = 1f;
  40. /// <summary>
  41. /// Resource url of horizontal scrollbar
  42. /// </summary>
  43. public static string horizontalScrollBar;
  44. /// <summary>
  45. /// Resource url of vertical scrollbar
  46. /// </summary>
  47. public static string verticalScrollBar;
  48. /// <summary>
  49. /// Scrolling step in pixels
  50. /// 当调用ScrollPane.scrollUp/Down/Left/Right时,或者点击滚动条的上下箭头时,滑动的距离。
  51. /// 鼠标滚轮触发一次滚动的距离设定为defaultScrollStep*2
  52. /// </summary>
  53. public static float defaultScrollStep = 25;
  54. /// <summary>
  55. /// Deceleration ratio of scrollpane when its in touch dragging.
  56. /// 当手指拖动并释放滚动区域后,内容会滑动一定距离后停下,这个速率就是减速的速率。
  57. /// 越接近1,减速越慢,意味着滑动的时间和距离更长。
  58. /// 这个是全局设置,也可以通过ScrollPane.decelerationRate进行个性设置。
  59. /// </summary>
  60. public static float defaultScrollDecelerationRate = 0.967f;
  61. /// <summary>
  62. /// Scrollbar display mode. Recommended 'Auto' for mobile and 'Visible' for web.
  63. /// </summary>
  64. public static ScrollBarDisplayType defaultScrollBarDisplay = ScrollBarDisplayType.Default;
  65. /// <summary>
  66. /// Allow dragging anywhere in container to scroll.
  67. /// </summary>
  68. public static bool defaultScrollTouchEffect = true;
  69. /// <summary>
  70. /// The "rebound" effect in the scolling container.
  71. /// </summary>
  72. public static bool defaultScrollBounceEffect = true;
  73. /// <summary>
  74. /// When the scrolling container is set to "snap to the item", the rolling distance threshold of which item is close to is determined.
  75. /// </summary>
  76. public static float defaultScrollSnappingThreshold = 0.5f;
  77. /// <summary>
  78. /// When the scrolling container is set to "page mode", it determines the scrolling distance threshold to which page to turn.
  79. /// </summary>
  80. public static float defaultScrollPagingThreshold = 0.3f;
  81. /// <summary>
  82. /// Resources url of PopupMenu.
  83. /// </summary>
  84. public static string popupMenu;
  85. /// <summary>
  86. /// Resource url of menu seperator.
  87. /// </summary>
  88. public static string popupMenu_seperator;
  89. /// <summary>
  90. /// In case of failure of loading content for GLoader, use this sign to indicate an error.
  91. /// </summary>
  92. public static string loaderErrorSign;
  93. /// <summary>
  94. /// Resource url of tooltips.
  95. /// </summary>
  96. public static string tooltipsWin;
  97. /// <summary>
  98. /// The number of visible items in ComboBox.
  99. /// </summary>
  100. public static int defaultComboBoxVisibleItemCount = 10;
  101. /// <summary>
  102. /// Pixel offsets of finger to trigger scrolling
  103. /// </summary>
  104. public static int touchScrollSensitivity = 20;
  105. /// <summary>
  106. /// Pixel offsets of finger to trigger dragging
  107. /// </summary>
  108. public static int touchDragSensitivity = 10;
  109. /// <summary>
  110. /// Pixel offsets of mouse pointer to trigger dragging.
  111. /// </summary>
  112. public static int clickDragSensitivity = 2;
  113. /// <summary>
  114. /// Allow softness on top or left side for scrollpane.
  115. /// </summary>
  116. public static bool allowSoftnessOnTopOrLeftSide = true;
  117. /// <summary>
  118. /// When click the window, brings to front automatically.
  119. /// </summary>
  120. public static bool bringWindowToFrontOnClick = true;
  121. /// <summary>
  122. ///
  123. /// </summary>
  124. public static float inputCaretSize = 1;
  125. /// <summary>
  126. ///
  127. /// </summary>
  128. public static Color inputHighlightColor = new Color32(255, 223, 141, 128);
  129. /// <summary>
  130. ///
  131. /// </summary>
  132. public static float frameTimeForAsyncUIConstruction = 0.002f;
  133. /// <summary>
  134. /// if RenderTexture using in painting mode has depth support.
  135. /// </summary>
  136. public static bool depthSupportForPaintingMode = true;
  137. /// <summary>
  138. /// Indicates whether to draw extra 4 or 8 times to achieve stroke effect for textfield.
  139. /// If it is true, that is the 8 times, otherwise it is the 4 times.
  140. /// </summary>
  141. public static bool enhancedTextOutlineEffect = false;
  142. [Obsolete("No use anymore.")]
  143. public static VertAlignType richTextRowVerticalAlign = VertAlignType.Bottom;
  144. /// <summary>
  145. /// Suggest to enable it on low dpi (e.g. 96dpi) screens.
  146. /// </summary>
  147. public static bool makePixelPerfect = false;
  148. public enum ConfigKey
  149. {
  150. DefaultFont,
  151. ButtonSound,
  152. ButtonSoundVolumeScale,
  153. HorizontalScrollBar,
  154. VerticalScrollBar,
  155. DefaultScrollStep,
  156. DefaultScrollBarDisplay,
  157. DefaultScrollTouchEffect,
  158. DefaultScrollBounceEffect,
  159. TouchScrollSensitivity,
  160. WindowModalWaiting,
  161. GlobalModalWaiting,
  162. PopupMenu,
  163. PopupMenu_seperator,
  164. LoaderErrorSign,
  165. TooltipsWin,
  166. DefaultComboBoxVisibleItemCount,
  167. TouchDragSensitivity,
  168. ClickDragSensitivity,
  169. ModalLayerColor,
  170. RenderingTextBrighterOnDesktop,
  171. AllowSoftnessOnTopOrLeftSide,
  172. InputCaretSize,
  173. InputHighlightColor,
  174. EnhancedTextOutlineEffect,
  175. DepthSupportForPaintingMode,
  176. RichTextRowVerticalAlign,
  177. Branch,
  178. PleaseSelect = 100
  179. }
  180. [Serializable]
  181. public class ConfigValue
  182. {
  183. public bool valid;
  184. public string s;
  185. public int i;
  186. public float f;
  187. public bool b;
  188. public Color c;
  189. public void Reset()
  190. {
  191. valid = false;
  192. s = null;
  193. i = 0;
  194. f = 0;
  195. b = false;
  196. c = Color.black;
  197. }
  198. }
  199. public List<ConfigValue> Items = new List<ConfigValue>();
  200. public List<string> PreloadPackages = new List<string>();
  201. void Awake()
  202. {
  203. if (Application.isPlaying)
  204. {
  205. foreach (string packagePath in PreloadPackages)
  206. {
  207. UIPackage.AddPackage(packagePath);
  208. }
  209. Load();
  210. }
  211. }
  212. public void Load()
  213. {
  214. int cnt = Items.Count;
  215. for (int i = 0; i < cnt; i++)
  216. {
  217. ConfigValue value = Items[i];
  218. if (!value.valid)
  219. continue;
  220. switch ((UIConfig.ConfigKey)i)
  221. {
  222. case ConfigKey.ButtonSound:
  223. if (Application.isPlaying)
  224. UIConfig.buttonSound = UIPackage.GetItemAssetByURL(value.s) as NAudioClip;
  225. break;
  226. case ConfigKey.ButtonSoundVolumeScale:
  227. UIConfig.buttonSoundVolumeScale = value.f;
  228. break;
  229. case ConfigKey.ClickDragSensitivity:
  230. UIConfig.clickDragSensitivity = value.i;
  231. break;
  232. case ConfigKey.DefaultComboBoxVisibleItemCount:
  233. UIConfig.defaultComboBoxVisibleItemCount = value.i;
  234. break;
  235. case ConfigKey.DefaultFont:
  236. UIConfig.defaultFont = value.s;
  237. break;
  238. case ConfigKey.DefaultScrollBarDisplay:
  239. UIConfig.defaultScrollBarDisplay = (ScrollBarDisplayType)value.i;
  240. break;
  241. case ConfigKey.DefaultScrollBounceEffect:
  242. UIConfig.defaultScrollBounceEffect = value.b;
  243. break;
  244. case ConfigKey.DefaultScrollStep:
  245. UIConfig.defaultScrollStep = value.i;
  246. break;
  247. case ConfigKey.DefaultScrollTouchEffect:
  248. UIConfig.defaultScrollTouchEffect = value.b;
  249. break;
  250. case ConfigKey.GlobalModalWaiting:
  251. UIConfig.globalModalWaiting = value.s;
  252. break;
  253. case ConfigKey.HorizontalScrollBar:
  254. UIConfig.horizontalScrollBar = value.s;
  255. break;
  256. case ConfigKey.LoaderErrorSign:
  257. UIConfig.loaderErrorSign = value.s;
  258. break;
  259. case ConfigKey.ModalLayerColor:
  260. UIConfig.modalLayerColor = value.c;
  261. break;
  262. case ConfigKey.PopupMenu:
  263. UIConfig.popupMenu = value.s;
  264. break;
  265. case ConfigKey.PopupMenu_seperator:
  266. UIConfig.popupMenu_seperator = value.s;
  267. break;
  268. case ConfigKey.TooltipsWin:
  269. UIConfig.tooltipsWin = value.s;
  270. break;
  271. case ConfigKey.TouchDragSensitivity:
  272. UIConfig.touchDragSensitivity = value.i;
  273. break;
  274. case ConfigKey.TouchScrollSensitivity:
  275. UIConfig.touchScrollSensitivity = value.i;
  276. break;
  277. case ConfigKey.VerticalScrollBar:
  278. UIConfig.verticalScrollBar = value.s;
  279. break;
  280. case ConfigKey.WindowModalWaiting:
  281. UIConfig.windowModalWaiting = value.s;
  282. break;
  283. case ConfigKey.AllowSoftnessOnTopOrLeftSide:
  284. UIConfig.allowSoftnessOnTopOrLeftSide = value.b;
  285. break;
  286. case ConfigKey.InputCaretSize:
  287. UIConfig.inputCaretSize = value.i;
  288. break;
  289. case ConfigKey.InputHighlightColor:
  290. UIConfig.inputHighlightColor = value.c;
  291. break;
  292. case ConfigKey.DepthSupportForPaintingMode:
  293. UIConfig.depthSupportForPaintingMode = value.b;
  294. break;
  295. case ConfigKey.EnhancedTextOutlineEffect:
  296. UIConfig.enhancedTextOutlineEffect = value.b;
  297. break;
  298. case ConfigKey.Branch:
  299. UIPackage.branch = value.s;
  300. break;
  301. }
  302. }
  303. }
  304. public static void SetDefaultValue(ConfigKey key, ConfigValue value)
  305. {
  306. switch (key)
  307. {
  308. case ConfigKey.ButtonSoundVolumeScale:
  309. value.f = 1;
  310. break;
  311. case ConfigKey.ClickDragSensitivity:
  312. value.i = 2;
  313. break;
  314. case ConfigKey.DefaultComboBoxVisibleItemCount:
  315. value.i = 10;
  316. break;
  317. case ConfigKey.DefaultScrollBarDisplay:
  318. value.i = (int)ScrollBarDisplayType.Default;
  319. break;
  320. case ConfigKey.DefaultScrollBounceEffect:
  321. case ConfigKey.DefaultScrollTouchEffect:
  322. value.b = true;
  323. break;
  324. case ConfigKey.DefaultScrollStep:
  325. value.i = 25;
  326. break;
  327. case ConfigKey.ModalLayerColor:
  328. value.c = new Color(0f, 0f, 0f, 0.4f);
  329. break;
  330. case ConfigKey.RenderingTextBrighterOnDesktop:
  331. value.b = true;
  332. break;
  333. case ConfigKey.TouchDragSensitivity:
  334. value.i = 10;
  335. break;
  336. case ConfigKey.TouchScrollSensitivity:
  337. value.i = 20;
  338. break;
  339. case ConfigKey.InputCaretSize:
  340. value.i = 1;
  341. break;
  342. case ConfigKey.InputHighlightColor:
  343. value.c = new Color32(255, 223, 141, 128);
  344. break;
  345. case ConfigKey.DepthSupportForPaintingMode:
  346. value.b = false;
  347. break;
  348. case ConfigKey.Branch:
  349. value.s = "";
  350. break;
  351. }
  352. }
  353. public static void ClearResourceRefs()
  354. {
  355. UIConfig.defaultFont = "";
  356. UIConfig.buttonSound = null;
  357. UIConfig.globalModalWaiting = null;
  358. UIConfig.horizontalScrollBar = null;
  359. UIConfig.loaderErrorSign = null;
  360. UIConfig.popupMenu = null;
  361. UIConfig.popupMenu_seperator = null;
  362. UIConfig.tooltipsWin = null;
  363. UIConfig.verticalScrollBar = null;
  364. UIConfig.windowModalWaiting = null;
  365. UIPackage.branch = null;
  366. }
  367. public void ApplyModifiedProperties()
  368. {
  369. EMRenderSupport.Reload();
  370. }
  371. public delegate NAudioClip SoundLoader(string url);
  372. /// <summary>
  373. ///
  374. /// </summary>
  375. public static SoundLoader soundLoader = null;
  376. }
  377. }