所有組件繼承BaseComponent.可直接調用GetBaseComponent.
/// <summary>
/// 獲取基礎組件
/// </summary>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
public static T GetBaseComponent<T>() where T : BaseComponent
{
return (T)GetBaseComponent(typeof(T));
}
/// <summary>
/// 獲取基礎組件
/// </summary>
/// <param name="type"></param>
/// <returns></returns>
internal static Component GetBaseComponent(Type type)
{
LinkedListNode<BaseComponent> curr = m_BaseComponentList.First;
while (curr != null)
{
if (curr.Value.GetType() == type)
return curr.Value;
curr = curr.Next;
}
return null;
}
Component1 與 Component2 在兩個組件繼承BaseComponent已經註冊m_BaseComponentList中
將對應物體下的組件腳本獲取到.
Component1 = GetBaseComponent<Component1>();
Component2= GetBaseComponent<Component2>();
寫的比較粗糙,並不可複製直接使用,近期有用到,把思想進行記錄.