拡張エディターからButtonコンポーネントにRigidBody.useGrabity=falseを設定しようとしています
Bool型なのでAddBoolPersistentListenerを使用すれば良いはずなのですが、第二引数が間違っているようで、動作しません
ダメだった例
●Target_Obj.GetComponent<Rigidbody>().useGravity
●Rigidbody test = Target_Obj.GetComponent<Rigidbody>();
test.useGravity
●Target_Obj(Rigidbody).useGravity
現在、
https://stackoverflow.com/questions/45589347/unity-add-second-persistentlistener-not-working
を参考にDelegateというものを使う方法を試してみたところ、コンパイルは通りますが実行時にエラーが出てしまいます
どう書き換えるべきでしょうか?
コード全文
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.UI;
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
public class SetUGUI_02 : EditorWindow //!< EditorWindowを継承してね!
{
//! MenuItem("メニュー名/項目名") のフォーマットで記載してね
[MenuItem("Tools/SetUGUI_02")]
static void ShowWindow() { // ウィンドウを表示! EditorWindow.GetWindow<SetUGUI_02>();
}
GameObject Target_Obj = null; void OnGUI() { Target_Obj = (GameObject)EditorGUILayout.ObjectField( "Target_Obj" , Target_Obj , typeof(GameObject), true ); if( GUILayout.Button( "SetEvents",EditorStyles.miniButtonRight ) ){ Test(); } } void Test(){ GameObject Select_Obj = Selection.activeGameObject; Button btn = Select_Obj.GetComponent<Button>();
//Delegate
UnityAction<bool> methodDelegateBool = System.Delegate.CreateDelegate(typeof(UnityAction<bool>), Target_Obj.GetComponent<Rigidbody>(), "useGravity") as UnityAction<bool>;
UnityEditor.Events.UnityEventTools.AddBoolPersistentListener(btn.onClick, Select_Obj.SetActive, true); UnityEditor.Events.UnityEventTools.AddObjectPersistentListener(btn.onClick, Select_Obj.transform.SetParent, Target_Obj.transform);
//useGravityをfalseにUnityEditor.Events.UnityEventTools.AddBoolPersistentListener(btn.onClick, methodDelegateBool, false);
}
}
エラーメッセージ
ArgumentException: Couldn't bind to method 'useGravity'.
System.Delegate.GetCandidateMethod (System.Type type, System.Type target, System.String method, BindingFlags bflags, Boolean ignoreCase, Boolean throwOnBindFailure) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System/Delegate.cs:351)
System.Delegate.CreateDelegate (System.Type type, System.Object target, System.String method, Boolean ignoreCase, Boolean throwOnBindFailure) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System/Delegate.cs:397)
System.Delegate.CreateDelegate (System.Type type, System.Object target, System.String method, Boolean ignoreCase) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System/Delegate.cs:406)
System.Delegate.CreateDelegate (System.Type type, System.Object target, System.String method) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System/Delegate.cs:300)
SetUGUI_02.Test () (at Assets/MyScript/SetUGUI_02.cs:29)
SetUGUI_02.OnGUI () (at Assets/MyScript/SetUGUI_02.cs:23)
System.Reflection.MonoMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Reflection/MonoMethod.cs:222)
Rethrow as TargetInvocationException: Exception has been thrown by the target of an invocation.
System.Reflection.MonoMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Reflection/MonoMethod.cs:232)
System.Reflection.MethodBase.Invoke (System.Object obj, System.Object[] parameters) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Reflection/MethodBase.cs:115)
UnityEditor.HostView.Invoke (System.String methodName, System.Object obj) (at C:/buildslave/unity/build/Editor/Mono/HostView.cs:295)
UnityEditor.HostView.Invoke (System.String methodName) (at C:/buildslave/unity/build/Editor/Mono/HostView.cs:288)
UnityEditor.HostView.InvokeOnGUI (Rect onGUIPosition) (at C:/buildslave/unity/build/Editor/Mono/HostView.cs:255)
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/03/18 16:26