kingBook

导航

泛型

private void Start() {
	SpriteRenderer renderer=createObjAddComponent<SpriteRenderer>("testObj");
	Debug.Log(renderer);//output: testObj (UnityEngine.SpriteRenderer)
}

private T createObjAddComponent<T>(string name)where T:Component{
	GameObject obj=new GameObject(name);
	T component=obj.AddComponent<T>();
	return component;
}

posted on 2019-05-24 17:06  kingBook  阅读(80)  评论(0编辑  收藏  举报