C# 通过指向对象来获取对象名称Unity Raycast
我想打印我指向的对象的名称(使用crosshari)。我写了一些脚本,它允许我通过查看对象来突出显示对象(包括在下面)。你能告诉我如何在这上面添加一行代码(我指的对象的打印名称)吗 1.选择经理C# 通过指向对象来获取对象名称Unity Raycast,c#,unity3d,raycasting,C#,Unity3d,Raycasting,我想打印我指向的对象的名称(使用crosshari)。我写了一些脚本,它允许我通过查看对象来突出显示对象(包括在下面)。你能告诉我如何在这上面添加一行代码(我指的对象的打印名称)吗 1.选择经理 `public class SelectionManager : MonoBehaviour { [SerializeField] private string selectableTag = "Selectable"; private ISelectionResp
`public class SelectionManager : MonoBehaviour
{
[SerializeField] private string selectableTag = "Selectable";
private ISelectionResponse _selectionResponse;
private Transform _selection;
private void Awake()
{
_selectionResponse = GetComponent<ISelectionResponse>();
}
private void Update()
{
if (_selection != null) _selectionResponse.OnDeselect(_selection);
var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
_selection = null;
if (Physics.Raycast(ray, out var hit))
{
var selection = hit.transform;
if (selection.CompareTag(selectableTag) || (selection.CompareTag("Stool_square1")))
{
//string name = gameObject.name;
//print(name);
_selection = selection;
}
}
if (_selection != null) _selectionResponse.OnSelect(_selection);
}
}`
尝试一些类似于hit.collider.name的方法不相关的提示:你应该对layermask进行光线投射。您当前的代码检查命中场景中可能较慢的任何碰撞器。Raycast函数有一个重载,它接受一个layermask并检查给定层中碰撞器的命中情况。
using UnityEngine;
public class OutlineSelectionResponse : MonoBehaviour, ISelectionResponse
{
public void OnSelect(Transform selection)
{
var outline = selection.GetComponent<Outline>();
if (outline != null) outline.OutlineWidth = 2;
}
public void OnDeselect(Transform selection)
{
var outline = selection.GetComponent<Outline>();
if (outline != null) outline.OutlineWidth = 0;
}
}
using UnityEngine;
internal interface ISelectionResponse
{
void OnSelect(Transform selection);
void OnDeselect(Transform selection);
}