首先,我知道Unity有FindObjectOfType()函式,但我不想用這個。
我做了一種 "ReferenceManager",用于我在unity中所有的腳本參考。 CameraManager、EventManager、UIManager等等。
這些 "管理器 "之間從不交流,它們總是像這樣交流。 例如,CameraManager --> ReferenceManager --> EventManager
。目前,我在ReferenceManager的公共結構Reference中持有所有管理器的參考。
我的問題是: 有什么好方法可以讓我有一個方法來回傳我想要的型別的管理器嗎?
我目前的代碼 :
public class ReferenceManager : MonoBehaviour
{
[SerializeField ]
public References = new References(); //所有參考都經過這個結構。
public struct References
{
[field: SerializeField] 。
public CameraManager cameraManager { get; set; }
[field: SerializeField] 。
public UIManager uiManager { get; set; }
}
private void Awake()
{
AssignReferences()。
}
void AssignReferences()
{
List<ISystemComponent> systemComponentsList = Reference.GetSystemComponents()。
foreach(ISystemComponent sC in systemComponentsList)
{
if(sC.GetType() == typeof(CameraManager))
{
references.cameraManager = sC as CameraManager;
}
if(sC.GetType() == typeof(UIManager) >)
{
references.uiManager = sC as UIManager;
}
}
}
}
public static class Reference
{
static List<ISystemComponent> systemComponentList = new List<ISystemComponent>(); /i hold all Managers in this list.
public static void SubscribeSystemComponent(ISystemComponent sC)
{
systemComponentList.Add(sC)。
}//任何管理器都可以通過此方法添加到串列中。
public static List<ISystemComponent> GetSystemComponents()
{
return systemComponentList;
}
}
public interface ISystemComponent//this connects all managers with interface
public class CameraManager 。MonoBehaviour, ISystemComponent /first manager[/span
{
private void Awake()
{
Reference.SubscribeSystemComponent(this)。
}
}
public class UIManager 。MonoBehaviour, ISystemComponent //s second manager
{
ReferenceManager rM;
CameraManager cM;
private void Awake()
{
Reference.SubscribeSystemComponent(this)。
}
private void Start()
{
GetReferences()。
}
void GetReferences()
{
ReferenceManager rM = FindObjectOfType<ReferenceManager>()。
cM = rM.references.cameraManager; //這樣,我目前連接了2個管理器 1個管理器 ---> ReferenceManager ---> 2個管理器
}
}
我想制作一個通用方法,該方法將始終回傳我將呼叫的Manager型別,類似這樣的方法:
/Desired example
ISystemComponent GetReference(/* Ispecify here somehow what type of manager i want*/)
{
return /*based of my specification that type will be returned by this method*/}
}
我很樂意聽取任何意見,謝謝。
uj5u.com熱心網友回復:
電話:
GetISystemComponent<MazeManager> ();
方法:
public static T GetISystemComponent<。 T>() where T : ISystemComponent
{
foreach(ISystemComponent sC in systemComponentList)
{
if(sC.GetType().Equals(typeof(T))
{
return (T)sC;
}
}
Debug.LogError(typeof(T) " - 此組件型別不存在于ISystemComponent的串列中")。
return default(T);
}
uj5u.com熱心網友回復:
我相信Everts的建議是使用一個Dictionary<Type, ISystemComponent>:
public class ReferenceManager : MonoBehaviour
{
[SerializeField ]
public References = new References(); //所有參考都經過這個結構。
public struct References
{
[field: SerializeField] 。
public CameraManager cameraManager { get; set; }
[field: SerializeField] 。
public UIManager uiManager { get; set; }
}
private void Awake()
{
AssignReferences()。
}
void AssignReferences()
{
var systemComponentsList = Reference.GetSystemComponents().Value;
foreach(ISystemComponent sC in systemComponentsList)
{
if(sC.GetType() == typeof(CameraManager))
{
references.cameraManager = sC as CameraManager;
}
if(sC.GetType() == typeof(UIManager) >)
{
references.uiManager = sC as UIManager;
}
}
}
}
public static class Reference
{
private static Dictionary<Type, ISystemComponent> systemComponentDict = new(); /i hold all Managers in this listpublic static void SubscribeSystemComponent(ISystemComponent sC)
{
systemComponentDict.Add(sC.GetType(), sC)。
}//任何管理器都可以通過這個方法添加到串列中。
public static Dictionary<Type, ISystemComponent> GetSystemComponents()
{
return systemComponentDict;
}
public static T GetISystemComponent< T>() where T : ISystemComponent
{
if(!systemComponentsDict.TryGetValue(typeof(T), out var component)
{
Debug.LogError(typeof(T) " - 此組件型別不存在于ISystemComponent的串列中" )。
component = default(T)。
}
return component。
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/331781.html
標籤:
