GLoader3D.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566
  1. using UnityEngine;
  2. using FairyGUI.Utils;
  3. namespace FairyGUI
  4. {
  5. /// <summary>
  6. ///
  7. /// </summary>
  8. public partial class GLoader3D : GObject, IAnimationGear, IColorGear
  9. {
  10. string _url;
  11. AlignType _align;
  12. VertAlignType _verticalAlign;
  13. bool _autoSize;
  14. FillType _fill;
  15. bool _shrinkOnly;
  16. string _animationName;
  17. string _skinName;
  18. bool _playing;
  19. int _frame;
  20. bool _loop;
  21. bool _updatingLayout;
  22. Color _color;
  23. protected PackageItem _contentItem;
  24. protected GoWrapper _content;
  25. public GLoader3D()
  26. {
  27. _url = string.Empty;
  28. _align = AlignType.Left;
  29. _verticalAlign = VertAlignType.Top;
  30. _playing = true;
  31. _color = Color.white;
  32. }
  33. override protected void CreateDisplayObject()
  34. {
  35. displayObject = new Container("GLoader3D");
  36. displayObject.gOwner = this;
  37. _content = new GoWrapper();
  38. _content.onUpdate += OnUpdateContent;
  39. ((Container)displayObject).AddChild(_content);
  40. ((Container)displayObject).opaque = true;
  41. }
  42. override public void Dispose()
  43. {
  44. _content.Dispose();
  45. base.Dispose();
  46. }
  47. /// <summary>
  48. ///
  49. /// </summary>
  50. public string url
  51. {
  52. get { return _url; }
  53. set
  54. {
  55. if (_url == value)
  56. return;
  57. ClearContent();
  58. _url = value;
  59. LoadContent();
  60. UpdateGear(7);
  61. }
  62. }
  63. override public string icon
  64. {
  65. get { return _url; }
  66. set { this.url = value; }
  67. }
  68. /// <summary>
  69. ///
  70. /// </summary>
  71. public AlignType align
  72. {
  73. get { return _align; }
  74. set
  75. {
  76. if (_align != value)
  77. {
  78. _align = value;
  79. UpdateLayout();
  80. }
  81. }
  82. }
  83. /// <summary>
  84. ///
  85. /// </summary>
  86. public VertAlignType verticalAlign
  87. {
  88. get { return _verticalAlign; }
  89. set
  90. {
  91. if (_verticalAlign != value)
  92. {
  93. _verticalAlign = value;
  94. UpdateLayout();
  95. }
  96. }
  97. }
  98. /// <summary>
  99. ///
  100. /// </summary>
  101. public FillType fill
  102. {
  103. get { return _fill; }
  104. set
  105. {
  106. if (_fill != value)
  107. {
  108. _fill = value;
  109. UpdateLayout();
  110. }
  111. }
  112. }
  113. /// <summary>
  114. ///
  115. /// </summary>
  116. public bool shrinkOnly
  117. {
  118. get { return _shrinkOnly; }
  119. set
  120. {
  121. if (_shrinkOnly != value)
  122. {
  123. _shrinkOnly = value;
  124. UpdateLayout();
  125. }
  126. }
  127. }
  128. /// <summary>
  129. ///
  130. /// </summary>
  131. public bool autoSize
  132. {
  133. get { return _autoSize; }
  134. set
  135. {
  136. if (_autoSize != value)
  137. {
  138. _autoSize = value;
  139. UpdateLayout();
  140. }
  141. }
  142. }
  143. public bool playing
  144. {
  145. get { return _playing; }
  146. set
  147. {
  148. if (_playing != value)
  149. {
  150. _playing = value;
  151. OnChange("playing");
  152. UpdateGear(5);
  153. }
  154. }
  155. }
  156. public int frame
  157. {
  158. get { return _frame; }
  159. set
  160. {
  161. if (_frame != value)
  162. {
  163. _frame = value;
  164. OnChange("frame");
  165. UpdateGear(5);
  166. }
  167. }
  168. }
  169. /// <summary>
  170. /// Not implemented
  171. /// </summary>
  172. public float timeScale
  173. {
  174. get;
  175. set;
  176. }
  177. /// <summary>
  178. /// Not implemented
  179. /// </summary>
  180. public bool ignoreEngineTimeScale
  181. {
  182. get;
  183. set;
  184. }
  185. /// <summary>
  186. /// Not implemented
  187. /// </summary>
  188. /// <param name="time"></param>
  189. public void Advance(float time)
  190. {
  191. }
  192. /// <summary>
  193. ///
  194. /// </summary>
  195. public bool loop
  196. {
  197. get { return _loop; }
  198. set
  199. {
  200. if (_loop != value)
  201. {
  202. _loop = value;
  203. OnChange("loop");
  204. }
  205. }
  206. }
  207. /// <summary>
  208. ///
  209. /// </summary>
  210. /// <value></value>
  211. public string animationName
  212. {
  213. get { return _animationName; }
  214. set
  215. {
  216. _animationName = value;
  217. OnChange("animationName");
  218. UpdateGear(5);
  219. }
  220. }
  221. /// <summary>
  222. ///
  223. /// </summary>
  224. /// <value></value>
  225. public string skinName
  226. {
  227. get { return _skinName; }
  228. set
  229. {
  230. _skinName = value;
  231. OnChange("skinName");
  232. UpdateGear(5);
  233. }
  234. }
  235. /// <summary>
  236. ///
  237. /// </summary>
  238. public Material material
  239. {
  240. get { return _content.material; }
  241. set { _content.material = value; }
  242. }
  243. /// <summary>
  244. ///
  245. /// </summary>
  246. public string shader
  247. {
  248. get { return _content.shader; }
  249. set { _content.shader = value; }
  250. }
  251. /// <summary>
  252. ///
  253. /// </summary>
  254. public Color color
  255. {
  256. get { return _color; }
  257. set
  258. {
  259. if (_color != value)
  260. {
  261. _color = value;
  262. UpdateGear(4);
  263. OnChange("color");
  264. }
  265. }
  266. }
  267. /// <summary>
  268. ///
  269. /// </summary>
  270. public GameObject wrapTarget
  271. {
  272. get { return _content.wrapTarget; }
  273. }
  274. /// <summary>
  275. ///
  276. /// </summary>
  277. /// <param name="gameObject"></param>
  278. /// <param name="cloneMaterial"></param>
  279. /// <param name="width"></param>
  280. /// <param name="height"></param>
  281. public void SetWrapTarget(GameObject gameObject, bool cloneMaterial, int width, int height)
  282. {
  283. _content.SetWrapTarget(gameObject, cloneMaterial);
  284. _content.SetSize(width, height);
  285. sourceWidth = width;
  286. sourceHeight = height;
  287. UpdateLayout();
  288. }
  289. override public IFilter filter
  290. {
  291. get { return _content.filter; }
  292. set { _content.filter = value; }
  293. }
  294. override public BlendMode blendMode
  295. {
  296. get { return _content.blendMode; }
  297. set { _content.blendMode = value; }
  298. }
  299. /// <summary>
  300. ///
  301. /// </summary>
  302. protected void LoadContent()
  303. {
  304. ClearContent();
  305. if (string.IsNullOrEmpty(_url))
  306. return;
  307. _contentItem = UIPackage.GetItemByURL(_url);
  308. if (_contentItem != null)
  309. {
  310. _contentItem = _contentItem.getBranch();
  311. _contentItem = _contentItem.getHighResolution();
  312. _contentItem.Load();
  313. if (_contentItem.type == PackageItemType.Spine)
  314. {
  315. #if FAIRYGUI_SPINE
  316. LoadSpine();
  317. #endif
  318. }
  319. else if (_contentItem.type == PackageItemType.DragoneBones)
  320. {
  321. #if FAIRYGUI_DRAGONBONES
  322. LoadDragonBones();
  323. #endif
  324. }
  325. }
  326. else
  327. LoadExternal();
  328. }
  329. virtual protected void OnChange(string propertyName)
  330. {
  331. if (_contentItem == null)
  332. return;
  333. if (_contentItem.type == PackageItemType.Spine)
  334. {
  335. #if FAIRYGUI_SPINE
  336. OnChangeSpine(propertyName);
  337. #endif
  338. }
  339. else if (_contentItem.type == PackageItemType.DragoneBones)
  340. {
  341. #if FAIRYGUI_DRAGONBONES
  342. OnChangeDragonBones(propertyName);
  343. #endif
  344. }
  345. }
  346. virtual protected void LoadExternal()
  347. {
  348. }
  349. virtual protected void FreeExternal()
  350. {
  351. GameObject.DestroyImmediate(_content.wrapTarget);
  352. }
  353. protected void UpdateLayout()
  354. {
  355. if (sourceWidth == 0 || sourceHeight == 0)
  356. return;
  357. float contentWidth = sourceWidth;
  358. float contentHeight = sourceHeight;
  359. if (_autoSize)
  360. {
  361. _updatingLayout = true;
  362. if (contentWidth == 0)
  363. contentWidth = 50;
  364. if (contentHeight == 0)
  365. contentHeight = 30;
  366. SetSize(contentWidth, contentHeight);
  367. _updatingLayout = false;
  368. if (_width == contentWidth && _height == contentHeight)
  369. {
  370. _content.SetXY(0, 0);
  371. _content.SetScale(1, 1);
  372. InvalidateBatchingState();
  373. return;
  374. }
  375. //如果不相等,可能是由于大小限制造成的,要后续处理
  376. }
  377. float sx = 1, sy = 1;
  378. if (_fill != FillType.None)
  379. {
  380. sx = this.width / sourceWidth;
  381. sy = this.height / sourceHeight;
  382. if (sx != 1 || sy != 1)
  383. {
  384. if (_fill == FillType.ScaleMatchHeight)
  385. sx = sy;
  386. else if (_fill == FillType.ScaleMatchWidth)
  387. sy = sx;
  388. else if (_fill == FillType.Scale)
  389. {
  390. if (sx > sy)
  391. sx = sy;
  392. else
  393. sy = sx;
  394. }
  395. else if (_fill == FillType.ScaleNoBorder)
  396. {
  397. if (sx > sy)
  398. sy = sx;
  399. else
  400. sx = sy;
  401. }
  402. if (_shrinkOnly)
  403. {
  404. if (sx > 1)
  405. sx = 1;
  406. if (sy > 1)
  407. sy = 1;
  408. }
  409. contentWidth = sourceWidth * sx;
  410. contentHeight = sourceHeight * sy;
  411. }
  412. }
  413. _content.SetScale(sx, sy);
  414. float nx;
  415. float ny;
  416. if (_align == AlignType.Center)
  417. nx = (this.width - contentWidth) / 2;
  418. else if (_align == AlignType.Right)
  419. nx = this.width - contentWidth;
  420. else
  421. nx = 0;
  422. if (_verticalAlign == VertAlignType.Middle)
  423. ny = (this.height - contentHeight) / 2;
  424. else if (_verticalAlign == VertAlignType.Bottom)
  425. ny = this.height - contentHeight;
  426. else
  427. ny = 0;
  428. _content.SetXY(nx, ny);
  429. InvalidateBatchingState();
  430. }
  431. protected void ClearContent()
  432. {
  433. if (_content.wrapTarget != null)
  434. {
  435. if (_contentItem != null)
  436. {
  437. if (_contentItem.type == PackageItemType.Spine)
  438. {
  439. #if FAIRYGUI_SPINE
  440. FreeSpine();
  441. #endif
  442. }
  443. else if (_contentItem.type == PackageItemType.DragoneBones)
  444. {
  445. #if FAIRYGUI_DRAGONBONES
  446. FreeDragonBones();
  447. #endif
  448. }
  449. }
  450. else
  451. FreeExternal();
  452. }
  453. _content.wrapTarget = null;
  454. _contentItem = null;
  455. }
  456. protected void OnUpdateContent(UpdateContext context)
  457. {
  458. if (_contentItem == null)
  459. return;
  460. if (_contentItem.type == PackageItemType.Spine)
  461. {
  462. #if FAIRYGUI_SPINE
  463. OnUpdateSpine(context);
  464. #endif
  465. }
  466. else if (_contentItem.type == PackageItemType.DragoneBones)
  467. {
  468. #if FAIRYGUI_DRAGONBONES
  469. OnUpdateDragonBones(context);
  470. #endif
  471. }
  472. }
  473. override protected void HandleSizeChanged()
  474. {
  475. base.HandleSizeChanged();
  476. if (!_updatingLayout)
  477. UpdateLayout();
  478. }
  479. override public void Setup_BeforeAdd(ByteBuffer buffer, int beginPos)
  480. {
  481. base.Setup_BeforeAdd(buffer, beginPos);
  482. buffer.Seek(beginPos, 5);
  483. _url = buffer.ReadS();
  484. _align = (AlignType)buffer.ReadByte();
  485. _verticalAlign = (VertAlignType)buffer.ReadByte();
  486. _fill = (FillType)buffer.ReadByte();
  487. _shrinkOnly = buffer.ReadBool();
  488. _autoSize = buffer.ReadBool();
  489. _animationName = buffer.ReadS();
  490. _skinName = buffer.ReadS();
  491. _playing = buffer.ReadBool();
  492. _frame = buffer.ReadInt();
  493. _loop = buffer.ReadBool();
  494. if (buffer.ReadBool())
  495. this.color = buffer.ReadColor(); //color
  496. if (!string.IsNullOrEmpty(_url))
  497. LoadContent();
  498. }
  499. }
  500. }