CurvedUISettings.cs 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813
  1. #define CURVEDUI_PRESENT //If you're an asset creator and want to see if CurvedUI is imported, just use "#if CURVEDUI_PRESENT [your code] #endif"
  2. using System;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. using UnityEngine.EventSystems;
  6. using System.Collections.Generic;
  7. #if CURVEDUI_TMP || TMP_PRESENT
  8. using TMPro;
  9. #endif
  10. /// <summary>
  11. /// This class stores settings for the entire canvas. It also stores useful methods for converting cooridinates to and from 2d canvas to curved canvas, or world space.
  12. /// CurvedUIVertexEffect components (added to every canvas gameobject)ask this class for per-canvas settings when applying their curve effect.
  13. /// </summary>
  14. namespace CurvedUI
  15. {
  16. [AddComponentMenu("CurvedUI/CurvedUISettings")]
  17. [RequireComponent(typeof(Canvas))]
  18. public class CurvedUISettings : MonoBehaviour
  19. {
  20. public const string Version = "3.2";
  21. #region SETTINGS
  22. //Global settings
  23. [SerializeField]
  24. CurvedUIShape shape;
  25. [SerializeField]
  26. float quality = 1f;
  27. [SerializeField]
  28. bool interactable = true;
  29. [SerializeField]
  30. bool blocksRaycasts = true;
  31. [SerializeField]
  32. bool forceUseBoxCollider = false;
  33. //Cyllinder settings
  34. [SerializeField]
  35. int angle = 90;
  36. [SerializeField]
  37. bool preserveAspect = true;
  38. //Sphere settings
  39. [SerializeField]
  40. int vertAngle = 90;
  41. //ring settings
  42. [SerializeField]
  43. float ringFill = 0.5f;
  44. [SerializeField]
  45. int ringExternalDiamater = 1000;
  46. [SerializeField]
  47. bool ringFlipVertical = false;
  48. //internal system settings
  49. int baseCircleSegments = 16;
  50. //stored variables
  51. Vector2 savedRectSize;
  52. float savedRadius;
  53. Canvas myCanvas;
  54. RectTransform m_rectTransform;
  55. #endregion
  56. #region LIFECYCLE
  57. void Awake()
  58. {
  59. // If this canvas is on Default layer, switch it to UI layer..
  60. // this is to make sure that when using raycasting to detect interactions,
  61. // nothing will interfere with it.
  62. if (gameObject.layer == 0) this.gameObject.layer = 5;
  63. //save initial variables
  64. savedRectSize = RectTransform.rect.size;
  65. }
  66. void Start()
  67. {
  68. if (Application.isPlaying)
  69. {
  70. // lets get rid of any raycasters and add our custom one
  71. // It will be responsible for handling interactions.
  72. BaseRaycaster[] raycasters = GetComponents<BaseRaycaster>();
  73. foreach(BaseRaycaster caster in raycasters)
  74. {
  75. if (!(caster is CurvedUIRaycaster))
  76. caster.enabled = false;
  77. }
  78. this.gameObject.AddComponentIfMissing<CurvedUIRaycaster>();
  79. //find if there are any child canvases that may break interactions
  80. Canvas[] canvases = GetComponentsInChildren<Canvas>();
  81. foreach(Canvas cnv in canvases)
  82. {
  83. if (cnv.gameObject != this.gameObject)
  84. {
  85. Transform trans = cnv.transform;
  86. string hierarchyName = trans.name;
  87. while(trans.parent != null)
  88. {
  89. hierarchyName = trans.parent.name + "/" + hierarchyName;
  90. trans = trans.parent;
  91. }
  92. Debug.LogWarning("CURVEDUI: Interactions on nested canvases are not supported. You won't be able to interact with any child object of [" + hierarchyName + "]", cnv.gameObject);
  93. }
  94. }
  95. }
  96. //find needed references
  97. if (myCanvas == null)
  98. myCanvas = GetComponent<Canvas>();
  99. savedRadius = GetCyllinderRadiusInCanvasSpace();
  100. }
  101. void OnEnable()
  102. {
  103. //Redraw canvas object on enable.
  104. foreach (UnityEngine.UI.Graphic graph in (this).GetComponentsInChildren<UnityEngine.UI.Graphic>())
  105. {
  106. graph.SetAllDirty();
  107. }
  108. }
  109. void OnDisable()
  110. {
  111. foreach (UnityEngine.UI.Graphic graph in (this).GetComponentsInChildren<UnityEngine.UI.Graphic>())
  112. {
  113. graph.SetAllDirty();
  114. }
  115. }
  116. void Update()
  117. {
  118. //recreate the geometry if entire canvas has been resized
  119. if (RectTransform.rect.size != savedRectSize)
  120. {
  121. savedRectSize = RectTransform.rect.size;
  122. SetUIAngle(angle);
  123. }
  124. //check for improper canvas size
  125. if (savedRectSize.x == 0 || savedRectSize.y == 0)
  126. Debug.LogError("CurvedUI: Your Canvas size must be bigger than 0!");
  127. }
  128. #endregion
  129. #region PRIVATE
  130. /// <summary>
  131. /// Changes the horizontal angle of the canvas.
  132. /// </summary>
  133. /// <param name="newAngle"></param>
  134. void SetUIAngle(int newAngle) {
  135. if (myCanvas == null)
  136. myCanvas = GetComponent<Canvas>();
  137. //temp fix to make interactions with angle 0 possible
  138. if (newAngle == 0) newAngle = 1;
  139. angle = newAngle;
  140. savedRadius = GetCyllinderRadiusInCanvasSpace();
  141. foreach (CurvedUIVertexEffect ve in GetComponentsInChildren<CurvedUIVertexEffect>())
  142. ve.SetDirty();
  143. foreach (Graphic graph in GetComponentsInChildren<Graphic>())
  144. graph.SetAllDirty();
  145. if (Application.isPlaying && GetComponent<CurvedUIRaycaster>() != null)
  146. //tell raycaster to update its collider now that angle has changed.
  147. GetComponent<CurvedUIRaycaster>().RebuildCollider();
  148. }
  149. Vector3 CanvasToCyllinder(Vector3 pos)
  150. {
  151. float theta = (pos.x / savedRectSize.x) * Angle * Mathf.Deg2Rad;
  152. pos.x = Mathf.Sin(theta) * (SavedRadius + pos.z);
  153. pos.z += Mathf.Cos(theta) * (SavedRadius + pos.z) - (SavedRadius + pos.z);
  154. return pos;
  155. }
  156. Vector3 CanvasToCyllinderVertical(Vector3 pos)
  157. {
  158. float theta = (pos.y / savedRectSize.y) * Angle * Mathf.Deg2Rad;
  159. pos.y = Mathf.Sin(theta) * (SavedRadius + pos.z);
  160. pos.z += Mathf.Cos(theta) * (SavedRadius + pos.z) - (SavedRadius + pos.z);
  161. return pos;
  162. }
  163. Vector3 CanvasToRing(Vector3 pos)
  164. {
  165. float r = pos.y.Remap(savedRectSize.y * 0.5f * (RingFlipVertical ? 1 : -1), -savedRectSize.y * 0.5f * (RingFlipVertical ? 1 : -1), RingExternalDiameter * (1 - RingFill) * 0.5f, RingExternalDiameter * 0.5f);
  166. float theta = (pos.x / savedRectSize.x).Remap(-0.5f, 0.5f, Mathf.PI / 2.0f, angle * Mathf.Deg2Rad + Mathf.PI / 2.0f);
  167. pos.x = r * Mathf.Cos(theta);
  168. pos.y = r * Mathf.Sin(theta);
  169. return pos;
  170. }
  171. Vector3 CanvasToSphere(Vector3 pos)
  172. {
  173. float radius = SavedRadius;
  174. float vAngle = VerticalAngle;
  175. if (PreserveAspect)
  176. {
  177. vAngle = angle * (savedRectSize.y / savedRectSize.x);
  178. radius += Angle > 0 ? -pos.z : pos.z;
  179. }
  180. else {
  181. radius = savedRectSize.x / 2.0f + pos.z;
  182. if (vAngle == 0) return Vector3.zero;
  183. }
  184. //convert planar coordinates to spherical coordinates
  185. float theta = (pos.x / savedRectSize.x).Remap(-0.5f, 0.5f, (180 - angle) / 2.0f - 90, 180 - (180 - angle) / 2.0f - 90);
  186. theta *= Mathf.Deg2Rad;
  187. float gamma = (pos.y / savedRectSize.y).Remap(-0.5f, 0.5f, (180 - vAngle) / 2.0f, 180 - (180 - vAngle) / 2.0f);
  188. gamma *= Mathf.Deg2Rad;
  189. pos.z = Mathf.Sin(gamma) * Mathf.Cos(theta) * radius;
  190. pos.y = -radius * Mathf.Cos(gamma);
  191. pos.x = Mathf.Sin(gamma) * Mathf.Sin(theta) * radius;
  192. if (PreserveAspect)
  193. pos.z -= radius;
  194. return pos;
  195. }
  196. #endregion
  197. #region PUBLIC
  198. RectTransform RectTransform {
  199. get
  200. {
  201. if (m_rectTransform == null) m_rectTransform = transform as RectTransform;
  202. return m_rectTransform;
  203. }
  204. }
  205. /// <summary>
  206. /// Adds the CurvedUIVertexEffect component to every child gameobject that requires it.
  207. /// CurvedUIVertexEffect creates the curving effect.
  208. /// </summary>
  209. public void AddEffectToChildren()
  210. {
  211. foreach (UnityEngine.UI.Graphic graph in GetComponentsInChildren<UnityEngine.UI.Graphic>(true))
  212. {
  213. if (graph.GetComponent<CurvedUIVertexEffect>() == null)
  214. {
  215. graph.gameObject.AddComponent<CurvedUIVertexEffect>();
  216. graph.SetAllDirty();
  217. }
  218. }
  219. //additional script that will create a curved caret for input fields
  220. foreach(UnityEngine.UI.InputField iField in GetComponentsInChildren<UnityEngine.UI.InputField>(true))
  221. {
  222. if (iField.GetComponent<CurvedUIInputFieldCaret>() == null)
  223. {
  224. iField.gameObject.AddComponent<CurvedUIInputFieldCaret>();
  225. }
  226. }
  227. //TextMeshPro experimental support. Go to CurvedUITMP.cs to learn how to enable it.
  228. #if CURVEDUI_TMP || TMP_PRESENT
  229. foreach(TextMeshProUGUI tmp in GetComponentsInChildren<TextMeshProUGUI>(true)){
  230. if(tmp.GetComponent<CurvedUITMP>() == null){
  231. tmp.gameObject.AddComponent<CurvedUITMP>();
  232. tmp.SetAllDirty();
  233. }
  234. }
  235. foreach (TMP_InputField tmp in GetComponentsInChildren<TMP_InputField>(true))
  236. {
  237. tmp.AddComponentIfMissing<CurvedUITMPInputFieldCaret>();
  238. }
  239. #endif
  240. }
  241. /// <summary>
  242. /// Maps a world space vector to a curved canvas.
  243. /// Operates in Canvas's local space.
  244. /// </summary>
  245. /// <param name="pos">World space vector</param>
  246. /// <returns>
  247. /// A vector on curved canvas in canvas's local space
  248. /// </returns>
  249. public Vector3 VertexPositionToCurvedCanvas(Vector3 pos)
  250. {
  251. switch (Shape)
  252. {
  253. case CurvedUIShape.CYLINDER:
  254. {
  255. return CanvasToCyllinder(pos);
  256. }
  257. case CurvedUIShape.CYLINDER_VERTICAL:
  258. {
  259. return CanvasToCyllinderVertical(pos);
  260. }
  261. case CurvedUIShape.RING:
  262. {
  263. return CanvasToRing(pos);
  264. }
  265. case CurvedUIShape.SPHERE:
  266. {
  267. return CanvasToSphere(pos);
  268. }
  269. default:
  270. {
  271. return Vector3.zero;
  272. }
  273. }
  274. }
  275. /// <summary>
  276. /// Converts a point in Canvas space to a point on Curved surface in world space units.
  277. /// </summary>
  278. /// <param name="pos">Position on canvas in canvas space</param>
  279. /// <returns>
  280. /// Position on curved canvas in world space.
  281. /// </returns>
  282. public Vector3 CanvasToCurvedCanvas(Vector3 pos)
  283. {
  284. pos = VertexPositionToCurvedCanvas(pos);
  285. if (float.IsNaN(pos.x) || float.IsInfinity(pos.x)) return Vector3.zero;
  286. else return transform.localToWorldMatrix.MultiplyPoint3x4(pos);
  287. }
  288. /// <summary>
  289. /// Returns a normal direction on curved canvas for a given point on flat canvas. Works in canvas' local space.
  290. /// </summary>
  291. /// <param name="pos"></param>
  292. /// <returns></returns>
  293. public Vector3 CanvasToCurvedCanvasNormal(Vector3 pos)
  294. {
  295. //find the position in canvas space
  296. pos = VertexPositionToCurvedCanvas(pos);
  297. switch (Shape)
  298. {
  299. case CurvedUIShape.CYLINDER:
  300. {
  301. // find the direction to the center of cyllinder on flat XZ plane
  302. return transform.localToWorldMatrix.MultiplyVector((pos - new Vector3(0, 0, -GetCyllinderRadiusInCanvasSpace())).ModifyY(0)).normalized;
  303. }
  304. case CurvedUIShape.CYLINDER_VERTICAL:
  305. {
  306. // find the direction to the center of cyllinder on flat YZ plane
  307. return transform.localToWorldMatrix.MultiplyVector((pos - new Vector3(0, 0, -GetCyllinderRadiusInCanvasSpace())).ModifyX(0)).normalized;
  308. }
  309. case CurvedUIShape.RING:
  310. {
  311. // just return the back direction of the canvas
  312. return -transform.forward;
  313. }
  314. case CurvedUIShape.SPHERE:
  315. {
  316. //return the direction towards the sphere's center
  317. Vector3 center = (PreserveAspect ? new Vector3(0, 0, -GetCyllinderRadiusInCanvasSpace()) : Vector3.zero);
  318. return transform.localToWorldMatrix.MultiplyVector((pos - center)).normalized;
  319. }
  320. default:
  321. {
  322. return Vector3.zero;
  323. }
  324. }
  325. }
  326. /// <summary>
  327. /// Raycasts along the given ray and returns the intersection with the flat canvas.
  328. /// Use to convert from world space to flat canvas space.
  329. /// </summary>
  330. /// <param name="ray"></param>
  331. /// <returns>
  332. /// Returns the true if ray hits the CurvedCanvas.
  333. /// Outputs intersection point in flat canvas space.
  334. /// </returns>
  335. public bool RaycastToCanvasSpace(Ray ray, out Vector2 o_positionOnCanvas)
  336. {
  337. CurvedUIRaycaster caster = this.GetComponent<CurvedUIRaycaster>();
  338. o_positionOnCanvas = Vector2.zero;
  339. switch (Shape)
  340. {
  341. case CurvedUISettings.CurvedUIShape.CYLINDER:
  342. {
  343. return caster.RaycastToCyllinderCanvas(ray, out o_positionOnCanvas, true);
  344. }
  345. case CurvedUISettings.CurvedUIShape.CYLINDER_VERTICAL:
  346. {
  347. return caster.RaycastToCyllinderVerticalCanvas(ray, out o_positionOnCanvas, true);
  348. }
  349. case CurvedUISettings.CurvedUIShape.RING:
  350. {
  351. return caster.RaycastToRingCanvas(ray, out o_positionOnCanvas, true);
  352. }
  353. case CurvedUISettings.CurvedUIShape.SPHERE:
  354. {
  355. return caster.RaycastToSphereCanvas(ray, out o_positionOnCanvas, true);
  356. }
  357. default:
  358. {
  359. return false;
  360. }
  361. }
  362. }
  363. /// <summary>
  364. /// Returns the radius of curved canvas cyllinder, expressed in Cavas's local space units.
  365. /// </summary>
  366. public float GetCyllinderRadiusInCanvasSpace()
  367. {
  368. float ret;
  369. if (PreserveAspect)
  370. {
  371. if(shape == CurvedUIShape.CYLINDER_VERTICAL)
  372. ret = (RectTransform.rect.size.y / ((2 * Mathf.PI) * (angle / 360.0f)));
  373. else
  374. ret = (RectTransform.rect.size.x / ((2 * Mathf.PI) * (angle / 360.0f)));
  375. }
  376. else
  377. ret = (RectTransform.rect.size.x * 0.5f) / Mathf.Sin(Mathf.Clamp(angle, -180.0f, 180.0f) * 0.5f * Mathf.Deg2Rad);
  378. return angle == 0 ? 0 : ret;
  379. }
  380. /// <summary>
  381. /// Tells you how big UI quads can get before they should be tesselate to look good on current canvas settings.
  382. /// Used by CurvedUIVertexEffect to determine how many quads need to be created for each graphic.
  383. /// </summary>
  384. public Vector2 GetTesslationSize(bool modifiedByQuality = true)
  385. {
  386. Vector2 ret = RectTransform.rect.size;
  387. if (Angle != 0 || (!PreserveAspect && vertAngle != 0))
  388. {
  389. switch (shape)
  390. {
  391. case CurvedUIShape.CYLINDER: ret /= GetSegmentsByAngle(angle); break;
  392. case CurvedUIShape.CYLINDER_VERTICAL: goto case CurvedUIShape.CYLINDER;
  393. case CurvedUIShape.RING: goto case CurvedUIShape.CYLINDER;
  394. case CurvedUIShape.SPHERE:
  395. {
  396. ret.x /= GetSegmentsByAngle(angle);
  397. if (PreserveAspect)
  398. ret.y = ret.x * RectTransform.rect.size.y / RectTransform.rect.size.x;
  399. else
  400. ret.y /= GetSegmentsByAngle(VerticalAngle);
  401. break;
  402. }
  403. }
  404. }
  405. //Debug.Log(this.gameObject.name + " returning size " + ret + " which is " + ret * this.transform.localScale.x + " in world space.", this.gameObject);
  406. return ret / (modifiedByQuality ? Mathf.Clamp(Quality, 0.01f, 10.0f) : 1);
  407. }
  408. float GetSegmentsByAngle(float angle)
  409. {
  410. if (angle.Abs() <= 1)
  411. return 1;
  412. else if (angle.Abs() < 90)//proportionaly twice as many segments for small angle canvases
  413. return baseCircleSegments * (Mathf.Abs(angle).Remap(0, 90, 0.01f, 0.5f));
  414. else
  415. return baseCircleSegments * (Mathf.Abs(angle).Remap(90, 360.0f, 0.5f, 1));
  416. }
  417. /// <summary>
  418. /// Tells you how many segmetens should the entire 360 deg. cyllinder or sphere consist of.
  419. /// Used by CurvedUIVertexEffect
  420. /// </summary>
  421. public int BaseCircleSegments {
  422. get { return baseCircleSegments; }
  423. }
  424. /// <summary>
  425. /// The measure of the arc of the Canvas.
  426. /// </summary>
  427. public int Angle {
  428. get { return angle; }
  429. set {
  430. if (angle != value)
  431. SetUIAngle(value);
  432. }
  433. }
  434. /// <summary>
  435. /// Multiplier used to deremine how many segments a base curve of a shape has.
  436. /// Default 1. Lower values greatly increase performance. Higher values give you sharper curve.
  437. /// </summary>
  438. public float Quality {
  439. get { return quality; }
  440. set {
  441. if (quality != value)
  442. {
  443. quality = value;
  444. SetUIAngle(angle);
  445. }
  446. }
  447. }
  448. /// <summary>
  449. /// Current Shape of the canvas
  450. /// </summary>
  451. public CurvedUIShape Shape {
  452. get { return shape; }
  453. set {
  454. if (shape != value)
  455. {
  456. shape = value;
  457. SetUIAngle(angle);
  458. }
  459. }
  460. }
  461. /// <summary>
  462. /// Vertical angle of the canvas. Used in sphere shape and ring shape.
  463. /// </summary>
  464. public int VerticalAngle {
  465. get { return vertAngle; }
  466. set {
  467. if (vertAngle != value)
  468. {
  469. vertAngle = value;
  470. SetUIAngle(angle);
  471. }
  472. }
  473. }
  474. /// <summary>
  475. /// Fill of a ring in ring shaped canvas. 0-1
  476. /// </summary>
  477. public float RingFill {
  478. get { return ringFill; }
  479. set {
  480. if (ringFill != value)
  481. {
  482. ringFill = value;
  483. SetUIAngle(angle);
  484. }
  485. }
  486. }
  487. /// <summary>
  488. /// Calculated radius of the curved canvas.
  489. /// </summary>
  490. public float SavedRadius {
  491. get {
  492. if (savedRadius == 0)
  493. savedRadius = GetCyllinderRadiusInCanvasSpace();
  494. return savedRadius;
  495. }
  496. }
  497. /// <summary>
  498. /// External diameter of the ring shaped canvas.
  499. /// </summary>
  500. public int RingExternalDiameter {
  501. get { return ringExternalDiamater; }
  502. set {
  503. if (ringExternalDiamater != value)
  504. {
  505. ringExternalDiamater = value;
  506. SetUIAngle(angle);
  507. }
  508. }
  509. }
  510. /// <summary>
  511. /// Whether the center of the ring should be bottom or top of the canvas.
  512. /// </summary>
  513. public bool RingFlipVertical {
  514. get { return ringFlipVertical; }
  515. set {
  516. if (ringFlipVertical != value)
  517. {
  518. ringFlipVertical = value;
  519. SetUIAngle(angle);
  520. }
  521. }
  522. }
  523. /// <summary>
  524. /// If enabled, CurvedUI will try to preserve aspect ratio of original canvas.
  525. /// </summary>
  526. public bool PreserveAspect {
  527. get { return preserveAspect; }
  528. set {
  529. if (preserveAspect != value)
  530. {
  531. preserveAspect = value;
  532. SetUIAngle(angle);
  533. }
  534. }
  535. }
  536. /// <summary>
  537. /// Can the canvas be interacted with?
  538. /// </summary>
  539. public bool Interactable {
  540. get { return interactable; }
  541. set { interactable = value; }
  542. }
  543. /// <summary>
  544. /// Should The collider for this canvas be created using more expensive box colliders?
  545. /// DEfault false.
  546. /// </summary>
  547. public bool ForceUseBoxCollider {
  548. get { return forceUseBoxCollider; }
  549. set { forceUseBoxCollider = value; }
  550. }
  551. /// <summary>
  552. /// Will the canvas block raycasts
  553. /// Settings this to false will destroy the canvas' collider.
  554. /// </summary>
  555. public bool BlocksRaycasts {
  556. get { return blocksRaycasts; }
  557. set
  558. {
  559. if (blocksRaycasts != value) {
  560. blocksRaycasts = value;
  561. //tell raycaster to update its collider now that angle has changed.
  562. if (Application.isPlaying && GetComponent<CurvedUIRaycaster>() != null)
  563. GetComponent<CurvedUIRaycaster>().RebuildCollider();
  564. }
  565. }
  566. }
  567. /// <summary>
  568. /// Should the raycaster take other layers into account to determine if canvas has been interacted with.
  569. /// </summary>
  570. [Obsolete("Use RaycastLayerMask property instead.")]
  571. public bool RaycastMyLayerOnly {
  572. get { return true; }
  573. set { }
  574. }
  575. /// <summary>
  576. /// Forces all child CurvedUI objects to recalculate
  577. /// </summary>
  578. /// <param name="calculateCurvedOnly"> Forces children to recalculate only the curvature. Will not make them retesselate vertices. Much faster.</param>
  579. public void SetAllChildrenDirty(bool recalculateCurveOnly = false)
  580. {
  581. foreach (CurvedUIVertexEffect eff in this.GetComponentsInChildren<CurvedUIVertexEffect>())
  582. {
  583. if (recalculateCurveOnly)
  584. eff.SetDirty();
  585. else
  586. eff.CurvingRequired = true;
  587. }
  588. }
  589. #endregion
  590. #region SHORTCUTS
  591. /// <summary>
  592. /// Returns true if user's pointer is currently pointing inside this canvas.
  593. /// This is a shortcut to CurvedUIRaycaster's PointingAtCanvas bool.
  594. /// </summary>
  595. public bool PointingAtCanvas {
  596. get {
  597. if (GetComponent<CurvedUIRaycaster>() != null)
  598. return GetComponent<CurvedUIRaycaster>().PointingAtCanvas;
  599. else {
  600. Debug.LogWarning("CURVEDUI: Can't check if user is pointing at this canvas - No CurvedUIRaycaster is added to this canvas.");
  601. return false;
  602. }
  603. }
  604. }
  605. /// <summary>
  606. /// Sends OnClick event to every Button under pointer.
  607. /// This is a shortcut to CurvedUIRaycaster's Click method.
  608. /// </summary>
  609. public void Click()
  610. {
  611. if (GetComponent<CurvedUIRaycaster>() != null)
  612. GetComponent<CurvedUIRaycaster>().Click();
  613. }
  614. /// <summary>
  615. /// Current controller mode. Decides how user can interact with the canvas.
  616. /// This is a shortcut to CurvedUIInputModule's property.
  617. /// </summary>
  618. public CurvedUIInputModule.CUIControlMethod ControlMethod {
  619. get { return CurvedUIInputModule.ControlMethod; }
  620. set { CurvedUIInputModule.ControlMethod = value; }
  621. }
  622. /// <summary>
  623. /// Returns all objects currently under the pointer.
  624. /// This is a shortcut to CurvedUIInputModule's method.
  625. /// </summary>
  626. public List<GameObject> GetObjectsUnderPointer()
  627. {
  628. if (GetComponent<CurvedUIRaycaster>() != null)
  629. return GetComponent<CurvedUIRaycaster>().GetObjectsUnderPointer();
  630. else return new List<GameObject>();
  631. }
  632. /// <summary>
  633. /// Returns all the canvas objects that are visible under given Screen Position of EventCamera
  634. /// This is a shortcut to CurvedUIInputModule's method.
  635. /// </summary>
  636. public List<GameObject> GetObjectsUnderScreenPos(Vector2 pos, Camera eventCamera = null)
  637. {
  638. if (eventCamera == null)
  639. eventCamera = myCanvas.worldCamera;
  640. if (GetComponent<CurvedUIRaycaster>() != null)
  641. return GetComponent<CurvedUIRaycaster>().GetObjectsUnderScreenPos(pos, eventCamera);
  642. else return new List<GameObject>();
  643. }
  644. /// <summary>
  645. /// Returns all the canvas objects that are intersected by given ray.
  646. /// This is a shortcut to CurvedUIInputModule's method.
  647. /// </summary>
  648. public List<GameObject> GetObjectsHitByRay(Ray ray)
  649. {
  650. if (GetComponent<CurvedUIRaycaster>() != null)
  651. return GetComponent<CurvedUIRaycaster>().GetObjectsHitByRay(ray);
  652. else return new List<GameObject>();
  653. }
  654. /// <summary>
  655. /// Gaze Control Method. Should execute OnClick events on button after user points at them?
  656. /// This is a shortcut to CurvedUIInputModule's property.
  657. /// </summary>
  658. public bool GazeUseTimedClick {
  659. get { return CurvedUIInputModule.Instance.GazeUseTimedClick; }
  660. set { CurvedUIInputModule.Instance.GazeUseTimedClick = value; }
  661. }
  662. /// <summary>
  663. /// Gaze Control Method. How long after user points on a button should we click it?
  664. /// This is a shortcut to CurvedUIInputModule's property.
  665. /// </summary>
  666. public float GazeClickTimer {
  667. get { return CurvedUIInputModule.Instance.GazeClickTimer; }
  668. set { CurvedUIInputModule.Instance.GazeClickTimer = value; }
  669. }
  670. /// <summary>
  671. /// Gaze Control Method. How long after user looks at a button should we start the timer? Default 1 second.
  672. /// This is a shortcut to CurvedUIInputModule's property.
  673. /// </summary>
  674. public float GazeClickTimerDelay {
  675. get { return CurvedUIInputModule.Instance.GazeClickTimerDelay; }
  676. set { CurvedUIInputModule.Instance.GazeClickTimerDelay = value; }
  677. }
  678. /// <summary>
  679. /// Gaze Control Method. How long till Click method is executed on Buttons under gaze? Goes 0-1.
  680. /// This is a shortcut to CurvedUIInputModule's property.
  681. /// </summary>
  682. public float GazeTimerProgress {
  683. get { return CurvedUIInputModule.Instance.GazeTimerProgress; }
  684. }
  685. #endregion
  686. #region ENUMS
  687. public enum CurvedUIShape
  688. {
  689. CYLINDER = 0,
  690. RING = 1,
  691. SPHERE = 2,
  692. CYLINDER_VERTICAL = 3,
  693. }
  694. #endregion
  695. }
  696. }