基本上,您将如何创建一个公共无效的MethodToDoStuff(),附加一个单行为脚本并将该方法链接到一个按钮上,以便在单击该按钮时调用该方法“ MethodToDoStuff”。
现在我想通过编辑器脚本来做到这一点。
我可以手动执行此操作,但是拥有编辑脚本非常有效。
这是我到目前为止所拥有的:
感谢所有帮助,谢谢!
编辑脚本:
void OnWizardCreate()
{
doesHandler = GameObject.FindWithTag("WhatItDoes").GetComponent<WhatThisDoes>();
GameObject selection = Selection.activeGameObject;
EventTrigger trig=(EventTrigger)selection.AddComponent(typeof(EventTrigger));
EventTrigger.Entry onPointerDown = new EventTrigger.Entry();
onPointerDown.eventID = EventTriggerType.PointerDown;
EventTrigger.Entry onPointerUp = new EventTrigger.Entry();
onPointerUp.eventID = EventTriggerType.PointerUp;
trig.triggers.Add(onPointerDown);
trig.triggers.Add(onPointerUp);
}
其他脚本:
public void HidePanel()
{
whatItDoesPanel.SetActive(false);
}
答案 0 :(得分:0)
在运行时,您通常会打电话给
onPointerDown.AddListener(doesHandler.HidePanel);
但是这只会暂时添加侦听器。
添加一个持久的侦听器要复杂一些,但是幸运的是现在有了一个工具:UnityEventTools.AddPersistentListener
所以afaik您只需要添加
UnityEventTools.AddPersistentListener(onPointerDown, doesHandler.HidePanel);
UnityEventTools.AddPersistentListener(onPointerUp, doesHandler.HidePanel);
您还应该在使用Undo.RecordObject
之前使用其他功能,以将更改后的对象和场景标记为脏,并添加一个撤消/重做条目,如
Undo.RecordObject(selection, "Added event triggers");
大概是这样的
void OnWizardCreate()
{
doesHandler = Object.FindObjectOfType<WhatThisDoes>();
if(!doesHandler)
{
Debug.LogWarning("No WhatThisDoes found in the scene -> Ignored");
return;
}
var selection = Selection.activeGameObject;
if(!selection)
{
Debug.LogWarning("Nothing selected -> Ignored")
return;
}
if(selection.GetComponent<EventTrigger>())
{
Debug.LogWarning($"The selected object {selection} already has an EventTrigger attached! -> Ignored");
return;
}
// log the undo before making changes
Undo.RecordObject(selection, "Added event triggers");
var onPointerDown = new EventTrigger.Entry();
onPointerDown.eventID = EventTriggerType.PointerDown;
UnityEventTools.AddPersistentListener(onPointerDown, doesHandler.HidePanel);
var onPointerUp = new EventTrigger.Entry();
onPointerUp.eventID = EventTriggerType.PointerUp;
UnityEventTools.AddPersistentListener(onPointerUp, doesHandler.HidePanel);
var trig = selection.AddComponent<EventTrigger>();
trig.triggers.Add(onPointerDown);
trig.triggers.Add(onPointerUp);
}