【发布时间】:2018-03-16 20:11:19
【问题描述】:
我正在开发一个拖放系统并尝试实现一个挂钩系统(类似于 WordPress 使用的系统),以便在某些事件发生时触发操作。
我的委托函数有多个问题:
- 无法将
UIElementDragger.CallBackFunction表达式转换为ListObserver.CallBackFunction类型 -
ListObserver.ListObserver(string, ListObserver.CallBackFunction)的最佳重载方法匹配有一些无效参数
您对如何正确或以其他方式实现“挂钩系统”有任何建议,以便我可以使用某些自定义事件调用的带有参数的函数?
我已经截断了下面的代码以保留相关部分,但如果您需要全部内容,我很乐意提供。
绑定事件的代码是:
注意:UIElementDragger 附加到 EventSystem。
//... removed "using"
public class GameSyllab : MonoBehaviour {
//... removed attributes
private UIElementDragger UIElementDragger;
// Use this for initialization
void Start () {
//Hook the functions
UIElementDragger = GetComponent<UIElementDragger> ("UIElementDragger");
UIElementDragger.HookOnto("DropComplete", DropComplete);
UIElementDragger.HookOnto("DropCancel", DropCancel);
//... removed unrelated code
}
void DropComplete(Object objDraggable, Object objDropZone){
// Do an action based on the element being dropped
}
void DropCancel(Object objDraggable, Object objDropZone){
// Play a cancel sound
}
//...removed unrelated class code
添加可拖动行为的 UIElementDragger 类的代码:
注意:这个类也附加到 EventSystem
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
public class UIElementDragger : MonoBehaviour {
//A list with observers that are waiting for something to happen
List<ListObserver> observers = new List<ListObserver>();
public delegate void CallBackFunction(Object objDraggable, Object objDropZone);
// ... removed extra attributes
// Update is called once per frame
void Update () {
//... removed Mouse down code
if (Input.GetMouseButtonUp (0)) {
if (objectToDrag != null) {
Transform objectToDrop = GetDraggableTransformUnderMouse(DROPZONE_TAG);
if (objectToDrop != null) {
//Drop the dragged object on it's new drop zone
objectToDrag.position = objectToDrop.position;
NotifyObservers ("DropComplete", objectToDrag, objectToDrop);
} else {
//Move the item back to its original position
objectToDrag.position = originalPosition;
NotifyObservers ("DropCancelled", objectToDrag, null);
}
objectToDragImage.raycastTarget = true;
objectToDrag = null;
}
dragging = false;
}
}
//Send notifications if something has happened
public void NotifyObservers(string hookName, Object objectInstance, Object dropZoneInstance)
{
for (int i = 0; i < observers.Count; i++)
{
if (observers [i].GetHookName () == hookName) {
observers [i].Call (objectInstance, dropZoneInstance);
}
}
}
//Add observer to the list
public void HookOnto(string newHookName, CallBackFunction newCallBackFunction)
{
ListObserver newObserver = new ListObserver (newHookName, newCallBackFunction);
observers.Add(newObserver);
}
}
//In the same file, but a separate class
public class ListObserver:MonoBehaviour{
private string hookName;
public delegate void CallBackFunction(Object objDraggable, Object objDropZone);
private CallBackFunction callBackFunction;
public ListObserver(string newHookName, CallBackFunction newCallBackFunction){
hookName = newHookName;
callBackFunction = newCallBackFunction;
}
public void Call(Object objDraggable, Object objDropZone){
callBackFunction (objDraggable, objDropZone);
}
public string GetHookName(){
return hookName;
}
}
【问题讨论】:
标签: c# unity3d events delegates listener