DisplayUGUI.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706
  1. // UnityEngine.UI was moved to a package in 2019.2.0
  2. // Unfortunately no way to test for this across all Unity versions yet
  3. // You can set up the asmdef to reference the new package, but the package doesn't
  4. // existing in Unity 2017 etc, and it throws an error due to missing reference
  5. #define AVPRO_PACKAGE_UNITYUI
  6. #if (UNITY_2019_2_OR_NEWER && AVPRO_PACKAGE_UNITYUI) || (!UNITY_2019_2_OR_NEWER)
  7. #if UNITY_EDITOR_OSX || UNITY_STANDALONE_OSX || UNITY_IOS || UNITY_TVOS
  8. #define UNITY_PLATFORM_SUPPORTS_YPCBCR
  9. #endif
  10. #if UNITY_EDITOR || UNITY_STANDALONE_OSX || UNITY_STANDALONE_WIN || UNITY_IOS || UNITY_TVOS || UNITY_ANDROID || (UNITY_WEBGL && UNITY_2017_2_OR_NEWER)
  11. #define UNITY_PLATFORM_SUPPORTS_LINEAR
  12. #endif
  13. #if (!UNITY_STANDALONE_WIN && !UNITY_EDITOR_WIN) && (UNITY_EDITOR_OSX || UNITY_STANDALONE_OSX || UNITY_IOS || UNITY_TVOS || UNITY_ANDROID)
  14. #define UNITY_PLATFORM_SUPPORTS_VIDEOTRANSFORM
  15. #endif
  16. #if (UNITY_EDITOR_WIN || (!UNITY_EDITOR && UNITY_STANDALONE_WIN))
  17. #define UNITY_PLATFORM_SUPPORTS_VIDEOASPECTRATIO
  18. #endif
  19. using System.Collections.Generic;
  20. using UnityEngine;
  21. using UnityEngine.UI;
  22. using UnityEngine.Serialization;
  23. //-----------------------------------------------------------------------------
  24. // Copyright 2015-2021 RenderHeads Ltd. All rights reserved.
  25. //-----------------------------------------------------------------------------
  26. namespace RenderHeads.Media.AVProVideo
  27. {
  28. /// <summary>
  29. /// Displays the video from MediaPlayer component using uGUI
  30. /// </summary>
  31. [HelpURL("http://renderheads.com/products/avpro-video/")]
  32. [AddComponentMenu("AVPro Video/Display uGUI", 200)]
  33. //[ExecuteInEditMode]
  34. public class DisplayUGUI : MaskableGraphic
  35. {
  36. [SerializeField] MediaPlayer _mediaPlayer;
  37. public MediaPlayer Player
  38. {
  39. get { return _mediaPlayer; }
  40. set { ChangeMediaPlayer(value); }
  41. }
  42. [Tooltip("Default texture to display when the video texture is preparing")]
  43. [SerializeField] Texture _defaultTexture;
  44. public Texture DefaultTexture
  45. {
  46. get { return _defaultTexture; }
  47. set { if (_defaultTexture != value) { _defaultTexture = value; } }
  48. }
  49. [FormerlySerializedAs("m_UVRect")]
  50. [SerializeField] Rect _uvRect = new Rect(0f, 0f, 1f, 1f);
  51. public Rect UVRect
  52. {
  53. get { return _uvRect; }
  54. set { _uvRect = value; }
  55. }
  56. [SerializeField] bool _setNativeSize = false;
  57. public bool ApplyNativeSize
  58. {
  59. get { return _setNativeSize; }
  60. set { _setNativeSize = value; }
  61. }
  62. [SerializeField] ScaleMode _scaleMode = ScaleMode.ScaleToFit;
  63. public ScaleMode ScaleMode
  64. {
  65. get { return _scaleMode; }
  66. set { _scaleMode = value; }
  67. }
  68. [SerializeField] bool _noDefaultDisplay = true;
  69. public bool NoDefaultDisplay
  70. {
  71. get { return _noDefaultDisplay; }
  72. set { _noDefaultDisplay = value; }
  73. }
  74. [SerializeField] bool _displayInEditor = true;
  75. public bool DisplayInEditor
  76. {
  77. get { return _displayInEditor; }
  78. set { _displayInEditor = value; }
  79. }
  80. private int _lastWidth;
  81. private int _lastHeight;
  82. private Orientation _lastOrientation;
  83. private bool _flipY;
  84. private Texture _lastTexture;
  85. private static Shader _shaderStereoPacking;
  86. private static Shader _shaderAlphaPacking;
  87. private static Shader _shaderAndroidOES;
  88. private static Shader _shaderAndroidOESAlphaPacking;
  89. private bool _isUserMaterial = true;
  90. private Material _material;
  91. private List<UIVertex> _vertices = new List<UIVertex>(4);
  92. private static List<int> QuadIndices = new List<int>(new int[] { 0, 1, 2, 2, 3, 0 });
  93. protected override void Awake()
  94. {
  95. if (_mediaPlayer != null)
  96. {
  97. _mediaPlayer.Events.AddListener(OnMediaPlayerEvent);
  98. }
  99. base.Awake();
  100. }
  101. // Callback function to handle events
  102. private void OnMediaPlayerEvent(MediaPlayer mp, MediaPlayerEvent.EventType et, ErrorCode errorCode)
  103. {
  104. switch (et)
  105. {
  106. case MediaPlayerEvent.EventType.FirstFrameReady:
  107. if (_isUserMaterial && null != GetRequiredShader())
  108. {
  109. Debug.LogWarning("[AVProVideo] Custom material is being used but the video requires our internal shader for correct rendering. Consider removing custom shader or modifying it for AVPro Video support.", this);
  110. }
  111. LateUpdate();
  112. break;
  113. case MediaPlayerEvent.EventType.PropertiesChanged:
  114. case MediaPlayerEvent.EventType.ResolutionChanged:
  115. case MediaPlayerEvent.EventType.Closing:
  116. LateUpdate();
  117. break;
  118. }
  119. // TODO: remove this, we're just doing this so we can make sure texture is correct when running in EDIT mode
  120. LateUpdate();
  121. }
  122. private void ChangeMediaPlayer(MediaPlayer player)
  123. {
  124. if (_mediaPlayer != player)
  125. {
  126. if (_mediaPlayer != null)
  127. {
  128. _mediaPlayer.Events.RemoveListener(OnMediaPlayerEvent);
  129. }
  130. _mediaPlayer = player;
  131. if (_mediaPlayer != null)
  132. {
  133. _mediaPlayer.Events.AddListener(OnMediaPlayerEvent);
  134. }
  135. LateUpdate();
  136. }
  137. }
  138. private static Shader EnsureShader(Shader shader, string name)
  139. {
  140. if (shader == null)
  141. {
  142. shader = Shader.Find(name);
  143. if (shader == null)
  144. {
  145. Debug.LogWarning("[AVProVideo] Missing shader " + name);
  146. }
  147. }
  148. return shader;
  149. }
  150. private static Shader EnsureAlphaPackingShader()
  151. {
  152. _shaderAlphaPacking = EnsureShader(_shaderAlphaPacking, "AVProVideo/Internal/UI/Transparent Packed (stereo)");
  153. return _shaderAlphaPacking;
  154. }
  155. private static Shader EnsureStereoPackingShader()
  156. {
  157. _shaderStereoPacking = EnsureShader(_shaderStereoPacking, "AVProVideo/Internal/UI/Stereo");
  158. return _shaderStereoPacking;
  159. }
  160. private Shader EnsureAndroidOESShader()
  161. {
  162. _shaderAndroidOES = EnsureShader(_shaderAndroidOES, "AVProVideo/Internal/UI/Stereo - AndroidOES");
  163. return _shaderAndroidOES;
  164. }
  165. private static Shader EnsureAndroidOESAlphaPackingShader()
  166. {
  167. _shaderAndroidOESAlphaPacking = EnsureShader(_shaderAndroidOESAlphaPacking, "AVProVideo/Internal/UI/Transparent Packed (stereo) - AndroidOES");
  168. return _shaderAndroidOESAlphaPacking;
  169. }
  170. protected override void Start()
  171. {
  172. _isUserMaterial = (this.m_Material != null);
  173. if (_isUserMaterial)
  174. {
  175. _material = new Material(this.material);
  176. this.material = _material;
  177. }
  178. base.Start();
  179. }
  180. protected override void OnDestroy()
  181. {
  182. // Destroy existing material
  183. if (_material != null)
  184. {
  185. this.material = null;
  186. #if UNITY_EDITOR
  187. Material.DestroyImmediate(_material);
  188. #else
  189. Material.Destroy(_material);
  190. #endif
  191. _material = null;
  192. }
  193. ChangeMediaPlayer(null);
  194. base.OnDestroy();
  195. }
  196. private Shader GetRequiredShader()
  197. {
  198. Shader result = null;
  199. if (result == null && _mediaPlayer.TextureProducer != null)
  200. {
  201. switch (_mediaPlayer.TextureProducer.GetTextureStereoPacking())
  202. {
  203. case StereoPacking.None:
  204. break;
  205. case StereoPacking.LeftRight:
  206. case StereoPacking.TopBottom:
  207. case StereoPacking.TwoTextures:
  208. result = EnsureStereoPackingShader();
  209. break;
  210. }
  211. if (_mediaPlayer.TextureProducer.GetTextureTransparency() == TransparencyMode.Transparent)
  212. {
  213. result = EnsureAlphaPackingShader();
  214. }
  215. switch (_mediaPlayer.TextureProducer.GetTextureAlphaPacking())
  216. {
  217. case AlphaPacking.None:
  218. break;
  219. case AlphaPacking.LeftRight:
  220. case AlphaPacking.TopBottom:
  221. result = EnsureAlphaPackingShader();
  222. break;
  223. }
  224. }
  225. #if UNITY_PLATFORM_SUPPORTS_LINEAR
  226. if (result == null && _mediaPlayer.Info != null)
  227. {
  228. if (QualitySettings.activeColorSpace == ColorSpace.Linear && !_mediaPlayer.Info.PlayerSupportsLinearColorSpace())
  229. {
  230. result = EnsureAlphaPackingShader();
  231. }
  232. }
  233. #endif
  234. if (result == null && _mediaPlayer.TextureProducer != null && _mediaPlayer.TextureProducer.GetTextureCount() == 2)
  235. {
  236. result = EnsureAlphaPackingShader();
  237. }
  238. // if (_mediaPlayer.TextureProducer != null && _mediaPlayer.IsUsingAndroidOESPath() && (_defaultTexture == null || _lastTexture != _defaultTexture ))
  239. if (_mediaPlayer.TextureProducer != null && _mediaPlayer.IsUsingAndroidOESPath())
  240. {
  241. // This shader handles stereo too
  242. result = EnsureAndroidOESShader();
  243. if (_mediaPlayer.TextureProducer.GetTextureTransparency() == TransparencyMode.Transparent)
  244. {
  245. result = EnsureAndroidOESAlphaPackingShader();
  246. }
  247. switch (_mediaPlayer.TextureProducer.GetTextureAlphaPacking())
  248. {
  249. case AlphaPacking.None:
  250. break;
  251. case AlphaPacking.LeftRight:
  252. case AlphaPacking.TopBottom:
  253. result = EnsureAndroidOESAlphaPackingShader();
  254. break;
  255. }
  256. }
  257. return result;
  258. }
  259. /// <summary>
  260. /// Returns the texture used to draw this Graphic.
  261. /// </summary>
  262. public override Texture mainTexture
  263. {
  264. get
  265. {
  266. Texture result = Texture2D.whiteTexture;
  267. if (HasValidTexture())
  268. {
  269. Texture resamplerTex = _mediaPlayer.FrameResampler == null || _mediaPlayer.FrameResampler.OutputTexture == null ? null : _mediaPlayer.FrameResampler.OutputTexture[0];
  270. result = _mediaPlayer.UseResampler ? resamplerTex : _mediaPlayer.TextureProducer.GetTexture();
  271. }
  272. else
  273. {
  274. if (_noDefaultDisplay)
  275. {
  276. result = null;
  277. }
  278. else if (_defaultTexture != null)
  279. {
  280. result = _defaultTexture;
  281. }
  282. #if UNITY_EDITOR
  283. if (result == null && _displayInEditor)
  284. {
  285. result = Resources.Load<Texture2D>("AVProVideoIcon");
  286. }
  287. #endif
  288. }
  289. return result;
  290. }
  291. }
  292. public bool HasValidTexture()
  293. {
  294. return (Application.isPlaying && _mediaPlayer != null && _mediaPlayer.TextureProducer != null && _mediaPlayer.TextureProducer.GetTexture() != null);
  295. }
  296. private void UpdateInternalMaterial()
  297. {
  298. if (_mediaPlayer != null)
  299. {
  300. // Get required shader
  301. Shader currentShader = null;
  302. if (_material != null)
  303. {
  304. currentShader = _material.shader;
  305. }
  306. Shader nextShader = GetRequiredShader();
  307. // If the shader requirement has changed
  308. if (currentShader != nextShader)
  309. {
  310. // Destroy existing material
  311. if (_material != null)
  312. {
  313. this.material = null;
  314. #if UNITY_EDITOR
  315. Material.DestroyImmediate(_material);
  316. #else
  317. Material.Destroy(_material);
  318. #endif
  319. _material = null;
  320. }
  321. // Create new material
  322. if (nextShader != null)
  323. {
  324. _material = new Material(nextShader);
  325. }
  326. }
  327. this.material = _material;
  328. }
  329. }
  330. // We do a LateUpdate() to allow for any changes in the texture that may have happened in Update()
  331. void LateUpdate()
  332. {
  333. if (_setNativeSize)
  334. {
  335. SetNativeSize();
  336. }
  337. if (_lastTexture != mainTexture)
  338. {
  339. _lastTexture = mainTexture;
  340. SetVerticesDirty();
  341. SetMaterialDirty();
  342. }
  343. if (HasValidTexture())
  344. {
  345. if (mainTexture != null)
  346. {
  347. Orientation orientation = Helper.GetOrientation(_mediaPlayer.Info.GetTextureTransform());
  348. if (mainTexture.width != _lastWidth || mainTexture.height != _lastHeight || orientation != _lastOrientation)
  349. {
  350. _lastWidth = mainTexture.width;
  351. _lastHeight = mainTexture.height;
  352. _lastOrientation = orientation;
  353. SetVerticesDirty();
  354. SetMaterialDirty();
  355. }
  356. }
  357. }
  358. if (Application.isPlaying)
  359. {
  360. if (!_isUserMaterial)
  361. {
  362. UpdateInternalMaterial();
  363. }
  364. }
  365. if (material != null && _mediaPlayer != null)
  366. {
  367. // TODO: only run when dirty
  368. VideoRender.SetupMaterialForMedia(materialForRendering, _mediaPlayer);
  369. }
  370. }
  371. /// <summary>
  372. /// Texture to be used.
  373. /// </summary>
  374. public MediaPlayer CurrentMediaPlayer
  375. {
  376. get
  377. {
  378. return _mediaPlayer;
  379. }
  380. set
  381. {
  382. if (_mediaPlayer != value)
  383. {
  384. _mediaPlayer = value;
  385. //SetVerticesDirty();
  386. SetMaterialDirty();
  387. }
  388. }
  389. }
  390. /// <summary>
  391. /// UV rectangle used by the texture.
  392. /// </summary>
  393. public Rect uvRect
  394. {
  395. get
  396. {
  397. return _uvRect;
  398. }
  399. set
  400. {
  401. if (_uvRect == value)
  402. {
  403. return;
  404. }
  405. _uvRect = value;
  406. SetVerticesDirty();
  407. }
  408. }
  409. /// <summary>
  410. /// Adjust the scale of the Graphic to make it pixel-perfect.
  411. /// </summary>
  412. [ContextMenu("Set Native Size")]
  413. public override void SetNativeSize()
  414. {
  415. Texture tex = mainTexture;
  416. if (tex != null)
  417. {
  418. int w = Mathf.RoundToInt(tex.width * uvRect.width);
  419. int h = Mathf.RoundToInt(tex.height * uvRect.height);
  420. if (_mediaPlayer != null)
  421. {
  422. #if UNITY_PLATFORM_SUPPORTS_VIDEOTRANSFORM && !(!UNITY_EDITOR && UNITY_ANDROID)
  423. if (_mediaPlayer.Info != null)
  424. {
  425. Orientation ori = Helper.GetOrientation(_mediaPlayer.Info.GetTextureTransform());
  426. if (ori == Orientation.Portrait || ori == Orientation.PortraitFlipped)
  427. {
  428. w = Mathf.RoundToInt(tex.height * uvRect.width);
  429. h = Mathf.RoundToInt(tex.width * uvRect.height);
  430. }
  431. }
  432. #endif
  433. if (_mediaPlayer.TextureProducer != null)
  434. {
  435. if (_mediaPlayer.TextureProducer.GetTextureAlphaPacking() == AlphaPacking.LeftRight ||
  436. _mediaPlayer.TextureProducer.GetTextureStereoPacking() == StereoPacking.LeftRight)
  437. {
  438. w /= 2;
  439. }
  440. else if (_mediaPlayer.TextureProducer.GetTextureAlphaPacking() == AlphaPacking.TopBottom ||
  441. _mediaPlayer.TextureProducer.GetTextureStereoPacking() == StereoPacking.TopBottom)
  442. {
  443. h /= 2;
  444. }
  445. }
  446. }
  447. rectTransform.anchorMax = rectTransform.anchorMin;
  448. rectTransform.sizeDelta = new Vector2(w, h);
  449. }
  450. }
  451. protected override void OnPopulateMesh(VertexHelper vh)
  452. {
  453. vh.Clear();
  454. _OnFillVBO(_vertices);
  455. vh.AddUIVertexStream(_vertices, QuadIndices );
  456. }
  457. private void _OnFillVBO(List<UIVertex> vbo)
  458. {
  459. _flipY = false;
  460. if (HasValidTexture())
  461. {
  462. _flipY = _mediaPlayer.TextureProducer.RequiresVerticalFlip();
  463. }
  464. Rect uvRect = _uvRect;
  465. Vector4 v = GetDrawingDimensions(_scaleMode, ref uvRect);
  466. #if UNITY_PLATFORM_SUPPORTS_VIDEOTRANSFORM
  467. Matrix4x4 m = Matrix4x4.identity;
  468. if (HasValidTexture())
  469. {
  470. m = Helper.GetMatrixForOrientation(Helper.GetOrientation(_mediaPlayer.Info.GetTextureTransform()));
  471. }
  472. #endif
  473. vbo.Clear();
  474. var vert = UIVertex.simpleVert;
  475. vert.color = color;
  476. vert.position = new Vector2(v.x, v.y);
  477. vert.uv0 = new Vector2(uvRect.xMin, uvRect.yMin);
  478. if (_flipY)
  479. {
  480. vert.uv0 = new Vector2(uvRect.xMin, 1.0f - uvRect.yMin);
  481. }
  482. #if UNITY_PLATFORM_SUPPORTS_VIDEOTRANSFORM
  483. vert.uv0 = m.MultiplyPoint3x4(vert.uv0);
  484. #endif
  485. vbo.Add(vert);
  486. vert.position = new Vector2(v.x, v.w);
  487. vert.uv0 = new Vector2(uvRect.xMin, uvRect.yMax);
  488. if (_flipY)
  489. {
  490. vert.uv0 = new Vector2(uvRect.xMin, 1.0f - uvRect.yMax);
  491. }
  492. #if UNITY_PLATFORM_SUPPORTS_VIDEOTRANSFORM
  493. vert.uv0 = m.MultiplyPoint3x4(vert.uv0);
  494. #endif
  495. vbo.Add(vert);
  496. vert.position = new Vector2(v.z, v.w);
  497. vert.uv0 = new Vector2(uvRect.xMax, uvRect.yMax);
  498. if (_flipY)
  499. {
  500. vert.uv0 = new Vector2(uvRect.xMax, 1.0f - uvRect.yMax);
  501. }
  502. #if UNITY_PLATFORM_SUPPORTS_VIDEOTRANSFORM
  503. vert.uv0 = m.MultiplyPoint3x4(vert.uv0);
  504. #endif
  505. vbo.Add(vert);
  506. vert.position = new Vector2(v.z, v.y);
  507. vert.uv0 = new Vector2(uvRect.xMax, uvRect.yMin);
  508. if (_flipY)
  509. {
  510. vert.uv0 = new Vector2(uvRect.xMax, 1.0f - uvRect.yMin);
  511. }
  512. #if UNITY_PLATFORM_SUPPORTS_VIDEOTRANSFORM
  513. vert.uv0 = m.MultiplyPoint3x4(vert.uv0);
  514. #endif
  515. vbo.Add(vert);
  516. }
  517. private Vector4 GetDrawingDimensions(ScaleMode scaleMode, ref Rect uvRect)
  518. {
  519. Vector4 returnSize = Vector4.zero;
  520. if (mainTexture != null)
  521. {
  522. var padding = Vector4.zero;
  523. var textureSize = new Vector2(mainTexture.width, mainTexture.height);
  524. {
  525. // Adjust textureSize based on orientation
  526. #if UNITY_PLATFORM_SUPPORTS_VIDEOTRANSFORM && !(!UNITY_EDITOR && UNITY_ANDROID)
  527. if (HasValidTexture())
  528. {
  529. Matrix4x4 m = Helper.GetMatrixForOrientation(Helper.GetOrientation(_mediaPlayer.Info.GetTextureTransform()));
  530. textureSize = m.MultiplyVector(textureSize);
  531. textureSize.x = Mathf.Abs(textureSize.x);
  532. textureSize.y = Mathf.Abs(textureSize.y);
  533. }
  534. #endif
  535. #if UNITY_PLATFORM_SUPPORTS_VIDEOASPECTRATIO
  536. if (HasValidTexture())
  537. {
  538. float par = _mediaPlayer.TextureProducer.GetTexturePixelAspectRatio();
  539. if (par > 0f)
  540. {
  541. if (par > 1f)
  542. {
  543. textureSize.x *= par;
  544. }
  545. else
  546. {
  547. textureSize.y /= par;
  548. }
  549. }
  550. }
  551. #endif
  552. // Adjust textureSize based on alpha/stereo packing
  553. if (_mediaPlayer != null && _mediaPlayer.TextureProducer != null)
  554. {
  555. if (_mediaPlayer.TextureProducer.GetTextureAlphaPacking() == AlphaPacking.LeftRight ||
  556. _mediaPlayer.TextureProducer.GetTextureStereoPacking() == StereoPacking.LeftRight)
  557. {
  558. textureSize.x /= 2;
  559. }
  560. else if (_mediaPlayer.TextureProducer.GetTextureAlphaPacking() == AlphaPacking.TopBottom ||
  561. _mediaPlayer.TextureProducer.GetTextureStereoPacking() == StereoPacking.TopBottom)
  562. {
  563. textureSize.y /= 2;
  564. }
  565. }
  566. }
  567. Rect r = GetPixelAdjustedRect();
  568. // Fit the above textureSize into rectangle r
  569. int spriteW = Mathf.RoundToInt( textureSize.x );
  570. int spriteH = Mathf.RoundToInt( textureSize.y );
  571. var size = new Vector4( padding.x / spriteW,
  572. padding.y / spriteH,
  573. (spriteW - padding.z) / spriteW,
  574. (spriteH - padding.w) / spriteH );
  575. {
  576. if (textureSize.sqrMagnitude > 0.0f)
  577. {
  578. if (scaleMode == ScaleMode.ScaleToFit)
  579. {
  580. float spriteRatio = textureSize.x / textureSize.y;
  581. float rectRatio = r.width / r.height;
  582. if (spriteRatio > rectRatio)
  583. {
  584. float oldHeight = r.height;
  585. r.height = r.width * (1.0f / spriteRatio);
  586. r.y += (oldHeight - r.height) * rectTransform.pivot.y;
  587. }
  588. else
  589. {
  590. float oldWidth = r.width;
  591. r.width = r.height * spriteRatio;
  592. r.x += (oldWidth - r.width) * rectTransform.pivot.x;
  593. }
  594. }
  595. else if (scaleMode == ScaleMode.ScaleAndCrop)
  596. {
  597. float aspectRatio = textureSize.x / textureSize.y;
  598. float screenRatio = r.width / r.height;
  599. if (screenRatio > aspectRatio)
  600. {
  601. float adjust = aspectRatio / screenRatio;
  602. uvRect = new Rect(uvRect.xMin, (uvRect.yMin * adjust) + (1f - adjust) * 0.5f, uvRect.width, adjust * uvRect.height);
  603. }
  604. else
  605. {
  606. float adjust = screenRatio / aspectRatio;
  607. uvRect = new Rect(uvRect.xMin * adjust + (0.5f - adjust * 0.5f), uvRect.yMin, adjust * uvRect.width, uvRect.height);
  608. }
  609. }
  610. }
  611. }
  612. returnSize = new Vector4( r.x + r.width * size.x,
  613. r.y + r.height * size.y,
  614. r.x + r.width * size.z,
  615. r.y + r.height * size.w );
  616. }
  617. return returnSize;
  618. }
  619. }
  620. }
  621. #endif