private EventSystem myEventSystem;
public static GameNotes Instance { get; private set; }
private void Awake()
{
if (Instance == null)
{
Instance = this;
DontDestroyOnLoad(gameObject);
// Initialize your notes or load from file
savePath = Path.Combine(Application.persistentDataPath, "notes.txt");
LoadFromFile(); // Load notes if they exist
}
else
{
Destroy(gameObject); // Ensure only one instance exists
}
myEventSystem = GetComponentInChildren<EventSystem>(true);
}
myEventSystem 为空。
此屏幕截图显示了事件系统在场景中的位置。该脚本附加到 GameNotes GameObject。
我想获取 EventSystem 对象、游戏对象以及组件。两个都。
使用 GetComponentInChildren 不起作用,myEventSystem 为空。
T GetComponentInChildren<T>(bool includeInactive)
获取类型 的第一个子组件T
,如果未找到,则从父组件获取,否则返回 null。Component
除非您在具有所需组件的子级的父级上使用此方法,否则它将不起作用。你
EventSystem
没有父母,所以它不适用于你的情况。最好的选择是使用,它会查找场景中的T FindObjectOfType<T>(bool includeInactive) where T : Object
第一个Component
类型,否则返回。T
null
GameObject.
如果您在 中运行它,则可以避免MonoBehavior
,因为它源自GameObject
这会找到所需的
eventSystem
,假设场景中只有一个并且它处于活动状态。true
如果参数处于活动状态,请确保将其更改为。EventSystem
如果场景中有多个,请考虑使用tag
检查,同时查找EventSystems
场景中的所有内容。或者更快地使用
LINQ
: