DisplayObject.cs 58 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878
  1. using System;
  2. using System.Text;
  3. using UnityEngine;
  4. using FairyGUI.Utils;
  5. namespace FairyGUI
  6. {
  7. /// <summary>
  8. ///
  9. /// </summary>
  10. public class DisplayObject : EventDispatcher
  11. {
  12. /// <summary>
  13. ///
  14. /// </summary>
  15. public string name;
  16. /// <summary>
  17. ///
  18. /// </summary>
  19. public Container parent { get; private set; }
  20. /// <summary>
  21. ///
  22. /// </summary>
  23. public GameObject gameObject { get; protected set; }
  24. /// <summary>
  25. ///
  26. /// </summary>
  27. public Transform cachedTransform { get; protected set; }
  28. /// <summary>
  29. ///
  30. /// </summary>
  31. public NGraphics graphics { get; protected set; }
  32. /// <summary>
  33. ///
  34. /// </summary>
  35. public NGraphics paintingGraphics { get; protected set; }
  36. /// <summary>
  37. ///
  38. /// </summary>
  39. public event Action onPaint;
  40. /// <summary>
  41. ///
  42. /// </summary>
  43. public GObject gOwner;
  44. /// <summary>
  45. ///
  46. /// </summary>
  47. public uint id;
  48. bool _visible;
  49. bool _touchable;
  50. Vector2 _pivot;
  51. Vector3 _pivotOffset;
  52. Vector3 _rotation; //由于万向锁,单独旋转一个轴是会影响到其他轴的,所以这里需要单独保存
  53. Vector2 _skew;
  54. int _renderingOrder;
  55. float _alpha;
  56. bool _grayed;
  57. BlendMode _blendMode;
  58. IFilter _filter;
  59. Transform _home;
  60. string _cursor;
  61. bool _perspective;
  62. int _focalLength;
  63. Vector3 _pixelPerfectAdjustment;
  64. int _checkPixelPerfect;
  65. EventListener _onClick;
  66. EventListener _onRightClick;
  67. EventListener _onTouchBegin;
  68. EventListener _onTouchMove;
  69. EventListener _onTouchEnd;
  70. EventListener _onRollOver;
  71. EventListener _onRollOut;
  72. EventListener _onMouseWheel;
  73. EventListener _onAddedToStage;
  74. EventListener _onRemovedFromStage;
  75. EventListener _onKeyDown;
  76. EventListener _onClickLink;
  77. EventListener _onFocusIn;
  78. EventListener _onFocusOut;
  79. protected internal int _paintingMode; //1-滤镜,2-blendMode,4-transformMatrix, 8-cacheAsBitmap
  80. protected internal PaintingInfo _paintingInfo;
  81. protected Rect _contentRect;
  82. protected NGraphics.VertexMatrix _vertexMatrix;
  83. protected internal Flags _flags;
  84. protected internal float[] _batchingBounds;
  85. internal static uint _gInstanceCounter;
  86. internal static HideFlags hideFlags = HideFlags.None;
  87. public DisplayObject()
  88. {
  89. id = _gInstanceCounter++;
  90. _alpha = 1;
  91. _visible = true;
  92. _touchable = true;
  93. _blendMode = BlendMode.Normal;
  94. _focalLength = 2000;
  95. _flags |= Flags.OutlineChanged;
  96. if (UIConfig.makePixelPerfect)
  97. _flags |= Flags.PixelPerfect;
  98. }
  99. /// <summary>
  100. ///
  101. /// </summary>
  102. public EventListener onClick
  103. {
  104. get { return _onClick ?? (_onClick = new EventListener(this, "onClick")); }
  105. }
  106. /// <summary>
  107. ///
  108. /// </summary>
  109. public EventListener onRightClick
  110. {
  111. get { return _onRightClick ?? (_onRightClick = new EventListener(this, "onRightClick")); }
  112. }
  113. /// <summary>
  114. ///
  115. /// </summary>
  116. public EventListener onTouchBegin
  117. {
  118. get { return _onTouchBegin ?? (_onTouchBegin = new EventListener(this, "onTouchBegin")); }
  119. }
  120. /// <summary>
  121. ///
  122. /// </summary>
  123. public EventListener onTouchMove
  124. {
  125. get { return _onTouchMove ?? (_onTouchMove = new EventListener(this, "onTouchMove")); }
  126. }
  127. /// <summary>
  128. ///
  129. /// </summary>
  130. public EventListener onTouchEnd
  131. {
  132. get { return _onTouchEnd ?? (_onTouchEnd = new EventListener(this, "onTouchEnd")); }
  133. }
  134. /// <summary>
  135. ///
  136. /// </summary>
  137. public EventListener onRollOver
  138. {
  139. get { return _onRollOver ?? (_onRollOver = new EventListener(this, "onRollOver")); }
  140. }
  141. /// <summary>
  142. ///
  143. /// </summary>
  144. public EventListener onRollOut
  145. {
  146. get { return _onRollOut ?? (_onRollOut = new EventListener(this, "onRollOut")); }
  147. }
  148. /// <summary>
  149. ///
  150. /// </summary>
  151. public EventListener onMouseWheel
  152. {
  153. get { return _onMouseWheel ?? (_onMouseWheel = new EventListener(this, "onMouseWheel")); }
  154. }
  155. /// <summary>
  156. ///
  157. /// </summary>
  158. public EventListener onAddedToStage
  159. {
  160. get { return _onAddedToStage ?? (_onAddedToStage = new EventListener(this, "onAddedToStage")); }
  161. }
  162. /// <summary>
  163. ///
  164. /// </summary>
  165. public EventListener onRemovedFromStage
  166. {
  167. get { return _onRemovedFromStage ?? (_onRemovedFromStage = new EventListener(this, "onRemovedFromStage")); }
  168. }
  169. /// <summary>
  170. ///
  171. /// </summary>
  172. public EventListener onKeyDown
  173. {
  174. get { return _onKeyDown ?? (_onKeyDown = new EventListener(this, "onKeyDown")); }
  175. }
  176. /// <summary>
  177. ///
  178. /// </summary>
  179. public EventListener onClickLink
  180. {
  181. get { return _onClickLink ?? (_onClickLink = new EventListener(this, "onClickLink")); }
  182. }
  183. /// <summary>
  184. ///
  185. /// </summary>
  186. public EventListener onFocusIn
  187. {
  188. get { return _onFocusIn ?? (_onFocusIn = new EventListener(this, "onFocusIn")); }
  189. }
  190. /// <summary>
  191. ///
  192. /// </summary>
  193. public EventListener onFocusOut
  194. {
  195. get { return _onFocusOut ?? (_onFocusOut = new EventListener(this, "onFocusOut")); }
  196. }
  197. protected void CreateGameObject(string gameObjectName)
  198. {
  199. gameObject = new GameObject(gameObjectName);
  200. cachedTransform = gameObject.transform;
  201. if (Application.isPlaying)
  202. {
  203. UnityEngine.Object.DontDestroyOnLoad(gameObject);
  204. DisplayObjectInfo info = gameObject.AddComponent<DisplayObjectInfo>();
  205. info.displayObject = this;
  206. }
  207. gameObject.hideFlags = DisplayObject.hideFlags;
  208. gameObject.SetActive(false);
  209. }
  210. protected void SetGameObject(GameObject gameObject)
  211. {
  212. this.gameObject = gameObject;
  213. this.cachedTransform = gameObject.transform;
  214. _rotation = cachedTransform.localEulerAngles;
  215. _flags |= Flags.UserGameObject;
  216. }
  217. protected void DestroyGameObject()
  218. {
  219. if ((_flags & Flags.UserGameObject) == 0 && gameObject != null)
  220. {
  221. if (Application.isPlaying)
  222. GameObject.Destroy(gameObject);
  223. else
  224. GameObject.DestroyImmediate(gameObject);
  225. gameObject = null;
  226. cachedTransform = null;
  227. }
  228. }
  229. /// <summary>
  230. ///
  231. /// </summary>
  232. public float alpha
  233. {
  234. get { return _alpha; }
  235. set { _alpha = value; }
  236. }
  237. /// <summary>
  238. ///
  239. /// </summary>
  240. public bool grayed
  241. {
  242. get { return _grayed; }
  243. set { _grayed = value; }
  244. }
  245. /// <summary>
  246. ///
  247. /// </summary>
  248. public bool visible
  249. {
  250. get { return _visible; }
  251. set
  252. {
  253. if (_visible != value)
  254. {
  255. _visible = value;
  256. _flags |= Flags.OutlineChanged;
  257. if (parent != null && _visible)
  258. {
  259. gameObject.SetActive(true);
  260. InvalidateBatchingState();
  261. if (this is Container)
  262. ((Container)this).InvalidateBatchingState(true);
  263. }
  264. else
  265. gameObject.SetActive(false);
  266. }
  267. }
  268. }
  269. /// <summary>
  270. ///
  271. /// </summary>
  272. public float x
  273. {
  274. get { return cachedTransform.localPosition.x; }
  275. set
  276. {
  277. SetPosition(value, -cachedTransform.localPosition.y, cachedTransform.localPosition.z);
  278. }
  279. }
  280. /// <summary>
  281. ///
  282. /// </summary>
  283. public float y
  284. {
  285. get { return -cachedTransform.localPosition.y; }
  286. set
  287. {
  288. SetPosition(cachedTransform.localPosition.x, value, cachedTransform.localPosition.z);
  289. }
  290. }
  291. /// <summary>
  292. ///
  293. /// </summary>
  294. public float z
  295. {
  296. get { return cachedTransform.localPosition.z; }
  297. set
  298. {
  299. SetPosition(cachedTransform.localPosition.x, -cachedTransform.localPosition.y, value);
  300. }
  301. }
  302. /// <summary>
  303. ///
  304. /// </summary>
  305. public Vector2 xy
  306. {
  307. get { return new Vector2(this.x, this.y); }
  308. set { SetPosition(value.x, value.y, cachedTransform.localPosition.z); }
  309. }
  310. /// <summary>
  311. ///
  312. /// </summary>
  313. public Vector3 position
  314. {
  315. get { return new Vector3(this.x, this.y, this.z); }
  316. set { SetPosition(value.x, value.y, value.z); }
  317. }
  318. /// <summary>
  319. ///
  320. /// </summary>
  321. /// <param name="xv"></param>
  322. /// <param name="yv"></param>
  323. public void SetXY(float xv, float yv)
  324. {
  325. SetPosition(xv, yv, cachedTransform.localPosition.z);
  326. }
  327. /// <summary>
  328. ///
  329. /// </summary>
  330. /// <param name="xv"></param>
  331. /// <param name="yv"></param>
  332. /// <param name="zv"></param>
  333. public void SetPosition(float xv, float yv, float zv)
  334. {
  335. Vector3 v = new Vector3();
  336. v.x = xv;
  337. v.y = -yv;
  338. v.z = zv;
  339. if (v != cachedTransform.localPosition)
  340. {
  341. cachedTransform.localPosition = v;
  342. _flags |= Flags.OutlineChanged;
  343. if ((_flags & Flags.PixelPerfect) != 0)
  344. {
  345. //总在下一帧再完成PixelPerfect,这样当物体在连续运动时,不会因为PixelPerfect而发生抖动。
  346. _checkPixelPerfect = Time.frameCount;
  347. _pixelPerfectAdjustment = Vector3.zero;
  348. }
  349. }
  350. }
  351. /// <summary>
  352. /// If the object position is align by pixel
  353. /// </summary>
  354. public bool pixelPerfect
  355. {
  356. get { return (_flags & Flags.PixelPerfect) != 0; }
  357. set
  358. {
  359. if (value)
  360. _flags |= Flags.PixelPerfect;
  361. else
  362. _flags &= ~Flags.PixelPerfect;
  363. }
  364. }
  365. /// <summary>
  366. ///
  367. /// </summary>
  368. public float width
  369. {
  370. get
  371. {
  372. EnsureSizeCorrect();
  373. return _contentRect.width;
  374. }
  375. set
  376. {
  377. if (!Mathf.Approximately(value, _contentRect.width))
  378. {
  379. _contentRect.width = value;
  380. _flags |= Flags.WidthChanged;
  381. _flags &= ~Flags.HeightChanged;
  382. OnSizeChanged();
  383. }
  384. }
  385. }
  386. /// <summary>
  387. ///
  388. /// </summary>
  389. public float height
  390. {
  391. get
  392. {
  393. EnsureSizeCorrect();
  394. return _contentRect.height;
  395. }
  396. set
  397. {
  398. if (!Mathf.Approximately(value, _contentRect.height))
  399. {
  400. _contentRect.height = value;
  401. _flags &= ~Flags.WidthChanged;
  402. _flags |= Flags.HeightChanged;
  403. OnSizeChanged();
  404. }
  405. }
  406. }
  407. /// <summary>
  408. ///
  409. /// </summary>
  410. public Vector2 size
  411. {
  412. get
  413. {
  414. EnsureSizeCorrect();
  415. return _contentRect.size;
  416. }
  417. set
  418. {
  419. SetSize(value.x, value.y);
  420. }
  421. }
  422. /// <summary>
  423. ///
  424. /// </summary>
  425. /// <param name="wv"></param>
  426. /// <param name="hv"></param>
  427. public void SetSize(float wv, float hv)
  428. {
  429. if (!Mathf.Approximately(wv, _contentRect.width))
  430. _flags |= Flags.WidthChanged;
  431. else
  432. _flags &= ~Flags.WidthChanged;
  433. if (!Mathf.Approximately(hv, _contentRect.height))
  434. _flags |= Flags.HeightChanged;
  435. else
  436. _flags &= ~Flags.HeightChanged;
  437. if ((_flags & Flags.WidthChanged) != 0 || (_flags & Flags.HeightChanged) != 0)
  438. {
  439. _contentRect.width = wv;
  440. _contentRect.height = hv;
  441. OnSizeChanged();
  442. }
  443. }
  444. virtual public void EnsureSizeCorrect()
  445. {
  446. }
  447. virtual protected void OnSizeChanged()
  448. {
  449. ApplyPivot();
  450. if (_paintingInfo != null)
  451. _paintingInfo.flag = 1;
  452. if (graphics != null)
  453. graphics.contentRect = _contentRect;
  454. _flags |= Flags.OutlineChanged;
  455. }
  456. /// <summary>
  457. ///
  458. /// </summary>
  459. public float scaleX
  460. {
  461. get { return cachedTransform.localScale.x; }
  462. set
  463. {
  464. Vector3 v = cachedTransform.localScale;
  465. v.x = v.z = ValidateScale(value);
  466. cachedTransform.localScale = v;
  467. _flags |= Flags.OutlineChanged;
  468. ApplyPivot();
  469. }
  470. }
  471. /// <summary>
  472. ///
  473. /// </summary>
  474. public float scaleY
  475. {
  476. get { return cachedTransform.localScale.y; }
  477. set
  478. {
  479. Vector3 v = cachedTransform.localScale;
  480. v.y = ValidateScale(value);
  481. cachedTransform.localScale = v;
  482. _flags |= Flags.OutlineChanged;
  483. ApplyPivot();
  484. }
  485. }
  486. /// <summary>
  487. ///
  488. /// </summary>
  489. /// <param name="xv"></param>
  490. /// <param name="yv"></param>
  491. public void SetScale(float xv, float yv)
  492. {
  493. Vector3 v = new Vector3();
  494. v.x = v.z = ValidateScale(xv);
  495. v.y = ValidateScale(yv);
  496. cachedTransform.localScale = v;
  497. _flags |= Flags.OutlineChanged;
  498. ApplyPivot();
  499. }
  500. /// <summary>
  501. /// 在scale过小情况(极端情况=0),当使用Transform的坐标变换时,变换到世界,再从世界变换到本地,会由于精度问题造成结果错误。
  502. /// 这种错误会导致Batching错误,因为Batching会使用缓存的outline。
  503. /// 这里限制一下scale的最小值作为当前解决方案。
  504. /// 这个方案并不完美,因为限制了本地scale值并不能保证对世界scale不会过小。
  505. /// </summary>
  506. /// <param name="value"></param>
  507. /// <returns></returns>
  508. private float ValidateScale(float value)
  509. {
  510. if (value >= 0 && value < 0.001f)
  511. value = 0.001f;
  512. else if (value < 0 && value > -0.001f)
  513. value = -0.001f;
  514. return value;
  515. }
  516. /// <summary>
  517. ///
  518. /// </summary>
  519. public Vector2 scale
  520. {
  521. get { return cachedTransform.localScale; }
  522. set
  523. {
  524. SetScale(value.x, value.y);
  525. }
  526. }
  527. /// <summary>
  528. ///
  529. /// </summary>
  530. public float rotation
  531. {
  532. get
  533. {
  534. //和Unity默认的旋转方向相反
  535. return -_rotation.z;
  536. }
  537. set
  538. {
  539. _rotation.z = -value;
  540. _flags |= Flags.OutlineChanged;
  541. if (_perspective)
  542. UpdateTransformMatrix();
  543. else
  544. {
  545. cachedTransform.localEulerAngles = _rotation;
  546. ApplyPivot();
  547. }
  548. }
  549. }
  550. /// <summary>
  551. ///
  552. /// </summary>
  553. public float rotationX
  554. {
  555. get
  556. {
  557. return _rotation.x;
  558. }
  559. set
  560. {
  561. _rotation.x = value;
  562. _flags |= Flags.OutlineChanged;
  563. if (_perspective)
  564. UpdateTransformMatrix();
  565. else
  566. {
  567. cachedTransform.localEulerAngles = _rotation;
  568. ApplyPivot();
  569. }
  570. }
  571. }
  572. /// <summary>
  573. ///
  574. /// </summary>
  575. public float rotationY
  576. {
  577. get
  578. {
  579. return _rotation.y;
  580. }
  581. set
  582. {
  583. _rotation.y = value;
  584. _flags |= Flags.OutlineChanged;
  585. if (_perspective)
  586. UpdateTransformMatrix();
  587. else
  588. {
  589. cachedTransform.localEulerAngles = _rotation;
  590. ApplyPivot();
  591. }
  592. }
  593. }
  594. /// <summary>
  595. ///
  596. /// </summary>
  597. public Vector2 skew
  598. {
  599. get { return _skew; }
  600. set
  601. {
  602. _skew = value;
  603. _flags |= Flags.OutlineChanged;
  604. if (!Application.isPlaying) //编辑期间不支持!!
  605. return;
  606. UpdateTransformMatrix();
  607. }
  608. }
  609. /// <summary>
  610. /// 当对象处于ScreenSpace,也就是使用正交相机渲染时,对象虽然可以绕X轴或者Y轴旋转,但没有透视效果。设置perspective,可以模拟出透视效果。
  611. /// </summary>
  612. public bool perspective
  613. {
  614. get
  615. {
  616. return _perspective;
  617. }
  618. set
  619. {
  620. if (_perspective != value)
  621. {
  622. _perspective = value;
  623. if (_perspective)//屏蔽Unity自身的旋转变换
  624. cachedTransform.localEulerAngles = Vector3.zero;
  625. else
  626. cachedTransform.localEulerAngles = _rotation;
  627. ApplyPivot();
  628. UpdateTransformMatrix();
  629. }
  630. }
  631. }
  632. /// <summary>
  633. ///
  634. /// </summary>
  635. public int focalLength
  636. {
  637. get { return _focalLength; }
  638. set
  639. {
  640. if (value <= 0)
  641. value = 1;
  642. _focalLength = value;
  643. if (_vertexMatrix != null)
  644. UpdateTransformMatrix();
  645. }
  646. }
  647. void UpdateTransformMatrix()
  648. {
  649. Matrix4x4 matrix = Matrix4x4.identity;
  650. if (_skew.x != 0 || _skew.y != 0)
  651. ToolSet.SkewMatrix(ref matrix, _skew.x, _skew.y);
  652. if (_perspective)
  653. matrix *= Matrix4x4.TRS(Vector3.zero, Quaternion.Euler(_rotation), Vector3.one);
  654. if (matrix.isIdentity)
  655. _vertexMatrix = null;
  656. else if (_vertexMatrix == null)
  657. _vertexMatrix = new NGraphics.VertexMatrix();
  658. //组件的transformMatrix是通过paintingMode实现的,因为全部通过矩阵变换的话,和unity自身的变换混杂在一起,无力理清。
  659. if (_vertexMatrix != null)
  660. {
  661. _vertexMatrix.matrix = matrix;
  662. _vertexMatrix.cameraPos = new Vector3(_pivot.x * _contentRect.width, -_pivot.y * _contentRect.height, _focalLength);
  663. if (graphics == null)
  664. EnterPaintingMode(4, null);
  665. }
  666. else
  667. {
  668. if (graphics == null)
  669. LeavePaintingMode(4);
  670. }
  671. if (_paintingMode > 0)
  672. {
  673. paintingGraphics.vertexMatrix = _vertexMatrix;
  674. _paintingInfo.flag = 1;
  675. }
  676. else if (graphics != null)
  677. graphics.vertexMatrix = _vertexMatrix;
  678. _flags |= Flags.OutlineChanged;
  679. }
  680. /// <summary>
  681. ///
  682. /// </summary>
  683. public Vector2 pivot
  684. {
  685. get { return _pivot; }
  686. set
  687. {
  688. Vector3 deltaPivot = new Vector2((value.x - _pivot.x) * _contentRect.width, (_pivot.y - value.y) * _contentRect.height);
  689. Vector3 oldOffset = _pivotOffset;
  690. _pivot = value;
  691. UpdatePivotOffset();
  692. Vector3 v = cachedTransform.localPosition;
  693. v += oldOffset - _pivotOffset + deltaPivot;
  694. cachedTransform.localPosition = v;
  695. _flags |= Flags.OutlineChanged;
  696. }
  697. }
  698. void UpdatePivotOffset()
  699. {
  700. float px = _pivot.x * _contentRect.width;
  701. float py = _pivot.y * _contentRect.height;
  702. //注意这里不用处理skew,因为在顶点变换里有对pivot的处理
  703. Matrix4x4 matrix = Matrix4x4.TRS(Vector3.zero, cachedTransform.localRotation, cachedTransform.localScale);
  704. _pivotOffset = matrix.MultiplyPoint(new Vector3(px, -py, 0));
  705. if (_vertexMatrix != null)
  706. _vertexMatrix.cameraPos = new Vector3(_pivot.x * _contentRect.width, -_pivot.y * _contentRect.height, _focalLength);
  707. }
  708. void ApplyPivot()
  709. {
  710. if (_pivot.x != 0 || _pivot.y != 0)
  711. {
  712. Vector3 oldOffset = _pivotOffset;
  713. UpdatePivotOffset();
  714. Vector3 v = cachedTransform.localPosition;
  715. if ((_flags & Flags.PixelPerfect) != 0)
  716. {
  717. v -= _pixelPerfectAdjustment;
  718. _checkPixelPerfect = Time.frameCount;
  719. _pixelPerfectAdjustment = Vector3.zero;
  720. }
  721. v += oldOffset - _pivotOffset;
  722. cachedTransform.localPosition = v;
  723. _flags |= Flags.OutlineChanged;
  724. }
  725. }
  726. /// <summary>
  727. /// This is the pivot position
  728. /// </summary>
  729. public Vector3 location
  730. {
  731. get
  732. {
  733. Vector3 pos = this.position;
  734. pos.x += _pivotOffset.x;
  735. pos.y -= _pivotOffset.y;
  736. pos.z += _pivotOffset.z;
  737. return pos;
  738. }
  739. set
  740. {
  741. this.SetPosition(value.x - _pivotOffset.x, value.y + _pivotOffset.y, value.z - _pivotOffset.z);
  742. }
  743. }
  744. /// <summary>
  745. ///
  746. /// </summary>
  747. virtual public Material material
  748. {
  749. get
  750. {
  751. if (graphics != null)
  752. return graphics.material;
  753. else
  754. return null;
  755. }
  756. set
  757. {
  758. if (graphics != null)
  759. graphics.material = value;
  760. }
  761. }
  762. /// <summary>
  763. ///
  764. /// </summary>
  765. virtual public string shader
  766. {
  767. get
  768. {
  769. if (graphics != null)
  770. return graphics.shader;
  771. else
  772. return null;
  773. }
  774. set
  775. {
  776. if (graphics != null)
  777. graphics.shader = value;
  778. }
  779. }
  780. /// <summary>
  781. ///
  782. /// </summary>
  783. virtual public int renderingOrder
  784. {
  785. get
  786. {
  787. return _renderingOrder;
  788. }
  789. set
  790. {
  791. if ((_flags & Flags.GameObjectDisposed) != 0)
  792. {
  793. DisplayDisposedWarning();
  794. return;
  795. }
  796. _renderingOrder = value;
  797. if (graphics != null)
  798. graphics.sortingOrder = value;
  799. if (_paintingMode > 0)
  800. paintingGraphics.sortingOrder = value;
  801. }
  802. }
  803. /// <summary>
  804. ///
  805. /// </summary>
  806. public int layer
  807. {
  808. get
  809. {
  810. if (_paintingMode > 0)
  811. return paintingGraphics.gameObject.layer;
  812. else
  813. return gameObject.layer;
  814. }
  815. set
  816. {
  817. SetLayer(value, false);
  818. }
  819. }
  820. /// <summary>
  821. /// If the object can be focused?
  822. /// </summary>
  823. public bool focusable
  824. {
  825. get { return (_flags & Flags.NotFocusable) == 0; }
  826. set
  827. {
  828. if (value)
  829. _flags &= ~Flags.NotFocusable;
  830. else
  831. _flags |= Flags.NotFocusable;
  832. }
  833. }
  834. /// <summary>
  835. /// If the object can be navigated by TAB?
  836. /// </summary>
  837. public bool tabStop
  838. {
  839. get { return (_flags & Flags.TabStop) != 0; }
  840. set
  841. {
  842. if (value)
  843. _flags |= Flags.TabStop;
  844. else
  845. _flags &= ~Flags.TabStop;
  846. }
  847. }
  848. /// <summary>
  849. /// If the object focused?
  850. /// </summary>
  851. public bool focused
  852. {
  853. get
  854. {
  855. return Stage.inst.focus == this || (this is Container) && ((Container)this).IsAncestorOf(Stage.inst.focus);
  856. }
  857. }
  858. internal bool _AcceptTab()
  859. {
  860. if (_touchable && _visible
  861. && ((_flags & Flags.TabStop) != 0 || (_flags & Flags.TabStopChildren) != 0)
  862. && (_flags & Flags.NotFocusable) == 0)
  863. {
  864. Stage.inst.SetFocus(this, true);
  865. return true;
  866. }
  867. else
  868. return false;
  869. }
  870. /// <summary>
  871. ///
  872. /// </summary>
  873. /// <value></value>
  874. public string cursor
  875. {
  876. get { return _cursor; }
  877. set
  878. {
  879. _cursor = value;
  880. if (Application.isPlaying
  881. && (this == Stage.inst.touchTarget || (this is Container) && ((Container)this).IsAncestorOf(Stage.inst.touchTarget)))
  882. {
  883. Stage.inst._ChangeCursor(_cursor);
  884. }
  885. }
  886. }
  887. /// <summary>
  888. ///
  889. /// </summary>
  890. public bool isDisposed
  891. {
  892. get { return (_flags & Flags.Disposed) != 0 || gameObject == null; }
  893. }
  894. internal void InternalSetParent(Container value)
  895. {
  896. if (parent != value)
  897. {
  898. if (value == null && (parent._flags & Flags.Disposed) != 0)
  899. parent = value;
  900. else
  901. {
  902. parent = value;
  903. UpdateHierarchy();
  904. }
  905. _flags |= Flags.OutlineChanged;
  906. }
  907. }
  908. /// <summary>
  909. ///
  910. /// </summary>
  911. public Container topmost
  912. {
  913. get
  914. {
  915. DisplayObject currentObject = this;
  916. while (currentObject.parent != null)
  917. currentObject = currentObject.parent;
  918. return currentObject as Container;
  919. }
  920. }
  921. /// <summary>
  922. ///
  923. /// </summary>
  924. public Stage stage
  925. {
  926. get
  927. {
  928. return topmost as Stage;
  929. }
  930. }
  931. /// <summary>
  932. ///
  933. /// </summary>
  934. public Container worldSpaceContainer
  935. {
  936. get
  937. {
  938. Container wsc = null;
  939. DisplayObject currentObject = this;
  940. while (currentObject.parent != null)
  941. {
  942. if ((currentObject is Container) && ((Container)currentObject).renderMode == RenderMode.WorldSpace)
  943. {
  944. wsc = (Container)currentObject;
  945. break;
  946. }
  947. currentObject = currentObject.parent;
  948. }
  949. return wsc;
  950. }
  951. }
  952. /// <summary>
  953. ///
  954. /// </summary>
  955. public bool touchable
  956. {
  957. get { return _touchable; }
  958. set
  959. {
  960. if (_touchable != value)
  961. {
  962. _touchable = value;
  963. if (this is Container)
  964. {
  965. ColliderHitTest hitArea = ((Container)this).hitArea as ColliderHitTest;
  966. if (hitArea != null)
  967. hitArea.collider.enabled = value;
  968. }
  969. }
  970. }
  971. }
  972. /// <summary>
  973. ///
  974. /// </summary>
  975. /// <value></value>
  976. public bool touchDisabled
  977. {
  978. get { return (_flags & Flags.TouchDisabled) != 0; }
  979. }
  980. /// <summary>
  981. /// 进入绘画模式,整个对象将画到一张RenderTexture上,然后这种贴图将代替原有的显示内容。
  982. /// 可以在onPaint回调里对这张纹理进行进一步操作,实现特殊效果。
  983. /// </summary>
  984. public void EnterPaintingMode()
  985. {
  986. EnterPaintingMode(16384, null, 1);
  987. }
  988. /// <summary>
  989. /// 进入绘画模式,整个对象将画到一张RenderTexture上,然后这种贴图将代替原有的显示内容。
  990. /// 可以在onPaint回调里对这张纹理进行进一步操作,实现特殊效果。
  991. /// 可能有多个地方要求进入绘画模式,这里用requestorId加以区别,取值是1、2、4、8、16以此类推。1024内内部保留。用户自定义的id从1024开始。
  992. /// </summary>
  993. /// <param name="requestId">请求者id</param>
  994. /// <param name="extend">纹理四周的留空。如果特殊处理后的内容大于原内容,那么这里的设置可以使纹理扩大。</param>
  995. public void EnterPaintingMode(int requestorId, Margin? extend)
  996. {
  997. EnterPaintingMode(requestorId, extend, 1);
  998. }
  999. /// <summary>
  1000. /// 进入绘画模式,整个对象将画到一张RenderTexture上,然后这种贴图将代替原有的显示内容。
  1001. /// 可以在onPaint回调里对这张纹理进行进一步操作,实现特殊效果。
  1002. /// 可能有多个地方要求进入绘画模式,这里用requestorId加以区别,取值是1、2、4、8、16以此类推。1024内内部保留。用户自定义的id从1024开始。
  1003. /// </summary>
  1004. /// <param name="requestorId">请求者id</param>
  1005. /// <param name="extend">扩展纹理。如果特殊处理后的内容大于原内容,那么这里的设置可以使纹理扩大。</param>
  1006. /// <param name="scale">附加一个缩放系数</param>
  1007. public void EnterPaintingMode(int requestorId, Margin? extend, float scale)
  1008. {
  1009. bool first = _paintingMode == 0;
  1010. _paintingMode |= requestorId;
  1011. if (first)
  1012. {
  1013. if (_paintingInfo == null)
  1014. {
  1015. _paintingInfo = new PaintingInfo()
  1016. {
  1017. captureDelegate = Capture,
  1018. scale = 1
  1019. };
  1020. }
  1021. if (paintingGraphics == null)
  1022. {
  1023. if (graphics == null)
  1024. paintingGraphics = new NGraphics(this.gameObject);
  1025. else
  1026. {
  1027. GameObject go = new GameObject(this.gameObject.name + " (Painter)");
  1028. go.layer = this.gameObject.layer;
  1029. go.transform.SetParent(cachedTransform, false);
  1030. go.hideFlags = DisplayObject.hideFlags;
  1031. paintingGraphics = new NGraphics(go);
  1032. }
  1033. }
  1034. else
  1035. paintingGraphics.enabled = true;
  1036. paintingGraphics.vertexMatrix = null;
  1037. if (this is Container)
  1038. {
  1039. ((Container)this).SetChildrenLayer(CaptureCamera.hiddenLayer);
  1040. ((Container)this).UpdateBatchingFlags();
  1041. }
  1042. else
  1043. this.InvalidateBatchingState();
  1044. if (graphics != null)
  1045. this.gameObject.layer = CaptureCamera.hiddenLayer;
  1046. }
  1047. if (extend != null)
  1048. _paintingInfo.extend = (Margin)extend;
  1049. _paintingInfo.scale = scale;
  1050. _paintingInfo.flag = 1;
  1051. }
  1052. /// <summary>
  1053. /// 离开绘画模式
  1054. /// </summary>
  1055. /// <param name="requestId"></param>
  1056. public void LeavePaintingMode(int requestorId)
  1057. {
  1058. if (_paintingMode == 0 || (_flags & Flags.Disposed) != 0)
  1059. return;
  1060. _paintingMode ^= requestorId;
  1061. if (_paintingMode == 0)
  1062. {
  1063. paintingGraphics.enabled = false;
  1064. if (this is Container)
  1065. {
  1066. ((Container)this).SetChildrenLayer(this.layer);
  1067. ((Container)this).UpdateBatchingFlags();
  1068. }
  1069. else
  1070. this.InvalidateBatchingState();
  1071. if (graphics != null)
  1072. this.gameObject.layer = paintingGraphics.gameObject.layer;
  1073. }
  1074. }
  1075. /// <summary>
  1076. ///
  1077. /// </summary>
  1078. public bool paintingMode
  1079. {
  1080. get { return _paintingMode > 0; }
  1081. }
  1082. /// <summary>
  1083. /// 将整个显示对象(如果是容器,则容器包含的整个显示列表)静态化,所有内容被缓冲到一张纹理上。
  1084. /// DC将保持为1。CPU消耗将降到最低。但对象的任何变化不会更新。
  1085. /// 当cacheAsBitmap已经为true时,再次调用cacheAsBitmap=true将会刷新对象一次。
  1086. /// </summary>
  1087. public bool cacheAsBitmap
  1088. {
  1089. get { return (_flags & Flags.CacheAsBitmap) != 0; }
  1090. set
  1091. {
  1092. if (value)
  1093. {
  1094. _flags |= Flags.CacheAsBitmap;
  1095. EnterPaintingMode(8, null, UIContentScaler.scaleFactor);
  1096. }
  1097. else
  1098. {
  1099. _flags &= ~Flags.CacheAsBitmap;
  1100. LeavePaintingMode(8);
  1101. }
  1102. }
  1103. }
  1104. /// <summary>
  1105. ///
  1106. /// </summary>
  1107. /// <param name="extend"></param>
  1108. /// <param name="scale"></param>
  1109. /// <returns></returns>
  1110. public Texture2D GetScreenShot(Margin? extend, float scale)
  1111. {
  1112. EnterPaintingMode(8, null, scale);
  1113. UpdatePainting();
  1114. Capture();
  1115. Texture2D output;
  1116. if (paintingGraphics.texture == null)
  1117. output = new Texture2D(1, 1, TextureFormat.RGBA32, false, true);
  1118. else
  1119. {
  1120. RenderTexture rt = (RenderTexture)paintingGraphics.texture.nativeTexture;
  1121. output = new Texture2D(rt.width, rt.height, TextureFormat.RGBA32, false, true);
  1122. RenderTexture old = RenderTexture.active;
  1123. RenderTexture.active = rt;
  1124. output.ReadPixels(new Rect(0, 0, rt.width, rt.height), 0, 0);
  1125. output.Apply();
  1126. RenderTexture.active = old;
  1127. }
  1128. LeavePaintingMode(8);
  1129. return output;
  1130. }
  1131. /// <summary>
  1132. ///
  1133. /// </summary>
  1134. public IFilter filter
  1135. {
  1136. get
  1137. {
  1138. return _filter;
  1139. }
  1140. set
  1141. {
  1142. if (!Application.isPlaying) //编辑期间不支持!!
  1143. return;
  1144. if (value == _filter)
  1145. return;
  1146. if (_filter != null)
  1147. _filter.Dispose();
  1148. if (value != null && value.target != null)
  1149. value.target.filter = null;
  1150. _filter = value;
  1151. if (_filter != null)
  1152. _filter.target = this;
  1153. }
  1154. }
  1155. /// <summary>
  1156. ///
  1157. /// </summary>
  1158. public BlendMode blendMode
  1159. {
  1160. get { return _blendMode; }
  1161. set
  1162. {
  1163. _blendMode = value;
  1164. InvalidateBatchingState();
  1165. if (graphics == null)
  1166. {
  1167. if (_blendMode != BlendMode.Normal)
  1168. {
  1169. if (!Application.isPlaying) //Not supported in edit mode!
  1170. return;
  1171. EnterPaintingMode(2, null);
  1172. paintingGraphics.blendMode = _blendMode;
  1173. }
  1174. else
  1175. LeavePaintingMode(2);
  1176. }
  1177. else
  1178. graphics.blendMode = _blendMode;
  1179. }
  1180. }
  1181. /// <summary>
  1182. ///
  1183. /// </summary>
  1184. /// <param name="targetSpace"></param>
  1185. /// <returns></returns>
  1186. virtual public Rect GetBounds(DisplayObject targetSpace)
  1187. {
  1188. EnsureSizeCorrect();
  1189. if (targetSpace == this) // optimization
  1190. {
  1191. return _contentRect;
  1192. }
  1193. else if (targetSpace == parent && _rotation.z == 0)
  1194. {
  1195. return new Rect(cachedTransform.localPosition.x, -cachedTransform.localPosition.y,
  1196. _contentRect.width * cachedTransform.localScale.x, _contentRect.height * cachedTransform.localScale.y);
  1197. }
  1198. else
  1199. return TransformRect(_contentRect, targetSpace);
  1200. }
  1201. internal DisplayObject InternalHitTest()
  1202. {
  1203. if (_visible && (!HitTestContext.forTouch || _touchable))
  1204. return HitTest();
  1205. else
  1206. return null;
  1207. }
  1208. internal DisplayObject InternalHitTestMask()
  1209. {
  1210. if (_visible)
  1211. return HitTest();
  1212. else
  1213. return null;
  1214. }
  1215. virtual protected DisplayObject HitTest()
  1216. {
  1217. Rect rect = GetBounds(this);
  1218. if (rect.width == 0 || rect.height == 0)
  1219. return null;
  1220. Vector2 localPoint = WorldToLocal(HitTestContext.worldPoint, HitTestContext.direction);
  1221. if (rect.Contains(localPoint))
  1222. return this;
  1223. else
  1224. return null;
  1225. }
  1226. /// <summary>
  1227. /// 将舞台坐标转换为本地坐标
  1228. /// </summary>
  1229. /// <param name="point"></param>
  1230. /// <returns></returns>
  1231. public Vector2 GlobalToLocal(Vector2 point)
  1232. {
  1233. Container wsc = this.worldSpaceContainer;
  1234. if (wsc != null)//I am in a world space
  1235. {
  1236. Camera cam = wsc.GetRenderCamera();
  1237. Vector3 worldPoint;
  1238. Vector3 direction;
  1239. Vector3 screenPoint = new Vector3();
  1240. screenPoint.x = point.x;
  1241. screenPoint.y = Screen.height - point.y;
  1242. if (wsc.hitArea is MeshColliderHitTest)
  1243. {
  1244. Ray ray = cam.ScreenPointToRay(screenPoint);
  1245. RaycastHit hit;
  1246. if (((MeshColliderHitTest)wsc.hitArea).collider.Raycast(ray, out hit, 100))
  1247. {
  1248. point = new Vector2(hit.textureCoord.x * _contentRect.width, (1 - hit.textureCoord.y) * _contentRect.height);
  1249. worldPoint = Stage.inst.cachedTransform.TransformPoint(point.x, -point.y, 0);
  1250. direction = Vector3.back;
  1251. }
  1252. else //当射线没有击中模型时,无法确定本地坐标
  1253. return new Vector2(float.NaN, float.NaN);
  1254. }
  1255. else
  1256. {
  1257. screenPoint.z = cam.WorldToScreenPoint(this.cachedTransform.position).z;
  1258. worldPoint = cam.ScreenToWorldPoint(screenPoint);
  1259. Ray ray = cam.ScreenPointToRay(screenPoint);
  1260. direction = Vector3.zero - ray.direction;
  1261. }
  1262. return this.WorldToLocal(worldPoint, direction);
  1263. }
  1264. else //I am in stage space
  1265. {
  1266. Vector3 worldPoint = Stage.inst.cachedTransform.TransformPoint(point.x, -point.y, 0);
  1267. return this.WorldToLocal(worldPoint, Vector3.back);
  1268. }
  1269. }
  1270. /// <summary>
  1271. /// 将本地坐标转换为舞台坐标
  1272. /// </summary>
  1273. /// <param name="point"></param>
  1274. /// <returns></returns>
  1275. public Vector2 LocalToGlobal(Vector2 point)
  1276. {
  1277. Container wsc = this.worldSpaceContainer;
  1278. Vector3 worldPoint = this.cachedTransform.TransformPoint(point.x, -point.y, 0);
  1279. if (wsc != null)
  1280. {
  1281. if (wsc.hitArea is MeshColliderHitTest) //Not supported for UIPainter, use TransfromPoint instead.
  1282. return new Vector2(float.NaN, float.NaN);
  1283. Vector3 screePoint = wsc.GetRenderCamera().WorldToScreenPoint(worldPoint);
  1284. return new Vector2(screePoint.x, Stage.inst.size.y - screePoint.y);
  1285. }
  1286. else
  1287. {
  1288. point = Stage.inst.cachedTransform.InverseTransformPoint(worldPoint);
  1289. point.y = -point.y;
  1290. return point;
  1291. }
  1292. }
  1293. /// <summary>
  1294. /// 转换世界坐标点到等效的本地xy平面的点。等效的意思是他们在屏幕方向看到的位置一样。
  1295. /// 返回的点是在对象的本地坐标空间,且z=0
  1296. /// </summary>
  1297. /// <param name="worldPoint"></param>
  1298. /// <param name="direction"></param>
  1299. /// <returns></returns>
  1300. public Vector3 WorldToLocal(Vector3 worldPoint, Vector3 direction)
  1301. {
  1302. Vector3 localPoint = this.cachedTransform.InverseTransformPoint(worldPoint);
  1303. if (localPoint.z != 0) //如果对象绕x轴或y轴旋转过,或者对象是在透视相机,那么z值可能不为0,
  1304. {
  1305. //将世界坐标的摄影机方向在本地空间上投射,求出与xy平面的交点
  1306. direction = this.cachedTransform.InverseTransformDirection(direction);
  1307. float distOnLine = Vector3.Dot(Vector3.zero - localPoint, Vector3.forward) / Vector3.Dot(direction, Vector3.forward);
  1308. if (float.IsInfinity(distOnLine))
  1309. return Vector2.zero;
  1310. localPoint = localPoint + direction * distOnLine;
  1311. }
  1312. else if (_vertexMatrix != null)
  1313. {
  1314. Vector3 center = _vertexMatrix.cameraPos;
  1315. center.z = 0;
  1316. center -= _vertexMatrix.matrix.MultiplyPoint(center);
  1317. Matrix4x4 mm = _vertexMatrix.matrix.inverse;
  1318. localPoint -= center;
  1319. localPoint = mm.MultiplyPoint(localPoint);
  1320. Vector3 camPos = mm.MultiplyPoint(_vertexMatrix.cameraPos);
  1321. Vector3 vec = localPoint - camPos;
  1322. float lambda = -camPos.z / vec.z;
  1323. localPoint = camPos + lambda * vec;
  1324. localPoint.z = 0;
  1325. }
  1326. localPoint.y = -localPoint.y;
  1327. return localPoint;
  1328. }
  1329. /// <summary>
  1330. ///
  1331. /// </summary>
  1332. /// <param name="localPoint"></param>
  1333. /// <returns></returns>
  1334. public Vector3 LocalToWorld(Vector3 localPoint)
  1335. {
  1336. localPoint.y = -localPoint.y;
  1337. if (_vertexMatrix != null)
  1338. {
  1339. Vector3 center = _vertexMatrix.cameraPos;
  1340. center.z = 0;
  1341. center -= _vertexMatrix.matrix.MultiplyPoint(center);
  1342. localPoint = _vertexMatrix.matrix.MultiplyPoint(localPoint);
  1343. localPoint += center;
  1344. Vector3 camPos = _vertexMatrix.cameraPos;
  1345. Vector3 vec = localPoint - camPos;
  1346. float lambda = -camPos.z / vec.z;
  1347. localPoint = camPos + lambda * vec;
  1348. localPoint.z = 0;
  1349. }
  1350. return this.cachedTransform.TransformPoint(localPoint);
  1351. }
  1352. /// <summary>
  1353. ///
  1354. /// </summary>
  1355. /// <param name="point"></param>
  1356. /// <param name="targetSpace">null if to world space</param>
  1357. /// <returns></returns>
  1358. public Vector2 TransformPoint(Vector2 point, DisplayObject targetSpace)
  1359. {
  1360. if (targetSpace == this)
  1361. return point;
  1362. point = LocalToWorld(point);
  1363. if (targetSpace != null)
  1364. point = targetSpace.WorldToLocal(point, Vector3.back);
  1365. return point;
  1366. }
  1367. /// <summary>
  1368. ///
  1369. /// </summary>
  1370. /// <param name="rect"></param>
  1371. /// <param name="targetSpace">null if to world space</param>
  1372. /// <returns></returns>
  1373. public Rect TransformRect(Rect rect, DisplayObject targetSpace)
  1374. {
  1375. if (targetSpace == this)
  1376. return rect;
  1377. if (targetSpace == parent && _rotation.z == 0) // optimization
  1378. {
  1379. Vector3 vec = cachedTransform.localScale;
  1380. return new Rect((this.x + rect.x) * vec.x, (this.y + rect.y) * vec.y,
  1381. rect.width * vec.x, rect.height * vec.y);
  1382. }
  1383. else
  1384. {
  1385. Vector4 vec4 = new Vector4(float.MaxValue, float.MaxValue, float.MinValue, float.MinValue);
  1386. TransformRectPoint(rect.xMin, rect.yMin, targetSpace, ref vec4);
  1387. TransformRectPoint(rect.xMax, rect.yMin, targetSpace, ref vec4);
  1388. TransformRectPoint(rect.xMin, rect.yMax, targetSpace, ref vec4);
  1389. TransformRectPoint(rect.xMax, rect.yMax, targetSpace, ref vec4);
  1390. return Rect.MinMaxRect(vec4.x, vec4.y, vec4.z, vec4.w);
  1391. }
  1392. }
  1393. protected void TransformRectPoint(float px, float py, DisplayObject targetSpace, ref Vector4 vec4)
  1394. {
  1395. Vector2 v = TransformPoint(new Vector2(px, py), targetSpace);
  1396. if (vec4.x > v.x) vec4.x = v.x;
  1397. if (vec4.z < v.x) vec4.z = v.x;
  1398. if (vec4.y > v.y) vec4.y = v.y;
  1399. if (vec4.w < v.y) vec4.w = v.y;
  1400. }
  1401. /// <summary>
  1402. ///
  1403. /// </summary>
  1404. public void RemoveFromParent()
  1405. {
  1406. if (parent != null)
  1407. parent.RemoveChild(this);
  1408. }
  1409. /// <summary>
  1410. ///
  1411. /// </summary>
  1412. public void InvalidateBatchingState()
  1413. {
  1414. if (parent != null)
  1415. parent.InvalidateBatchingState(true);
  1416. }
  1417. virtual public void Update(UpdateContext context)
  1418. {
  1419. if (_checkPixelPerfect != 0)
  1420. {
  1421. if (_rotation == Vector3.zero)
  1422. {
  1423. Vector3 v = cachedTransform.localPosition;
  1424. v.x = Mathf.Round(v.x);
  1425. v.y = Mathf.Round(v.y);
  1426. _pixelPerfectAdjustment = v - cachedTransform.localPosition;
  1427. if (_pixelPerfectAdjustment != Vector3.zero)
  1428. cachedTransform.localPosition = v;
  1429. }
  1430. _checkPixelPerfect = 0;
  1431. }
  1432. if (graphics != null)
  1433. graphics.Update(context, context.alpha * _alpha, context.grayed | _grayed);
  1434. if (_paintingMode != 0)
  1435. {
  1436. UpdatePainting();
  1437. //如果是容器,Capture要等到Container.Update的最后执行,因为容器中可能也有需要Capture的内容,要等他们完成后再进行容器的Capture。
  1438. if (!(this is Container))
  1439. {
  1440. if ((_flags & Flags.CacheAsBitmap) == 0 || _paintingInfo.flag != 2)
  1441. UpdateContext.OnEnd += _paintingInfo.captureDelegate;
  1442. }
  1443. paintingGraphics.Update(context, 1, false);
  1444. }
  1445. if (_filter != null)
  1446. _filter.Update();
  1447. Stats.ObjectCount++;
  1448. }
  1449. void UpdatePainting()
  1450. {
  1451. NTexture paintingTexture = paintingGraphics.texture;
  1452. if (paintingTexture != null && paintingTexture.disposed) //Texture可能已被Stage.MonitorTexture销毁
  1453. {
  1454. paintingTexture = null;
  1455. _paintingInfo.flag = 1;
  1456. }
  1457. if (_paintingInfo.flag == 1)
  1458. {
  1459. _paintingInfo.flag = 0;
  1460. //从优化考虑,决定使用绘画模式的容器都需要明确指定大小,而不是自动计算包围。这在UI使用上并没有问题,因为组件总是有固定大小的
  1461. Margin extend = _paintingInfo.extend;
  1462. paintingGraphics.contentRect = new Rect(-extend.left, -extend.top, _contentRect.width + extend.left + extend.right, _contentRect.height + extend.top + extend.bottom);
  1463. int textureWidth = Mathf.RoundToInt(paintingGraphics.contentRect.width * _paintingInfo.scale);
  1464. int textureHeight = Mathf.RoundToInt(paintingGraphics.contentRect.height * _paintingInfo.scale);
  1465. if (paintingTexture == null || paintingTexture.width != textureWidth || paintingTexture.height != textureHeight)
  1466. {
  1467. if (paintingTexture != null)
  1468. paintingTexture.Dispose();
  1469. if (textureWidth > 0 && textureHeight > 0)
  1470. {
  1471. paintingTexture = new NTexture(CaptureCamera.CreateRenderTexture(textureWidth, textureHeight, UIConfig.depthSupportForPaintingMode));
  1472. Stage.inst.MonitorTexture(paintingTexture);
  1473. }
  1474. else
  1475. paintingTexture = null;
  1476. paintingGraphics.texture = paintingTexture;
  1477. }
  1478. }
  1479. if (paintingTexture != null)
  1480. paintingTexture.lastActive = Time.time;
  1481. }
  1482. void Capture()
  1483. {
  1484. if (paintingGraphics.texture == null)
  1485. return;
  1486. Vector2 offset = new Vector2(_paintingInfo.extend.left, _paintingInfo.extend.top);
  1487. CaptureCamera.Capture(this, (RenderTexture)paintingGraphics.texture.nativeTexture, paintingGraphics.contentRect.height, offset);
  1488. _paintingInfo.flag = 2; //2表示已完成一次Capture
  1489. if (onPaint != null)
  1490. onPaint();
  1491. }
  1492. /// <summary>
  1493. /// 为对象设置一个默认的父Transform。当对象不在显示列表里时,它的GameObject挂到哪里。
  1494. /// </summary>
  1495. public Transform home
  1496. {
  1497. get { return _home; }
  1498. set
  1499. {
  1500. _home = value;
  1501. if (value != null && cachedTransform.parent == null)
  1502. cachedTransform.SetParent(value, false);
  1503. }
  1504. }
  1505. void UpdateHierarchy()
  1506. {
  1507. if ((_flags & Flags.GameObjectDisposed) != 0)
  1508. return;
  1509. if ((_flags & Flags.UserGameObject) != 0)
  1510. {
  1511. //we dont change transform parent of this object
  1512. if (gameObject != null)
  1513. {
  1514. if (parent != null && visible)
  1515. gameObject.SetActive(true);
  1516. else
  1517. gameObject.SetActive(false);
  1518. }
  1519. }
  1520. else if (parent != null)
  1521. {
  1522. cachedTransform.SetParent(parent.cachedTransform, false);
  1523. if (_visible)
  1524. gameObject.SetActive(true);
  1525. int layerValue = parent.gameObject.layer;
  1526. if (parent._paintingMode != 0)
  1527. layerValue = CaptureCamera.hiddenLayer;
  1528. SetLayer(layerValue, true);
  1529. }
  1530. else if ((_flags & Flags.Disposed) == 0 && this.gameObject != null && !StageEngine.beingQuit)
  1531. {
  1532. if (Application.isPlaying)
  1533. {
  1534. if (gOwner == null || gOwner.parent == null)//如果gOwner还有parent的话,说明只是暂时的隐藏
  1535. {
  1536. cachedTransform.SetParent(_home, false);
  1537. if (_home == null)
  1538. UnityEngine.Object.DontDestroyOnLoad(this.gameObject);
  1539. }
  1540. }
  1541. gameObject.SetActive(false);
  1542. }
  1543. }
  1544. virtual protected bool SetLayer(int value, bool fromParent)
  1545. {
  1546. if ((_flags & Flags.LayerSet) != 0) //setted
  1547. {
  1548. if (fromParent)
  1549. return false;
  1550. }
  1551. else if ((_flags & Flags.LayerFromParent) != 0) //inherit from parent
  1552. {
  1553. if (!fromParent)
  1554. _flags |= Flags.LayerSet;
  1555. }
  1556. else
  1557. {
  1558. if (fromParent)
  1559. _flags |= Flags.LayerFromParent;
  1560. else
  1561. _flags |= Flags.LayerSet;
  1562. }
  1563. if (_paintingMode > 0)
  1564. paintingGraphics.gameObject.layer = value;
  1565. else if (gameObject.layer != value)
  1566. {
  1567. gameObject.layer = value;
  1568. if ((this is Container))
  1569. {
  1570. int cnt = ((Container)this).numChildren;
  1571. for (int i = 0; i < cnt; i++)
  1572. {
  1573. DisplayObject child = ((Container)this).GetChildAt(i);
  1574. child.SetLayer(value, true);
  1575. }
  1576. }
  1577. }
  1578. return true;
  1579. }
  1580. internal void _SetLayerDirect(int value)
  1581. {
  1582. if (_paintingMode > 0)
  1583. paintingGraphics.gameObject.layer = value;
  1584. else
  1585. gameObject.layer = value;
  1586. }
  1587. virtual public void Dispose()
  1588. {
  1589. if ((_flags & Flags.Disposed) != 0)
  1590. return;
  1591. _flags |= Flags.Disposed;
  1592. RemoveFromParent();
  1593. RemoveEventListeners();
  1594. if (graphics != null)
  1595. graphics.Dispose();
  1596. if (_filter != null)
  1597. _filter.Dispose();
  1598. if (paintingGraphics != null)
  1599. {
  1600. if (paintingGraphics.texture != null)
  1601. paintingGraphics.texture.Dispose();
  1602. paintingGraphics.Dispose();
  1603. if (paintingGraphics.gameObject != this.gameObject)
  1604. {
  1605. if (Application.isPlaying)
  1606. UnityEngine.Object.Destroy(paintingGraphics.gameObject);
  1607. else
  1608. UnityEngine.Object.DestroyImmediate(paintingGraphics.gameObject);
  1609. }
  1610. }
  1611. DestroyGameObject();
  1612. }
  1613. internal void DisplayDisposedWarning()
  1614. {
  1615. if ((_flags & Flags.DisposedWarning) == 0)
  1616. {
  1617. _flags |= Flags.DisposedWarning;
  1618. StringBuilder sb = new StringBuilder();
  1619. sb.Append("DisplayObject is still in use but GameObject was disposed. (");
  1620. if (gOwner != null)
  1621. {
  1622. sb.Append("type=").Append(gOwner.GetType().Name).Append(", x=").Append(gOwner.x).Append(", y=").Append(gOwner.y).Append(", name=").Append(gOwner.name);
  1623. if (gOwner.packageItem != null)
  1624. sb.Append(", res=" + gOwner.packageItem.name);
  1625. }
  1626. else
  1627. {
  1628. sb.Append("id=").Append(id).Append(", type=").Append(this.GetType().Name).Append(", name=").Append(name);
  1629. }
  1630. sb.Append(")");
  1631. Debug.LogError(sb.ToString());
  1632. }
  1633. }
  1634. protected internal class PaintingInfo
  1635. {
  1636. public Action captureDelegate; //缓存这个delegate,可以防止Capture状态下每帧104B的GC
  1637. public Margin extend;
  1638. public float scale;
  1639. public int flag;
  1640. }
  1641. [Flags]
  1642. protected internal enum Flags
  1643. {
  1644. Disposed = 1,
  1645. UserGameObject = 2,
  1646. TouchDisabled = 4,
  1647. OutlineChanged = 8,
  1648. UpdatingSize = 0x10,
  1649. WidthChanged = 0x20,
  1650. HeightChanged = 0x40,
  1651. PixelPerfect = 0x80,
  1652. LayerSet = 0x100,
  1653. LayerFromParent = 0x200,
  1654. NotFocusable = 0x400,
  1655. TabStop = 0x800,
  1656. TabStopChildren = 0x1000,
  1657. FairyBatching = 0x2000,
  1658. BatchingRequested = 0x4000,
  1659. BatchingRoot = 0x8000,
  1660. SkipBatching = 0x10000,
  1661. CacheAsBitmap = 0x20000,
  1662. GameObjectDisposed = 0x40000,
  1663. DisposedWarning = 0x80000
  1664. }
  1665. }
  1666. /// <summary>
  1667. ///
  1668. /// </summary>
  1669. public class DisplayObjectInfo : MonoBehaviour
  1670. {
  1671. /// <summary>
  1672. ///
  1673. /// /// </summary>
  1674. [System.NonSerialized]
  1675. public DisplayObject displayObject;
  1676. private void OnDestroy()
  1677. {
  1678. if (displayObject != null)
  1679. displayObject._flags |= DisplayObject.Flags.GameObjectDisposed;
  1680. }
  1681. }
  1682. }