C# 设置为active(激活)和Input.touch(输入)时出现故障
我有一个应用程序,在用户点击时生成一个模型,当用户点击模型时,会弹出一个菜单 当用户点击除canvas元素之外的任何其他位置时,菜单关闭 但这不起作用,当我按下canvas元素时,它会检测到canvas元素,但菜单仍然关闭 这是我的密码:C# 设置为active(激活)和Input.touch(输入)时出现故障,c#,unity3d,augmented-reality,C#,Unity3d,Augmented Reality,我有一个应用程序,在用户点击时生成一个模型,当用户点击模型时,会弹出一个菜单 当用户点击除canvas元素之外的任何其他位置时,菜单关闭 但这不起作用,当我按下canvas元素时,它会检测到canvas元素,但菜单仍然关闭 这是我的密码: void Update() { UpdatePlacementPose(); UpdatePlacementIndicator(); if (Input.touchCount > 0 )//&
void Update()
{
UpdatePlacementPose();
UpdatePlacementIndicator();
if (Input.touchCount > 0 )//&& Input.GetTouch(0).phase == TouchPhase.Began)
{
Ray ray = Camera.main.ScreenPointToRay(Input.GetTouch(0).position);
RaycastHit hit;
debugText.text = "touched";
// Check if finger is over a UI element
if (EventSystem.current.IsPointerOverGameObject(Input.GetTouch(0).fingerId))
{
Debug.Log("Touched the UI");
debugText1.text = "Touched the UI";
isui = true;
}
else
{
isui = false;
if (Physics.Raycast(ray, out hit) && (hit.transform.name != "Quad"))
{
//debugText.text = hit.transform.name;
debugText.text = Input.touchCount.ToString();
if (IsSpawned)
{
debugText1.text = ColourImage.activeSelf.ToString();
ColourImage.SetActive(true);
//debugText.text = objectToPlace.name;
}
}
else
{
if (placementPoseIsValid && Input.touchCount > 0 && !IsSpawned)
{
PlaceObject();
IsSpawned = true;
PlayerPrefs.SetString("type", "medium");
}
else
{
ColourImage.SetActive(false);
}
}
}
}
//if (placementPoseIsValid && Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began)
//{
// PlaceObject();
//}
}
最好的解决方案是在UI菜单后面创建“拦截器”对象
private GameObject CreateBlocker()
{
GameObject gameObject = new GameObject("Blocker");
RectTransform rectTransform = gameObject.AddComponent<RectTransform>();
rectTransform.SetParent(transformObjectBehindMenu,
false);
rectTransform.anchorMin = (Vector2)Vector3.zero;
rectTransform.anchorMax = (Vector2)Vector3.one;
rectTransform.sizeDelta = Vector2.zero;
Canvas canvas = gameObject.AddComponent<Canvas>();
canvas.overrideSorting = true;
canvas.sortingOrder += 1;
gameObject.AddComponent<GraphicRaycaster>();
gameObject.AddComponent<Image>().color = Color.clear;
gameObject.AddComponent<Button>().onClick.AddListener(delegate {
CloseMenu(); });
return gameObject;
}
private GameObject CreateBlocker()
{
GameObject GameObject=新游戏对象(“拦截器”);
RectTransform=gameObject.AddComponent();
SetParent(transformObjectBehindMenu,
假);
rectTransform.anchorMin=(Vector2)Vector3.0;
rectTransform.anchorMax=(Vector2)Vector3.1;
rectTransform.sizeDelta=Vector2.0;
Canvas Canvas=gameObject.AddComponent();
canvas.overrideSorting=true;
canvas.sortingOrder+=1;
gameObject.AddComponent();
gameObject.AddComponent().color=color.clear;
gameObject.AddComponent().onClick.AddListener(委托{
关闭菜单();});
返回游戏对象;
}
在模型单击时调用此方法,您可以在菜单中导航并在单击其他任何位置时关闭您可以使用
GraphicRaycaster
测试单击事件是否以UI元素为目标
这是一个简单的演示
public class Demo : MonoBehaviour
{
#pragma warning disable 0649
[SerializeField] private EventSystem _eventSystem;
[SerializeField] private GraphicRaycaster _graphicRaycaster;
#pragma warning restore 0649
private void Update()
{
if (Input.GetMouseButtonDown(0))
{
if (TestClickEvent(Input.mousePosition))
{
Debug.Log("Clicked UI Element");
}
}
}
private bool TestClickEvent(Vector3 mousePos)
{
PointerEventData data = new PointerEventData(_eventSystem);
data.position = mousePos;
List<RaycastResult> results = new List<RaycastResult>();
_graphicRaycaster.Raycast(data, results);
if(results.Count > 0)
{
//You can loop through the results to test for a specific UI element
return true;
}
return false;
}
}
公共类演示:MonoBehavior
{
#pragma警告禁用0649
[SerializeField]private EventSystem\u EventSystem;
[SerializeField]专用GraphicRaycaster\u GraphicRaycaster;
#pragma警告恢复0649
私有void更新()
{
if(Input.GetMouseButtonDown(0))
{
if(TestClickEvent(Input.mousePosition))
{
Log(“单击的UI元素”);
}
}
}
私有bool TestClickEvent(Vector3鼠标点)
{
PointerEventData数据=新的PointerEventData(\u eventSystem);
data.position=鼠标点;
列表结果=新列表();
_graphicRaycaster.Raycast(数据、结果);
如果(results.Count>0)
{
//您可以在结果中循环以测试特定的UI元素
返回true;
}
返回false;
}
}
有关更多信息,请参阅官方网站