Unity 复制组件

Unity 复制组件

 private void GetCompotent(GameObject objStart, GameObject objEnd)
    {
    	//获取组件 
        Text anim = objStart.GetComponent();

		//编辑器模式下可运行,发布后不可
        UnityEditorInternal.ComponentUtility.CopyComponent(anim);
        UnityEditorInternal.ComponentUtility.PasteComponentAsNew(objEnd);

		//发布后可运行,win和Android皆可
        System.Type type = anim.GetType();   
        Component copy = objEnd.AddComponent(type);
        System.Reflection.FieldInfo[] fields = type.GetFields();
        foreach (System.Reflection.FieldInfo field in fields)
        {
            field.SetValue(copy, field.GetValue(anim));
        }
    }

你可能感兴趣的:(Unity,unity)