处理输入、射线和发送事件。继承自UIBehaviour

The EventSystem is responsible for processing and handling events in a Unity scene. A scene should only contain one EventSystem. The EventSystem works in conjunction with a number of modules and mostly just holds state and delegates functionality to specific, overrideable components. When the EventSystem is started it searches for any BaseInputModules attached to the same GameObject and adds them to an internal list. On update each attached module receives an UpdateModules call, where the module can modify internal state. After each module has been Updated the active module has the Process call executed.This is where custom module processing can take place.

EventSystem的简单介绍和使用


静态属性:current 返回当前的EventSystem;set是将指定的eventSystem移到数组开头

private  static List<EventSystem> m_EventSystems = new List<EventSystem>();

/// <summary>
/// Return the current EventSystem.
/// </summary>
public static EventSystem current
{
    get { return m_EventSystems.Count > 0 ? m_EventSystems[0] : null; }
    set
    {
        int index = m_EventSystems.IndexOf(value);

        if (index >= 0)
        {
            m_EventSystems.RemoveAt(index);
            m_EventSystems.Insert(0, value);
        }
    }
}

在 OnEnable中,eventSystem将自身插入m_EventSystems

protected override void OnEnable()
{
    base.OnEnable();
    m_EventSystems.Add(this);
}

在OnDisable中,从m_EventSystems中移出,并清理非静态属性

 protected override void OnDisable()
{
    if (m_CurrentInputModule != null)
    {
        m_CurrentInputModule.DeactivateModule();
        m_CurrentInputModule = null;
    }

    m_EventSystems.Remove(this);

    base.OnDisable();
}

一个场景只有一个eventSystem,m_EventSystems还是设置为list,猜测目的是为了场景切换时,现在的场景OnDisable后current会立刻切换为list中的下一个。


UpdateModules

更新对baseInputModule的管理。由baseInputModule在onenable和onDisable时调用。获取所有激活状态的baseInputModule组件。

public void UpdateModules()
{
    GetComponents(m_SystemInputModules);
    for (int i = m_SystemInputModules.Count - 1; i >= 0; i--)
    {
        if (m_SystemInputModules[i] && m_SystemInputModules[i].IsActive())
            continue;

        m_SystemInputModules.RemoveAt(i);
    }
}

baseInputModule