- Home /
Generic method and factory pattern
Hi, I want to instantiate dynamicaly object with a generic factory pattern. The object must be inherit Enemy class. I cant pass GetType() in my generic method Thanks
public class Factory : MonoBehaviour {
public GameObject basic;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
public void Build<T>() where T : Enemy, new()
{
T t = new T();
GameObject go = (GameObject)Instantiate(basic,Vector3.zero, Quaternion.identity);
go.name = t.GetType().ToString();
go.GetComponent<MeshRenderer>().material = Resources.Load(go.name) as Material;
go.AddComponent<T>();
}
}
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class Detector : MonoBehaviour {
public List<Enemy> enemys = new List<Enemy>(); // content (Warrior, Paladin, Etc)
void OnTriggerEnter(Collider col)
{
if (col.tag == "Player")
{
Enemy e = enemys[Random.Range(0, 4)];
GetComponent<Factory>().Build<e.GetType()>(); // Compiler doesnt want that
}
}
}
public class Mage : Enemy {
}
public class Warrior: Enemy {
}
public class Paladin: Enemy {
}
Answer by Jamora · Mar 26, 2014 at 06:43 PM
You can't use generics like that. The passed type parameter must be a constant at compile time, so expressions aren't allowed.
The easiest fix would be to not use generics at all and pass the type as a parameter instead
public void Build(System.Type type){
if(!type.IsSubclassOf(typeof(Enemy))) return; //don't do anything if we didn't get an enemy-class
GameObject go = (GameObject)Instantiate(basic,Vector3.zero, Quaternion.identity);
go.name = type.ToString();
go.GetComponent<MeshRenderer>().material = Resources.Load(go.name) as Material;
go.AddComponent(type);
}
If you don't want to, or unexpected complications arise in your design from sending types to the factory, you could probably use with an enum instead, since you have a limited number of classes and they are unlikely to change. Enums can be easily converted to strings and then to types( using System.Type.GetType(classname); )
An alternative would be to have a list of all accepted types in the factory. You could set it up in Awake, or use reflection to fill it.
This solution is more elegant though. Disregard my suggestion.
Answer by moxiewhimsy · Mar 26, 2014 at 05:13 PM
Try using
go.AddComponent( go.name );
or more directly
go.AddComponent( t.GetType().ToString() );
The string must be the exact name of the component type you'd like to add.
edit: Ahh. I see. IMHO, a protected enum would probably be the simplest solution.
Thx for your response but the problem is in OnTriggerEnter methods.
GetComponent<Factory>().Build<e.GetType()>(); //e.GetType() prevents to compile
Answer by bakman · Apr 02, 2014 at 09:12 AM
I found the solution with reflexion. Thx for responses @Jamora yes I think pass the parameters to System.Type is a good solution if we cant do with reflexion. and yes enum is pretty good way if you dont have lot of class. Bye
int result = Random.Range(0, enemys.Count);
var t = typeof(Factory);
var e = enemys[result];
var m = t.GetMethod("Build");
var build = m.MakeGenericMethod(e.GetType());
var factory = GetComponent<Factory>();
build.Invoke(factory,null);
Your answer
Follow this Question
Related Questions
Generic Component Declaration 1 Answer
Animation Preview 0 Answers
Non-humanoid Rig Gets Mangled in Mecanim 0 Answers
Generic Method help 1 Answer