有没有办法使用脚本从Gameobject中删除组件?
例如:
我通过脚本向玩家添加public void deleteSMS(Context ctx, String message, String number) {
try {
Uri uriSms = Uri.parse("content://sms");
Cursor c = ctx.getContentResolver().query(uriSms, new String[] { "_id", "thread_id", "address", "person", "date", "body" }, null, null, null);
if (c != null && c.moveToFirst()) {
do {
long id = c.getLong(0);
long threadId = c.getLong(1);
String address = c.getString(2);
String body = c.getString(5);
String date = c.getString(3);
Log.e("log>>>", "0>" + c.getString(0) + "1>" + c.getString(1) + "2>" + c.getString(2) + "<-1>" + c.getString(3) + "4>" + c.getString(4)+ "5>" + c.getString(5));
// Log.e("log>>>", "date" + c.getString(0));
// if (body.contains(getResources().getText(R.string.invite_text).toString()) && address.equals(number)) {
if (message.equals(body) && address.equals(number)) {
// mLogger.logInfo("Deleting SMS with id: " + threadId);
int rows = ctx.getContentResolver().delete(Uri.parse("content://sms/" + id), "date=?",new String[] { c.getString(4) });
Log.e("log>>>", "Delete success......... rows: "+rows);
Log.e("log>>>", "Delete success......... body: "+body);
}
} while (c.moveToNext());
}
} catch (Exception e) {
//Log.e("log>>>", e.toString());
//Log.e("log>>>", e.getMessage());
throw e;
}
}
,将对象连接到它(用于抓取),当我放弃它时我想删除FixedJoint(因为,我不能只是“禁用”关节)。我该怎么办?
答案 0 :(得分:7)
是的,您使用Destroy
函数来销毁/删除GameObject中的组件。它可用于删除Component或GameObject。
添加组件:
gameObject.AddComponent<FixedJoint>();
删除组件:
FixedJoint fixedJoint = GetComponent<FixedJoint>();
Destroy(fixedJoint);
答案 1 :(得分:3)
为了试验程序员的正确答案,我创建了一个扩展方法,这样你就可以使用gameObject.RemoveComponent(/ *如果是立即* /,则为true),因为我觉得应该有这样的方法。
如果您想要使用它,您可以使用以下代码在任何地方创建新课程:
using UnityEngine;
public static class ExtensionMethods
{
public static void RemoveComponent<Component>(this GameObject obj, bool immediate = false)
{
Component component = obj.GetComponent<Component>();
if (component != null)
{
if (immediate)
{
Object.DestroyImmediate(component as Object, true);
}
else
{
Object.Destroy(component as Object);
}
}
}
}
然后像你一样使用AddComponent&lt;&gt;()
gameObject.RemoveComponent<FixedJoint>();
可以通过任何扩展MonoBehaviour的方法访问它。您还可以向此静态扩展类添加更多方法,只需使用&#34; this&#34; -syntax作为参数来扩展某个Unity类型。例如,如果您添加以下方法(来自extension method tutorial)
public static void ResetTransformation(this Transform trans)
{
trans.position = Vector3.zero;
trans.localRotation = Quaternion.identity;
trans.localScale = new Vector3(1, 1, 1);
}
您可以在任何脚本中使用transform.ResetTransformation();
来调用它。 (使班级看起来像:)
using UnityEngine;
public static class ExtensionMethods
{
public static void RemoveComponent<Component>(this GameObject obj, bool immediate = false)
{
Component component = obj.GetComponent<Component>();
if (component != null)
{
if (immediate)
{
Object.DestroyImmediate(component as Object, true);
}
else
{
Object.Destroy(component as Object);
}
}
}
public static void ResetTransformation(this Transform trans)
{
trans.position = Vector3.zero;
trans.localRotation = Quaternion.identity;
trans.localScale = new Vector3(1, 1, 1);
}
}