Unity c#获取单击的ui文本的值

Unity c#获取单击的ui文本的值,c#,unity3d,C#,Unity3d,有没有办法获取单击的ui文本的值? 我用RaycastHit试过了,但什么也没发生 这是我的代码: Text txt1, txt2, txt3; string textValue; private void Awake() { txtHeadline = GameObject.Find("InformationFields/txtHeadline").GetComponent<Text>(); txt1 = GameObject.Find("TextFields

有没有办法获取单击的ui文本的值? 我用RaycastHit试过了,但什么也没发生

这是我的代码:

Text txt1, txt2, txt3;

string textValue;

private void Awake()
{
    txtHeadline = GameObject.Find("InformationFields/txtHeadline").GetComponent<Text>();

    txt1 = GameObject.Find("TextFields/txtField1").GetComponent<Text>();
    txt2 = GameObject.Find("TextFields/txtField2").GetComponent<Text>();
    txt3 = GameObject.Find("TextFields/txtField3").GetComponent<Text>();
}

void Update()
{
    Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    RaycastHit hit;

    if(Physics.Raycast(ray, out hit))
    {
        // I know this might be wrong - but the code never reach this part.
        textValue = hit.collider.gameObject.name

        // How can I save the value from the clicked UI Text to textValue ?

        if(textValue != null)
        {
            Debug.Log("Text Value = " + textValue);
            txtHeadline.text = textValue;
        }
        else
        {
            Debug.Log("Text Value = null");
        }
    }
    else
    {
        Debug.Log("Nothing happens !! ");
    }
}
文本txt1、txt2、txt3;
字符串文本值;
私人空间
{
txtHeadline=GameObject.Find(“InformationFields/txtHeadline”).GetComponent();
txt1=GameObject.Find(“TextFields/txtField1”).GetComponent();
txt2=GameObject.Find(“TextFields/txtField2”).GetComponent();
txt3=GameObject.Find(“TextFields/txtField3”).GetComponent();
}
无效更新()
{
Ray-Ray=Camera.main.screenpointoray(输入.鼠标位置);
雷卡斯特击中;
如果(物理.光线投射(光线,出击))
{
//我知道这可能是错误的-但代码永远不会到达这一部分。
textValue=hit.collider.gameObject.name
//如何将单击的UI文本中的值保存为textValue?
if(textValue!=null)
{
Debug.Log(“Text Value=“+textValue”);
txtdheadline.text=textValue;
}
其他的
{
Log(“文本值=null”);
}
}
其他的
{
Log(“什么都没发生!!”;
}
}
例如,如果我单击
txt1
,我希望txt1的值将被添加到
txtHeadline

但是每次我点击任何地方,我的输出都是
,什么都没有发生。。我的光线投射器有什么问题?

您需要使用图形光线投射器,而不是物理光线投射器

它与physics raycast有很大的不同,因为您必须使用一些中间类(即,
PointerEventData
),并且它返回一个命中列表,而不仅仅是一个

此外,2019.3文件中似乎没有列出。以上链接适用于2019.1文档。看到它在不久的将来被弃用,我不会感到惊讶

上述文件摘录如下:

       //Set up the new Pointer Event
        m_PointerEventData = new PointerEventData(m_EventSystem);
        //Set the Pointer Event Position to that of the mouse position
        m_PointerEventData.position = Input.mousePosition;

        //Create a list of Raycast Results
        List<RaycastResult> results = new List<RaycastResult>();

        //Raycast using the Graphics Raycaster and mouse click position
        m_Raycaster.Raycast(m_PointerEventData, results);

        //For every result returned, output the name of the GameObject on the Canvas hit by the Ray
        foreach (RaycastResult result in results)
        {
            Debug.Log("Hit " + result.gameObject.name);
        }
//设置新指针事件
m_PointerEventData=新的PointerEventData(m_EventSystem);
//将指针事件位置设置为鼠标位置
m_PointerEventData.position=Input.mousePosition;
//创建光线投射结果的列表
列表结果=新列表();
//使用图形光线投射器和鼠标单击位置进行光线投射
m_Raycaster.Raycast(m_PointerEventData,results);
//对于返回的每个结果,在被光线击中的画布上输出游戏对象的名称
foreach(RaycastResult结果中的结果)
{
Log(“Hit”+result.gameObject.name);
}

难道你不能简单地使用按钮(没有图像)吗?谢谢你的解决方案。。从未想过:)我稍后会尝试,并让您知道它是否有效;)谢谢你的帮助。。我会稍后再尝试,并让你知道它是否有效:)对不起,我反应太晚了。。你的代码运行得很好。。谢谢