Unity 일반 T 사용 방법
1346 단어 Unity 기술
using UnityEngine;
using System.Collections;
// :
public class ActivityManageData:MonoBehaviour where T:MonoBehaviour
{
private T intance;
public void InstantiateObj(GameObject obj,GameObject ParentObj,Vector3 vec3)
{
GameObject mObj = Instantiate (obj, vec3, Quaternion.identity) as GameObject;
intance=mObj.AddComponent();
mObj.transform.parent = ParentObj.transform;
mObj.transform.localScale = Vector3.one;
}
}
4
// :
public class MyContent:MonoBehaviour
{
GameObject mObj;
GameObject mParentObj;
Vector3 mVec3;
public MyContent(GameObject obj,GameObject ParentObj,Vector3 vec3)
{
this.mObj = obj;
this.mParentObj = ParentObj;
this.mVec3 = vec3;
}
public void InstantiateObj(T t) where T:Component
{
mObj = Instantiate (mObj, mVec3, Quaternion.identity) as GameObject;
mObj.AddComponent();
mObj.transform.parent = mParentObj.transform;
mObj.transform.localScale = Vector3.one;
}
}
호출할 때 두 가지 방식으로 실례화 대상이 범형 T에 구체적인 유형을 지정하면 OK.