【问题标题】:How do you reference UI Event System from script?你如何从脚本中引用 UI 事件系统?
【发布时间】:2015-06-20 11:11:59
【问题描述】:

我尝试创建一个公共变量,然后从脚本附加到的游戏对象的检查器中引用 EventSystem 对象,但是每当我尝试从 EventSystems 访问变量时,

我收到 NullReference 异常:对象引用未设置为对象的实例。

我尝试使用GameObject.Find() 找到它,但显然我无法使用内置转换将gameObject 转换为EventSystem.EventSystem

我不知道该怎么办。我只想访问

EventSystem.currentSelectedGameObject.name

找出在 Canvas 中选择了哪个 UI 对象。

【问题讨论】:

  • 我以为我使用 EventSystem.current.currentSelectedGameObject.name 得到了它,但我仍然得到相同的 NullReference 异常...
  • 显然EventSystem.current.currentSelectedGameObject 为所有内容返回 Null。我想这就是问题所在。我不明白为什么当我清楚地能够使画布中的 Scroll Rect 对象正常运行时,它认为我什么都没有选择。
  • 求助于Camera.main.ScreenPointToRay(Input.mousePosition)Physics.Raycast() 寻找游戏对象,因为事实证明EventSystem 比预期的要复杂。

标签: unity3d unityscript


【解决方案1】:

这是您想要的,请参阅文档here

EventSystem.current

【讨论】:

    猜你喜欢
    • 2019-10-27
    • 1970-01-01
    • 2010-09-22
    • 1970-01-01
    • 1970-01-01
    • 2010-09-28
    • 1970-01-01
    • 1970-01-01
    • 2020-03-12
    相关资源
    最近更新 更多