Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.
CloseFor some reason your suggested change could not be submitted. Please <a>try again</a> in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.
ClosescreentoPanelSpaceFunction | The translation function. Set to null to revert to the default behavior. |
Sets the function that handles the transformation from screen space to panel space. For overlay panels, this function returns the input value.
If the panel's targetTexture is applied to 3D objects, use a function that raycasts
against MeshColliders in the Scene. The function can first check whether the GameObject that the ray hits
has a MeshRenderer with a shader that uses this panel's target texture. It can then return the transformed
RaycastHit.textureCoord
in the texture's pixel space. Return a coordinate outside the panel to
skip incoming pointer events if the ray doesn't hit a valid target or location.
#if Include_UITextureProjection_Sample using System; using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UIElements;
namespace Samples.Runtime.Rendering { public class UITextureProjection : MonoBehaviour { public Camera m_TargetCamera;
/// <summary> /// When using a render texture, this camera will be used to translate screencoodinates to the panel's coordinates /// </summary> /// <remarks> /// If none is set, it will be initialized with Camera.main /// </remarks> public Camera targetCamera { get { if (m_TargetCamera == null) m_TargetCamera = Camera.main; return m_TargetCamera; } set => m_TargetCamera = value; }
public PanelSettings TargetPanel;
private Func<Vector2, Vector2> m_DefaultRenderTextureScreenTranslation;
void OnEnable() { if (TargetPanel != null) { if (m_DefaultRenderTextureScreenTranslation == null) { m_DefaultRenderTextureScreenTranslation = (pos) => ScreenCoordinatesToRenderTexture(pos); }
TargetPanel.SetScreenToPanelSpaceFunction(m_DefaultRenderTextureScreenTranslation); } }
void OnDisable() { //we reset it back to the default behavior if (TargetPanel != null) { TargetPanel.SetScreenToPanelSpaceFunction(null); } }
/// <summary> /// Transforms a screen position to a position relative to render texture used by a MeshRenderer. /// </summary> /// <param name="screenPosition">The position in screen coordinates.</param> /// <param name="currentCamera">Camera used for 3d object picking</param> /// <param name="targetTexture">The texture used by the panel</param> /// <returns>Returns the coordinates in texel space, or a position containing NaN values if no hit was recorded or if the hit mesh's material is not using the render texture as their mainTexture</returns> private Vector2 ScreenCoordinatesToRenderTexture(Vector2 screenPosition) { var invalidPosition = new Vector2(float.NaN, float.NaN);
screenPosition.y = Screen.height - screenPosition.y; var cameraRay = targetCamera.ScreenPointToRay(screenPosition);
RaycastHit hit; if (!Physics.Raycast(cameraRay, out hit)) { return invalidPosition; }
var targetTexture = TargetPanel.targetTexture; MeshRenderer rend = hit.transform.GetComponent<MeshRenderer>();
if (rend == null || rend.sharedMaterial.mainTexture != targetTexture) { return invalidPosition; }
Vector2 pixelUV = hit.textureCoord;
//since y screen coordinates are usually inverted, we need to flip them pixelUV.y = 1 - pixelUV.y;
pixelUV.x *= targetTexture.width; pixelUV.y *= targetTexture.height;
return pixelUV; } } } #endif
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.