/// <summary> /// 当被拖拽物体碰到一个指定物体时,执行 Action; /// </summary> /// <param name="go"></param> /// <param name="collider_Go"></param> /// <param name="action"></param> /// <param name="dontSetPos"></param> /// <param name="dontDestroy"></param> public void SetAction(GameObject go, GameObject collider_Go, Action action, EventParames EventParames = 0) { base.SetAction(go, action, EventParames); this.collider_Go = collider_Go; GetPosition(); }
/// <summary> /// 为被操作物体设置参数; /// </summary> /// <param name="go"></param> /// <param name="action"></param> /// <param name="destroy"></param> public virtual void SetAction(GameObject go, Action action, EventParames EventParames = 0) { this.go = go; this.action = action; this.EventParames = EventParames; }
/// <summary> /// 对事件的类型进行设置; T 为有参数的委托; /// </summary> /// <typeparam name="T"></typeparam> /// <param name="eventType"></param> /// <param name="gos"></param> /// <param name="function"></param> /// <param name="parameters"></param> /// <param name="eventsManager"></param> public static void SetFunc <T>(EventsType eventType, GameObject[] gos, T function, string parameters, EventParames EventParames) where T : class { UnityEvent unityEvent = new UnityEvent(); if (typeof(T) == typeof(Action <string>)) { unityEvent.AddActKeyPair(parameters, (Action <string>)(object) function); } else { throw new Exception("事件委托机制不支持该函数类型!"); } SetFunc(unityEvent, eventType, gos, EventParames); }
/// <summary> /// 根据名字获得物体;并且提取事件参数;注意:如果有多个物体,则返回一个数组; /// </summary> /// <param name="gameObjectName"></param> /// <param name="dontDestroy"></param> /// <returns></returns> public static GameObject[] GetGameObject(string gameObjectName, out EventParames EventParames) { gameObjectName = gameObjectName.ToLower(); EventParames = 0; if (gameObjectName.Contains(",~destroy")) { EventParames |= EventParames.DontDestory; gameObjectName = gameObjectName.Replace(",~destroy", ""); } if (gameObjectName.Contains(",~order")) { EventParames |= EventParames.NoOrder; gameObjectName = gameObjectName.Replace(",~order", ""); } if (gameObjectName.Contains(",~setpos")) { EventParames |= EventParames.DontSetTrue; gameObjectName = gameObjectName.Replace(",~setpos", ""); } // 返回所有与事件相关的物体; string[] gameObjectNames = gameObjectName.Split(','); GameObject[] gameObjects = new GameObject[gameObjectNames.Length]; for (int i = 0; i < gameObjectNames.Length; i++) { gameObjects[i] = GetGameObject(gameObjectNames[i]); } return(gameObjects); }
/// <summary> /// 为第一个物体设置触发器, 当两个物体相接触,就会执行 Action; /// /// 要求两个物体都必须要有 Collider;否则,将抛出异常; /// </summary> /// <param name="go1"></param> /// <param name="go2"></param> /// <param name="action"></param> public static void OnTrigger(this GameObject go1, GameObject go2, Action action, EventParames EventParames = 0) { if (!go1.GetComponent <Collider>() || !go2.GetComponent <Collider>()) { throw new Exception("缺少Collider!"); } if (action == null) { throw new Exception("Action 不能为空"); } go1.AddComponent <OnTrigger>().SetAction(go1, go2, action, EventParames); }
public static void OnMouseDoubleClick(this GameObject gameObject, Action <string> action, string parameters, EventParames EventParames = 0) { EventManager.SetFunc(EventsType.LeftMouseDoubleClick, new[] { gameObject }, action, parameters, EventParames); }
public static void OnMouseRightUp(this GameObject gameObject, Action <string> action, string parameters, EventParames EventParames = 0) { EventManager.SetFunc(EventsType.RightMouseUp, new[] { gameObject }, action, parameters, EventParames); }
public static void OnMouseRightDown(this GameObject gameObject, Action action, EventParames EventParames = 0) { EventManager.SetFunc(EventsType.RightMouseDown, new[] { gameObject }, action, EventParames); }
public static void OnMouseLeftUp(this GameObject gameObject, Action action, EventParames EventParames = 0) { EventManager.SetFunc(EventsType.LeftMouseUp, new[] { gameObject }, action, EventParames); }
public static void OnMouseDrag(this GameObject gameObject, GameObject gameObject2, Action <string> function, string parameters, EventParames EventParames = 0) { EventManager.SetFunc(EventsType.MouseDrag, new[] { gameObject, gameObject2 }, function, parameters, EventParames); }
public static void OnMouseDrag(this GameObject gameObject, GameObject gameObject2, EventParames EventParames = 0) { EventManager.SetFunc(EventsType.MouseDrag, new[] { gameObject, gameObject2 }, new Action(() => {}), EventParames); }
/// <summary> /// 添加鼠标拖拽事件;添加事件完成时的 Action 函数; /// </summary> /// <param name="gameObject"></param> /// <param name="action"></param> public static void OnMouseDrag(this GameObject gameObject, Action function, EventParames EventParames = 0) { EventManager.SetFunc(EventsType.MouseDrag, new[] { gameObject }, function, EventParames); }
/// <summary> /// 对事件的类型进行设置; T 为无参数的委托; /// </summary> /// <typeparam name="T"></typeparam> /// <param name="eventType"></param> /// <param name="gos"></param> /// <param name="function"></param> /// <param name="eventsManager"></param> public static void SetFunc <T>(EventsType eventType, GameObject[] gos, T function, EventParames eventsManager) where T : class { UnityEvent unityEvent = new UnityEvent(); if (typeof(T) == typeof(Action)) { unityEvent.action = (Action)(object)function; } else if (typeof(T) == typeof(Func <bool>)) { unityEvent.func = (Func <bool>)(object) function; } else if (typeof(T) == typeof(Nullable)) { UnityTool.Log("OK……"); } else { throw new Exception("事件委托机制不支持该函数类型!"); } SetFunc(unityEvent, eventType, gos, eventsManager); }
/// <summary> /// 设置事件类型 /// </summary> /// <param name="unityEvent"></param> /// <param name="eventType"></param> /// <param name="gos"></param> /// <param name="eventsManager"></param> private static void SetFunc(UnityEvent unityEvent, EventsType eventType, GameObject[] gos, EventParames EventParames) { foreach (var go in gos) { if (!go.GetComponent <Collider>()) { throw new Exception(go.name + "物体没有碰撞器,不能被检测!"); } } unityEvent.go = gos; unityEvent.EventType = eventType; unityEvent.EventParames = EventParames; if ((EventParames & EventParames.NoOrder) != 0) { // 这里向事件脚本中传递的是 客户端的Action; AddEvent(unityEvent, unityEvent.action); return; } eventQueue.Enqueue(unityEvent); if (begin) { NextTool(); begin = false; } }
/// <summary> /// 重写父类的方法,记下初始位置; /// </summary> /// <param name="go"></param> /// <param name="action"></param> /// <param name="destroy"></param> public override void SetAction(GameObject go, Action action, EventParames EventParames = 0) { base.SetAction(go, action, EventParames); GetPosition(); }
/// <summary> /// 添加帧监听事件; /// </summary> /// <param name="gameObject"></param> /// <param name="action"></param> /// <param name="EventParames"></param> public static void OnUpdate(this GameObject gameObject, Action action, EventParames EventParames = 0) { EventManager.SetFunc(EventsType.OnUpdate, new[] { gameObject }, action, EventParames); }