关于Unity3D 泛型使用总结

****************************************************可以直接使用*********************************************************

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MyUtilTool : MonoBehaviour
{
    public GameObject Obj;
    public GameObject ParentObj;
    public Vector3 Vec3;

    GameObject mObj;
    GameObject mParentObj;
    Vector3 mVec3;
    
    private Rigidbody rig;
    public MyUtilTool(GameObject obj, GameObject ParentObj, Vector3 vec3)
    {
        this.mObj = obj;
        this.mParentObj = ParentObj;
        this.mVec3 = vec3;
    }

    public void InstantiateObj<T>(T t) where T : Component
    {
        mObj = Instantiate(mObj, mVec3, Quaternion.identity) as GameObject;
        mObj.AddComponent<T>();
        mObj.transform.parent = mParentObj.transform;
        mObj.transform.localScale = Vector3.one;
    }
    void Start()
    {
        MyUtilTool mc = new MyUtilTool(Obj, ParentObj, Vec3);
        mc.InstantiateObj<Rigidbody>(rig);
    }

}  


******************************************************静态工具类*************************************************

using System.Collections;
using System.Collections.Generic;
using UnityEngine;


public static  class UtilTool 
{
   public static void ObjT<T>(this GameObject gameObject) where T : Component
   {
        gameObject.AddComponent<T>();
        gameObject.transform.localScale = Vector3.one;
   }
       
}  

调用方式(可以直接访问使用)

 public GameObject obj;
    
void Start () {
        obj.ObjT<Rigidbody>();

}


posted @ 2018-06-05 18:20  低小调  阅读(169)  评论(1编辑  收藏  举报