GLoader3D.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561
  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. }
  219. }
  220. /// <summary>
  221. ///
  222. /// </summary>
  223. /// <value></value>
  224. public string skinName
  225. {
  226. get { return _skinName; }
  227. set
  228. {
  229. _skinName = value;
  230. OnChange("skinName");
  231. }
  232. }
  233. /// <summary>
  234. ///
  235. /// </summary>
  236. public Material material
  237. {
  238. get { return _content.material; }
  239. set { _content.material = value; }
  240. }
  241. /// <summary>
  242. ///
  243. /// </summary>
  244. public string shader
  245. {
  246. get { return _content.shader; }
  247. set { _content.shader = value; }
  248. }
  249. /// <summary>
  250. ///
  251. /// </summary>
  252. public Color color
  253. {
  254. get { return _color; }
  255. set
  256. {
  257. if (_color != value)
  258. {
  259. _color = value;
  260. UpdateGear(4);
  261. OnChange("color");
  262. }
  263. }
  264. }
  265. /// <summary>
  266. ///
  267. /// </summary>
  268. public GameObject wrapTarget
  269. {
  270. get { return _content.wrapTarget; }
  271. }
  272. /// <summary>
  273. ///
  274. /// </summary>
  275. /// <param name="gameObject"></param>
  276. /// <param name="cloneMaterial"></param>
  277. /// <param name="width"></param>
  278. /// <param name="height"></param>
  279. public void SetWrapTarget(GameObject gameObject, bool cloneMaterial, int width, int height)
  280. {
  281. _content.SetWrapTarget(gameObject, cloneMaterial);
  282. _content.SetSize(width, height);
  283. sourceWidth = width;
  284. sourceHeight = height;
  285. UpdateLayout();
  286. }
  287. override public IFilter filter
  288. {
  289. get { return _content.filter; }
  290. set { _content.filter = value; }
  291. }
  292. override public BlendMode blendMode
  293. {
  294. get { return _content.blendMode; }
  295. set { _content.blendMode = value; }
  296. }
  297. /// <summary>
  298. ///
  299. /// </summary>
  300. protected void LoadContent()
  301. {
  302. ClearContent();
  303. if (string.IsNullOrEmpty(_url))
  304. return;
  305. _contentItem = UIPackage.GetItemByURL(_url);
  306. if (_contentItem != null)
  307. {
  308. _contentItem = _contentItem.getBranch();
  309. _contentItem = _contentItem.getHighResolution();
  310. _contentItem.Load();
  311. if (_contentItem.type == PackageItemType.Spine)
  312. {
  313. #if FAIRYGUI_SPINE
  314. LoadSpine();
  315. #endif
  316. }
  317. else if (_contentItem.type == PackageItemType.DragoneBones)
  318. {
  319. #if FAIRYGUI_DRAGONBONES
  320. LoadDragonBones();
  321. #endif
  322. }
  323. }
  324. else
  325. LoadExternal();
  326. }
  327. virtual protected void OnChange(string propertyName)
  328. {
  329. if (_contentItem == null)
  330. return;
  331. if (_contentItem.type == PackageItemType.Spine)
  332. {
  333. #if FAIRYGUI_SPINE
  334. OnChangeSpine(propertyName);
  335. #endif
  336. }
  337. else if (_contentItem.type == PackageItemType.DragoneBones)
  338. {
  339. #if FAIRYGUI_DRAGONBONES
  340. OnChangeDragonBones(propertyName);
  341. #endif
  342. }
  343. }
  344. virtual protected void LoadExternal()
  345. {
  346. }
  347. virtual protected void FreeExternal()
  348. {
  349. GameObject.DestroyImmediate(_content.wrapTarget);
  350. }
  351. protected void UpdateLayout()
  352. {
  353. float contentWidth = sourceWidth;
  354. float contentHeight = sourceHeight;
  355. if (_autoSize)
  356. {
  357. _updatingLayout = true;
  358. if (contentWidth == 0)
  359. contentWidth = 50;
  360. if (contentHeight == 0)
  361. contentHeight = 30;
  362. SetSize(contentWidth, contentHeight);
  363. _updatingLayout = false;
  364. if (_width == contentWidth && _height == contentHeight)
  365. {
  366. _content.SetXY(0, 0);
  367. _content.SetScale(1, 1);
  368. InvalidateBatchingState();
  369. return;
  370. }
  371. //如果不相等,可能是由于大小限制造成的,要后续处理
  372. }
  373. float sx = 1, sy = 1;
  374. if (_fill != FillType.None)
  375. {
  376. sx = this.width / sourceWidth;
  377. sy = this.height / sourceHeight;
  378. if (sx != 1 || sy != 1)
  379. {
  380. if (_fill == FillType.ScaleMatchHeight)
  381. sx = sy;
  382. else if (_fill == FillType.ScaleMatchWidth)
  383. sy = sx;
  384. else if (_fill == FillType.Scale)
  385. {
  386. if (sx > sy)
  387. sx = sy;
  388. else
  389. sy = sx;
  390. }
  391. else if (_fill == FillType.ScaleNoBorder)
  392. {
  393. if (sx > sy)
  394. sy = sx;
  395. else
  396. sx = sy;
  397. }
  398. if (_shrinkOnly)
  399. {
  400. if (sx > 1)
  401. sx = 1;
  402. if (sy > 1)
  403. sy = 1;
  404. }
  405. contentWidth = sourceWidth * sx;
  406. contentHeight = sourceHeight * sy;
  407. }
  408. }
  409. _content.SetScale(sx, sy);
  410. float nx;
  411. float ny;
  412. if (_align == AlignType.Center)
  413. nx = (this.width - contentWidth) / 2;
  414. else if (_align == AlignType.Right)
  415. nx = this.width - contentWidth;
  416. else
  417. nx = 0;
  418. if (_verticalAlign == VertAlignType.Middle)
  419. ny = (this.height - contentHeight) / 2;
  420. else if (_verticalAlign == VertAlignType.Bottom)
  421. ny = this.height - contentHeight;
  422. else
  423. ny = 0;
  424. _content.SetXY(nx, ny);
  425. InvalidateBatchingState();
  426. }
  427. protected void ClearContent()
  428. {
  429. if (_content.wrapTarget != null)
  430. {
  431. if (_contentItem != null)
  432. {
  433. if (_contentItem.type == PackageItemType.Spine)
  434. {
  435. #if FAIRYGUI_SPINE
  436. FreeSpine();
  437. #endif
  438. }
  439. else if (_contentItem.type == PackageItemType.DragoneBones)
  440. {
  441. #if FAIRYGUI_DRAGONBONES
  442. FreeDragonBones();
  443. #endif
  444. }
  445. }
  446. else
  447. FreeExternal();
  448. }
  449. _content.wrapTarget = null;
  450. _contentItem = null;
  451. }
  452. protected void OnUpdateContent(UpdateContext context)
  453. {
  454. if (_contentItem == null)
  455. return;
  456. if (_contentItem.type == PackageItemType.Spine)
  457. {
  458. #if FAIRYGUI_SPINE
  459. OnUpdateSpine(context);
  460. #endif
  461. }
  462. else if (_contentItem.type == PackageItemType.DragoneBones)
  463. {
  464. #if FAIRYGUI_DRAGONBONES
  465. OnUpdateDragonBones(context);
  466. #endif
  467. }
  468. }
  469. override protected void HandleSizeChanged()
  470. {
  471. base.HandleSizeChanged();
  472. if (!_updatingLayout)
  473. UpdateLayout();
  474. }
  475. override public void Setup_BeforeAdd(ByteBuffer buffer, int beginPos)
  476. {
  477. base.Setup_BeforeAdd(buffer, beginPos);
  478. buffer.Seek(beginPos, 5);
  479. _url = buffer.ReadS();
  480. _align = (AlignType)buffer.ReadByte();
  481. _verticalAlign = (VertAlignType)buffer.ReadByte();
  482. _fill = (FillType)buffer.ReadByte();
  483. _shrinkOnly = buffer.ReadBool();
  484. _autoSize = buffer.ReadBool();
  485. _animationName = buffer.ReadS();
  486. _skinName = buffer.ReadS();
  487. _playing = buffer.ReadBool();
  488. _frame = buffer.ReadInt();
  489. _loop = buffer.ReadBool();
  490. if (buffer.ReadBool())
  491. this.color = buffer.ReadColor(); //color
  492. if (!string.IsNullOrEmpty(_url))
  493. LoadContent();
  494. }
  495. }
  496. }