UIPanel.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. #if UNITY_EDITOR
  5. using UnityEditor;
  6. #endif
  7. namespace FairyGUI
  8. {
  9. /// <summary>
  10. ///
  11. /// </summary>
  12. public enum FitScreen
  13. {
  14. None,
  15. FitSize,
  16. FitWidthAndSetMiddle,
  17. FitHeightAndSetCenter
  18. }
  19. /// <summary>
  20. ///
  21. /// </summary>
  22. [ExecuteInEditMode]
  23. [AddComponentMenu("FairyGUI/UI Panel")]
  24. public class UIPanel : MonoBehaviour, EMRenderTarget
  25. {
  26. /// <summary>
  27. ///
  28. /// </summary>
  29. public Container container { get; private set; }
  30. /// <summary>
  31. ///
  32. /// </summary>
  33. public string packageName;
  34. /// <summary>
  35. ///
  36. /// </summary>
  37. public string componentName;
  38. /// <summary>
  39. ///
  40. /// </summary>
  41. public FitScreen fitScreen;
  42. /// <summary>
  43. ///
  44. /// </summary>
  45. public int sortingOrder;
  46. [SerializeField]
  47. string packagePath;
  48. [SerializeField]
  49. RenderMode renderMode = RenderMode.ScreenSpaceOverlay;
  50. [SerializeField]
  51. Camera renderCamera = null;
  52. [SerializeField]
  53. Vector3 position;
  54. [SerializeField]
  55. Vector3 scale = new Vector3(1, 1, 1);
  56. [SerializeField]
  57. Vector3 rotation = new Vector3(0, 0, 0);
  58. [SerializeField]
  59. bool fairyBatching = false;
  60. [SerializeField]
  61. bool touchDisabled = false;
  62. [SerializeField]
  63. Vector2 cachedUISize;
  64. [SerializeField]
  65. HitTestMode hitTestMode = HitTestMode.Default;
  66. [SerializeField]
  67. bool setNativeChildrenOrder = false;
  68. [System.NonSerialized]
  69. int screenSizeVer;
  70. [System.NonSerialized]
  71. Rect uiBounds; //Track bounds even when UI is not created, edit mode
  72. GComponent _ui;
  73. [NonSerialized]
  74. bool _created;
  75. List<Renderer> _renders;
  76. void OnEnable()
  77. {
  78. if (Application.isPlaying)
  79. {
  80. if (this.container == null)
  81. {
  82. CreateContainer();
  83. if (!string.IsNullOrEmpty(packagePath) && UIPackage.GetByName(packageName) == null)
  84. UIPackage.AddPackage(packagePath);
  85. }
  86. }
  87. else
  88. {
  89. //不在播放状态时我们不在OnEnable创建,因为Prefab也会调用OnEnable,延迟到Update里创建(Prefab不调用Update)
  90. //每次播放前都会disable/enable一次。。。
  91. if (container != null)//如果不为null,可能是因为Prefab revert, 而不是因为Assembly reload,
  92. OnDestroy();
  93. EMRenderSupport.Add(this);
  94. screenSizeVer = 0;
  95. uiBounds.position = position;
  96. uiBounds.size = cachedUISize;
  97. if (uiBounds.size == Vector2.zero)
  98. uiBounds.size = new Vector2(30, 30);
  99. }
  100. }
  101. void OnDisable()
  102. {
  103. if (!Application.isPlaying)
  104. EMRenderSupport.Remove(this);
  105. }
  106. void Start()
  107. {
  108. if (!_created && Application.isPlaying)
  109. CreateUI_PlayMode();
  110. }
  111. void Update()
  112. {
  113. if (screenSizeVer != StageCamera.screenSizeVer)
  114. HandleScreenSizeChanged();
  115. }
  116. void OnDestroy()
  117. {
  118. if (container != null)
  119. {
  120. if (!Application.isPlaying)
  121. EMRenderSupport.Remove(this);
  122. if (_ui != null)
  123. {
  124. _ui.Dispose();
  125. _ui = null;
  126. }
  127. container.Dispose();
  128. container = null;
  129. }
  130. _renders = null;
  131. }
  132. void CreateContainer()
  133. {
  134. if (!Application.isPlaying)
  135. {
  136. Transform t = this.transform;
  137. int cnt = t.childCount;
  138. while (cnt > 0)
  139. {
  140. GameObject go = t.GetChild(cnt - 1).gameObject;
  141. if (go.name == "UI(AutoGenerated)")
  142. {
  143. #if (UNITY_2018_3_OR_NEWER && UNITY_EDITOR)
  144. if (PrefabUtility.IsPartOfPrefabInstance(go))
  145. PrefabUtility.UnpackPrefabInstance(PrefabUtility.GetOutermostPrefabInstanceRoot(gameObject), PrefabUnpackMode.Completely, InteractionMode.AutomatedAction);
  146. #endif
  147. UnityEngine.Object.DestroyImmediate(go);
  148. }
  149. cnt--;
  150. }
  151. }
  152. this.container = new Container(this.gameObject);
  153. this.container.renderMode = renderMode;
  154. this.container.renderCamera = renderCamera;
  155. this.container.touchable = !touchDisabled;
  156. this.container._panelOrder = sortingOrder;
  157. this.container.fairyBatching = fairyBatching;
  158. if (Application.isPlaying)
  159. {
  160. SetSortingOrder(this.sortingOrder, true);
  161. if (this.hitTestMode == HitTestMode.Raycast)
  162. {
  163. ColliderHitTest hitArea = new ColliderHitTest();
  164. hitArea.collider = this.gameObject.AddComponent<BoxCollider>();
  165. this.container.hitArea = hitArea;
  166. }
  167. if (setNativeChildrenOrder)
  168. {
  169. CacheNativeChildrenRenderers();
  170. this.container.onUpdate += () =>
  171. {
  172. int cnt = _renders.Count;
  173. int sv = UpdateContext.current.renderingOrder++;
  174. for (int i = 0; i < cnt; i++)
  175. {
  176. Renderer r = _renders[i];
  177. if (r != null)
  178. _renders[i].sortingOrder = sv;
  179. }
  180. };
  181. }
  182. }
  183. }
  184. /// <summary>
  185. ///
  186. /// </summary>
  187. public GComponent ui
  188. {
  189. get
  190. {
  191. if (!_created && Application.isPlaying)
  192. {
  193. if (!string.IsNullOrEmpty(packagePath) && UIPackage.GetByName(packageName) == null)
  194. UIPackage.AddPackage(packagePath);
  195. CreateUI_PlayMode();
  196. }
  197. return _ui;
  198. }
  199. }
  200. /// <summary>
  201. ///
  202. /// </summary>
  203. public void CreateUI()
  204. {
  205. if (_ui != null)
  206. {
  207. _ui.Dispose();
  208. _ui = null;
  209. }
  210. CreateUI_PlayMode();
  211. }
  212. /// <summary>
  213. /// Change the sorting order of the panel in runtime.
  214. /// </summary>
  215. /// <param name="value">sorting order value</param>
  216. /// <param name="apply">false if you dont want the default sorting behavior. e.g. call Stage.SortWorldSpacePanelsByZOrder later.</param>
  217. public void SetSortingOrder(int value, bool apply)
  218. {
  219. this.sortingOrder = value;
  220. container._panelOrder = value;
  221. if (apply)
  222. Stage.inst.ApplyPanelOrder(container);
  223. }
  224. /// <summary>
  225. ///
  226. /// </summary>
  227. /// <param name="value"></param>
  228. public void SetHitTestMode(HitTestMode value)
  229. {
  230. if (this.hitTestMode != value)
  231. {
  232. this.hitTestMode = value;
  233. BoxCollider collider = this.gameObject.GetComponent<BoxCollider>();
  234. if (this.hitTestMode == HitTestMode.Raycast)
  235. {
  236. if (collider == null)
  237. collider = this.gameObject.AddComponent<BoxCollider>();
  238. ColliderHitTest hitArea = new ColliderHitTest();
  239. hitArea.collider = collider;
  240. this.container.hitArea = hitArea;
  241. if (_ui != null)
  242. UpdateHitArea();
  243. }
  244. else
  245. {
  246. this.container.hitArea = null;
  247. if (collider != null)
  248. Component.Destroy(collider);
  249. }
  250. }
  251. }
  252. /// <summary>
  253. ///
  254. /// </summary>
  255. public void CacheNativeChildrenRenderers()
  256. {
  257. if (_renders == null)
  258. _renders = new List<Renderer>();
  259. else
  260. _renders.Clear();
  261. Transform t = this.container.cachedTransform;
  262. int cnt = t.childCount;
  263. for (int i = 0; i < cnt; i++)
  264. {
  265. GameObject go = t.GetChild(i).gameObject;
  266. if (go.name != "GComponent")
  267. _renders.AddRange(go.GetComponentsInChildren<Renderer>(true));
  268. }
  269. cnt = _renders.Count;
  270. for (int i = 0; i < cnt; i++)
  271. {
  272. Renderer r = _renders[i];
  273. if ((r is SkinnedMeshRenderer) || (r is MeshRenderer))
  274. {
  275. //Set the object rendering in Transparent Queue as UI objects
  276. if (r.sharedMaterial != null)
  277. r.sharedMaterial.renderQueue = 3000;
  278. }
  279. }
  280. }
  281. void CreateUI_PlayMode()
  282. {
  283. _created = true;
  284. if (string.IsNullOrEmpty(packageName) || string.IsNullOrEmpty(componentName))
  285. return;
  286. _ui = (GComponent)UIPackage.CreateObject(packageName, componentName);
  287. if (_ui != null)
  288. {
  289. _ui.position = position;
  290. if (scale.x != 0 && scale.y != 0)
  291. _ui.scale = scale;
  292. _ui.rotationX = rotation.x;
  293. _ui.rotationY = rotation.y;
  294. _ui.rotation = rotation.z;
  295. if (this.container.hitArea != null)
  296. {
  297. UpdateHitArea();
  298. _ui.onSizeChanged.Add(UpdateHitArea);
  299. _ui.onPositionChanged.Add(UpdateHitArea);
  300. }
  301. this.container.AddChildAt(_ui.displayObject, 0);
  302. HandleScreenSizeChanged();
  303. }
  304. else
  305. Debug.LogError("Create " + packageName + "/" + componentName + " failed!");
  306. }
  307. void UpdateHitArea()
  308. {
  309. ColliderHitTest hitArea = this.container.hitArea as ColliderHitTest;
  310. if (hitArea != null)
  311. {
  312. ((BoxCollider)hitArea.collider).center = new Vector3(_ui.xMin + _ui.width / 2, -_ui.yMin - _ui.height / 2);
  313. ((BoxCollider)hitArea.collider).size = _ui.size;
  314. }
  315. }
  316. void CreateUI_EditMode()
  317. {
  318. if (!EMRenderSupport.packageListReady || UIPackage.GetByName(packageName) == null)
  319. return;
  320. DisplayObject.hideFlags = HideFlags.DontSaveInEditor;
  321. GObject obj = UIPackage.CreateObject(packageName, componentName);
  322. if (obj != null && !(obj is GComponent))
  323. {
  324. obj.Dispose();
  325. Debug.LogWarning("Not a GComponnet: " + packageName + "/" + componentName);
  326. return;
  327. }
  328. _ui = (GComponent)obj;
  329. if (_ui != null)
  330. {
  331. _ui.displayObject.gameObject.hideFlags |= HideFlags.HideInHierarchy;
  332. _ui.gameObjectName = "UI(AutoGenerated)";
  333. _ui.position = position;
  334. if (scale.x != 0 && scale.y != 0)
  335. _ui.scale = scale;
  336. _ui.rotationX = rotation.x;
  337. _ui.rotationY = rotation.y;
  338. _ui.rotation = rotation.z;
  339. this.container.AddChildAt(_ui.displayObject, 0);
  340. cachedUISize = _ui.size;
  341. uiBounds.size = cachedUISize;
  342. HandleScreenSizeChanged();
  343. }
  344. }
  345. void HandleScreenSizeChanged()
  346. {
  347. if (!Application.isPlaying)
  348. DisplayObject.hideFlags = HideFlags.DontSaveInEditor;
  349. screenSizeVer = StageCamera.screenSizeVer;
  350. int width = Screen.width;
  351. int height = Screen.height;
  352. if (this.container != null)
  353. {
  354. Camera cam = container.GetRenderCamera();
  355. if (cam.targetDisplay != 0 && cam.targetDisplay < Display.displays.Length)
  356. {
  357. width = Display.displays[cam.targetDisplay].renderingWidth;
  358. height = Display.displays[cam.targetDisplay].renderingHeight;
  359. }
  360. if (this.container.renderMode != RenderMode.WorldSpace)
  361. {
  362. StageCamera sc = cam.GetComponent<StageCamera>();
  363. if (sc == null)
  364. sc = StageCamera.main.GetComponent<StageCamera>();
  365. this.container.scale = new Vector2(sc.unitsPerPixel * UIContentScaler.scaleFactor, sc.unitsPerPixel * UIContentScaler.scaleFactor);
  366. }
  367. }
  368. width = Mathf.CeilToInt(width / UIContentScaler.scaleFactor);
  369. height = Mathf.CeilToInt(height / UIContentScaler.scaleFactor);
  370. if (_ui != null)
  371. {
  372. switch (fitScreen)
  373. {
  374. case FitScreen.FitSize:
  375. _ui.SetSize(width, height);
  376. _ui.SetXY(0, 0, true);
  377. break;
  378. case FitScreen.FitWidthAndSetMiddle:
  379. _ui.SetSize(width, _ui.sourceHeight);
  380. _ui.SetXY(0, (int)((height - _ui.sourceHeight) / 2), true);
  381. break;
  382. case FitScreen.FitHeightAndSetCenter:
  383. _ui.SetSize(_ui.sourceWidth, height);
  384. _ui.SetXY((int)((width - _ui.sourceWidth) / 2), 0, true);
  385. break;
  386. }
  387. UpdateHitArea();
  388. }
  389. else
  390. {
  391. switch (fitScreen)
  392. {
  393. case FitScreen.FitSize:
  394. uiBounds.position = new Vector2(0, 0);
  395. uiBounds.size = new Vector2(width, height);
  396. break;
  397. case FitScreen.FitWidthAndSetMiddle:
  398. uiBounds.position = new Vector2(0, (int)((height - cachedUISize.y) / 2));
  399. uiBounds.size = new Vector2(width, cachedUISize.y);
  400. break;
  401. case FitScreen.FitHeightAndSetCenter:
  402. uiBounds.position = new Vector2((int)((width - cachedUISize.x) / 2), 0);
  403. uiBounds.size = new Vector2(cachedUISize.x, height);
  404. break;
  405. }
  406. }
  407. }
  408. #region edit mode functions
  409. void OnUpdateSource(object[] data)
  410. {
  411. if (Application.isPlaying)
  412. return;
  413. this.packageName = (string)data[0];
  414. this.packagePath = (string)data[1];
  415. this.componentName = (string)data[2];
  416. if ((bool)data[3])
  417. {
  418. if (container == null)
  419. return;
  420. if (_ui != null)
  421. {
  422. _ui.Dispose();
  423. _ui = null;
  424. }
  425. }
  426. }
  427. public void ApplyModifiedProperties(bool sortingOrderChanged, bool fitScreenChanged)
  428. {
  429. if (container != null)
  430. {
  431. container.renderMode = renderMode;
  432. container.renderCamera = renderCamera;
  433. if (sortingOrderChanged)
  434. {
  435. container._panelOrder = sortingOrder;
  436. if (Application.isPlaying)
  437. SetSortingOrder(sortingOrder, true);
  438. else
  439. EMRenderSupport.orderChanged = true;
  440. }
  441. container.fairyBatching = fairyBatching;
  442. }
  443. if (_ui != null)
  444. {
  445. if (fitScreen == FitScreen.None)
  446. _ui.position = position;
  447. if (scale.x != 0 && scale.y != 0)
  448. _ui.scale = scale;
  449. _ui.rotationX = rotation.x;
  450. _ui.rotationY = rotation.y;
  451. _ui.rotation = rotation.z;
  452. }
  453. if (fitScreen == FitScreen.None)
  454. uiBounds.position = position;
  455. screenSizeVer = 0;//force HandleScreenSizeChanged be called
  456. if (fitScreenChanged && this.fitScreen == FitScreen.None)
  457. {
  458. if (_ui != null)
  459. _ui.SetSize(_ui.sourceWidth, _ui.sourceHeight);
  460. uiBounds.size = cachedUISize;
  461. }
  462. }
  463. public void MoveUI(Vector3 delta)
  464. {
  465. if (fitScreen != FitScreen.None)
  466. return;
  467. this.position += delta;
  468. if (_ui != null)
  469. _ui.position = position;
  470. uiBounds.position = position;
  471. }
  472. public Vector3 GetUIWorldPosition()
  473. {
  474. if (_ui != null)
  475. return _ui.displayObject.cachedTransform.position;
  476. else
  477. return this.container.cachedTransform.TransformPoint(uiBounds.position);
  478. }
  479. void OnDrawGizmos()
  480. {
  481. if (Application.isPlaying || this.container == null)
  482. return;
  483. Vector3 pos, size;
  484. if (_ui != null)
  485. {
  486. Gizmos.matrix = _ui.displayObject.cachedTransform.localToWorldMatrix;
  487. pos = new Vector3(_ui.width / 2, -_ui.height / 2, 0);
  488. size = new Vector3(_ui.width, _ui.height, 0);
  489. }
  490. else
  491. {
  492. Gizmos.matrix = this.container.cachedTransform.localToWorldMatrix;
  493. pos = new Vector3(uiBounds.x + uiBounds.width / 2, -uiBounds.y - uiBounds.height / 2, 0);
  494. size = new Vector3(uiBounds.width, uiBounds.height, 0);
  495. }
  496. Gizmos.color = new Color(0, 0, 0, 0);
  497. Gizmos.DrawCube(pos, size);
  498. Gizmos.color = Color.white;
  499. Gizmos.DrawWireCube(pos, size);
  500. }
  501. public int EM_sortingOrder
  502. {
  503. get { return sortingOrder; }
  504. }
  505. public void EM_BeforeUpdate()
  506. {
  507. if (container == null)
  508. CreateContainer();
  509. if (packageName != null && componentName != null && _ui == null)
  510. CreateUI_EditMode();
  511. if (screenSizeVer != StageCamera.screenSizeVer)
  512. HandleScreenSizeChanged();
  513. }
  514. public void EM_Update(UpdateContext context)
  515. {
  516. DisplayObject.hideFlags = HideFlags.DontSaveInEditor;
  517. container.Update(context);
  518. if (setNativeChildrenOrder)
  519. {
  520. CacheNativeChildrenRenderers();
  521. int cnt = _renders.Count;
  522. int sv = context.renderingOrder++;
  523. for (int i = 0; i < cnt; i++)
  524. {
  525. Renderer r = _renders[i];
  526. if (r != null)
  527. r.sortingOrder = sv;
  528. }
  529. }
  530. }
  531. public void EM_Reload()
  532. {
  533. if (_ui != null)
  534. {
  535. _ui.Dispose();
  536. _ui = null;
  537. }
  538. }
  539. #endregion
  540. }
  541. }