In general see Type.AssemblyQualifiedName
.
If you don't have that you will need to at least have a reference to the Assembly in question and then use Assembly.GetType
.
You can also get the assembly if you know at least one type from the according assembly via Assembly.GetAssembly(theKnownType)
That said, you can not use the generic version GameObject.AddComponent<T>()
since the type-parameter of generics need to be compile-time constant!
You can however simply use the non-generic version of GameObject.AddComponent(Type)
public void Build(string compToAdd)
{
Type compToAddType = Type.GetType(compToAdd);
slot.AddComponent(compToAddType);
}
(Actually there even was an overload directly taking a string
as parameter but it was deprecated.)
Finally I personally would avoid it completely if possible! Instead of relying on your strings being correct, why not rather use e.g.
public void buildFoundation()
{
slot.AddComponent<FoundationComponnet>();
}
public void buildTurret()
{
slot.AddComponent<TurretComponent>();
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…