Example #1
0
    void Init()
    {
        ClickableManager2D.BindClickEvent(gameObject, delegate(GameObject go, Vector3 pos) {
            Debug.Log("Clicka");
            return;

            mainCtrl.HidePop();
            Vector3 posInWorld = clickableManager.m_camera.ScreenToWorldPoint(pos);
            Vector3 posLocal   = transform.InverseTransformPoint(posInWorld);
            posLocal.z         = 0;
            if (Mark != null)
            {
                Mark.transform.localPosition = posLocal;
            }
            player.FinishFollowPath();

            List <Vector2> path = StartSearch(player.transform.localPosition, posLocal);
            if (path.Count > 0)
            {
                player.FollowPath(path);
            }
        });
        {
            ClickableEventlistener2D listener = gameObject.AddComponent <ClickableEventlistener2D>();
            listener.BeginDragEvent += delegate(GameObject gb, Vector3 dragDir) {
            };

            listener.OnDragEvent  += delegate(GameObject gb, Vector3 dragDir) {
            };
            listener.EndDragEvent += delegate(GameObject gb, Vector3 dragDir) {
            };
        }
    }
Example #2
0
    public static void BindClickEvent(GameObject target, ClickableEventlistener2D.FieldEventProxy Func)
    {
        ClickableEventlistener2D listener = target.GetComponent <ClickableEventlistener2D>();

        if (listener == null)
        {
            listener             = target.AddComponent <ClickableEventlistener2D>();
            listener.ClickEvent += Func;
        }
    }
Example #3
0
 public void RegisterEvent()
 {
     {
         ClickableEventlistener2D listener = gameObject.GetComponent <ClickableEventlistener2D>();
         if (listener == null)
         {
             listener             = gameObject.AddComponent <ClickableEventlistener2D>();
             listener.ClickEvent += delegate {
                 gm.ChoosePot(this);
             };
             Debug.Log(listener.hasClickEvent());
         }
     }
 }
Example #4
0
    public void SetMap()
    {
        {
            ClickableEventlistener2D listener = Map.AddComponent <ClickableEventlistener2D> ();
            listener.BeginDragEvent += delegate(GameObject gb, Vector3 dragDir) {
                isMovingCamera         = true;
                isContinueMovingCamera = false;
                CameraToMove           = mainCamera.transform.localPosition;
            };

            listener.OnDragEvent += delegate(GameObject gb, Vector3 dragDir) {
                UpdateMoveTarget(dragDir);
            };
            listener.EndDragEvent += delegate(GameObject gb, Vector3 dragDir) {
                isContinueMovingCamera = true;
                isMovingCamera         = false;
            };
        }
    }
Example #5
0
    //GameObject[] clickedObjs;

    public void checkTouch()
    {
        //		if (!Stage.isTouchOnUI)
        //		{
        //			RaycastHit hit;
        //			Ray ray = Camera.main.ScreenPointToRay(new Vector2(Stage.inst.touchPosition.x, Screen.height - Stage.inst.touchPosition.y));
        //			if (Physics.Raycast(ray, out hit))
        //			{
        //				if (hit.transform == cube)
        //				{
        //					Debug.Log("Hit the cube");
        //				}
        //			}
        //		}
        if (Input.GetMouseButtonDown(0) || (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began))
        {
                        #if IPHONE || ANDROID
            if (EventSystem.current.IsPointerOverGameObject(Input.GetTouch(0).fingerId))
            {
                        #else
            //Debug.Log(Stage.inst.touchTarget.gameObject.name);
            if (EventSystem.current.IsPointerOverGameObject())
            {
#endif
                Debug.Log("鼠标在ui上");
                ;
            }
            else
            {
                if (nowMode == MouseState.NONE)
                {
                    Vector3      pos  = m_camera.ScreenToWorldPoint(Input.mousePosition);
                    RaycastHit[] hits = null;
                    hits = Physics.RaycastAll(pos, Vector3.forward, Mathf.Infinity);
                    if (hits.Length > 1)                          //检测是否射线接触物体
                    {
                        mouseDownPos   = Input.mousePosition;
                        nowClickedList = new List <GameObject>();
                        List <RaycastHit> hitsList = new List <RaycastHit>(hits);

                        hitsList.Sort((x, y) => {
                            return(x.distance.CompareTo(y.distance));
                        });
                        for (int i = 0; i < hits.Length; i++)
                        {
                            nowClickedList.Add(hits[i].collider.gameObject);
                        }
                        //nowClickGO = hits [0].collider.gameObject;
                        nowMode = MouseState.CLICK;
                    }
                    else if (hits.Length > 0)
                    {
                        mouseDownPos   = Input.mousePosition;
                        nowClickedList = new List <GameObject>()
                        {
                            hits[0].collider.gameObject
                        };
                        nowMode = MouseState.CLICK;
                    }
                }
            }
        }

        if (Input.GetMouseButton(0))
        {
            Vector3 mouseMove = (Input.mousePosition - mouseDownPos);
            mouseMove.z = 0;
            if (nowMode == MouseState.NONE)
            {
            }
            if (nowMode == MouseState.CLICK)
            {
                if (mouseMove.magnitude < dragYuzhi)
                {
                    //仍是点击
                }
                else
                {
                    //超过阀值 变为拖动
                    nowMode     = MouseState.DRAG;
                    lastDragPos = Input.mousePosition;

                    if (nowClickedList == null || nowClickedList.Count == 0 || nowClickedList[0].GetComponentInParent <ClickableEventlistener2D> () == null)
                    {
                        //无法回调
                    }
                    else
                    {
                        nowClickedList[0].GetComponentInParent <ClickableSprite> ().startDrag(lastDragPos);
                    }
                }
            }
            else if (nowMode == MouseState.DRAG)
            {
                Vector3 nowPos = Input.mousePosition;
                Vector3 delta  = nowPos - lastDragPos;
                if (nowClickedList == null || nowClickedList.Count == 0 || nowClickedList[0].GetComponentInParent <ClickableSprite> () == null)
                {
                    //无法回调
                }
                else
                {
                    nowClickedList[0].GetComponentInParent <ClickableSprite> ().onDrag(delta);
                }
                lastDragPos = nowPos;
            }
        }



        if (Input.GetMouseButtonUp(0))
        {
            if (nowMode == MouseState.NONE)
            {
            }
            else if (nowMode == MouseState.CLICK)
            {
                if (nowClickedList == null || nowClickedList.Count == 0 || nowClickedList[0].GetComponentInParent <ClickableSprite> () == null)
                {
                    //无法回调
                }
                else
                {
                    {
                        for (int i = 0; i < nowClickedList.Count; i++)
                        {
                            ClickableEventlistener2D cp = nowClickedList[i].GetComponentInParent <ClickableEventlistener2D>();
                            if (cp == null || !cp.hasClickEvent())
                            {
                                continue;
                            }
                            if (cp != null && cp.hasClickEvent())
                            {
                                cp.onClick(Input.mousePosition);
                                if (FieldEventResult == eFieldEventResult.Block)
                                {
                                    break;
                                }
                            }
                        }
                    }
                    FieldEventResult = eFieldEventResult.Block;
                }
            }
            else if (nowMode == MouseState.DRAG)
            {
                if (nowClickedList == null || nowClickedList.Count == 0 || nowClickedList[0].GetComponentInParent <ClickableSprite> () == null)
                {
                    //无法回调
                }
                else
                {
                    nowClickedList[0].GetComponentInParent <ClickableSprite> ().endDrag(Input.mousePosition);
                }
            }
            nowMode = MouseState.NONE;
        }
    }
}
Example #6
0
    public void checkTouch()
    {
        //		if (!Stage.isTouchOnUI)
        //		{
        //			RaycastHit hit;
        //			Ray ray = Camera.main.ScreenPointToRay(new Vector2(Stage.inst.touchPosition.x, Screen.height - Stage.inst.touchPosition.y));
        //			if (Physics.Raycast(ray, out hit))
        //			{
        //				if (hit.transform == cube)
        //				{
        //					Debug.Log("Hit the cube");
        //				}
        //			}
        //		}
        if (Input.GetMouseButtonDown(0) || (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began))
        {
                        #if IPHONE || ANDROID
            if (EventSystem.current.IsPointerOverGameObject(Input.GetTouch(0).fingerId))
            {
                        #else
            //Debug.Log(Stage.inst.touchTarget.gameObject.name);
            if (EventSystem.current.IsPointerOverGameObject())
            {
                        #endif
                ;
            }
            else
            {
                if (nowMode == MouseState.NONE)
                {
                    Vector3      pos  = m_camera.ScreenToWorldPoint(Input.mousePosition);
                    RaycastHit[] hits = null;
                    hits = Physics.RaycastAll(pos, Vector3.forward, Mathf.Infinity);
                    int minDis = int.MaxValue;
                    if (hits.Length > 0)                          //检测是否射线接触物体
                    {
                        mouseDownPos = Input.mousePosition;
                        nowClickGO   = hits [0].collider.gameObject;
                        nowMode      = MouseState.CLICK;
                    }
                    clickedObjs = new GameObject[hits.Length];
                    for (int i = 0; i < hits.Length; i++)
                    {
                        clickedObjs [i] = hits [i].collider.gameObject;
                    }
                }
            }
        }

        if (Input.GetMouseButton(0))
        {
            Vector3 mouseMove = (Input.mousePosition - mouseDownPos);
            mouseMove.z = 0;
            if (nowMode == MouseState.NONE)
            {
            }
            if (nowMode == MouseState.CLICK)
            {
                if (mouseMove.magnitude < dragYuzhi)
                {
                    //仍是点击
                }
                else
                {
                    //超过阀值 变为拖动
                    nowMode     = MouseState.DRAG;
                    lastDragPos = Input.mousePosition;
                    if (nowClickGO == null || nowClickGO.GetComponentInParent <ClickableEventlistener2D> () == null)
                    {
                        //无法回调
                    }
                    else
                    {
                        nowClickGO.GetComponentInParent <ClickableSprite> ().startDrag(lastDragPos);
                    }
                }
            }
            else if (nowMode == MouseState.DRAG)
            {
                Vector3 nowPos = Input.mousePosition;
                Vector3 delta  = nowPos - lastDragPos;
                if (nowClickGO == null || nowClickGO.GetComponentInParent <ClickableSprite> () == null)
                {
                    //无法回调
                }
                else
                {
                    nowClickGO.GetComponentInParent <ClickableSprite> ().onDrag(delta);
                }
                lastDragPos = nowPos;
            }
        }



        if (Input.GetMouseButtonUp(0))
        {
            if (nowMode == MouseState.NONE)
            {
            }
            else if (nowMode == MouseState.CLICK)
            {
                if (nowClickGO == null || nowClickGO.GetComponentInParent <ClickableSprite> () == null)
                {
                    //无法回调
                }
                else
                {
                    ClickableEventlistener2D cp = nowClickGO.GetComponentInParent <ClickableEventlistener2D>();

                    if (cp == null || !cp.hasClickEvent())
                    {
                        int idx = 1;
                        if (clickedObjs != null)
                        {
                            while (idx < clickedObjs.Length)
                            {
                                nowClickGO = clickedObjs [idx];
                                cp         = nowClickGO.GetComponentInParent <ClickableEventlistener2D>();
                                if (cp != null && cp.hasClickEvent())
                                {
                                    cp.onClick(Input.mousePosition);
                                    break;
                                }
                                idx++;
                            }
                        }
                    }
                    else
                    {
                        cp.onClick(Input.mousePosition);
                    }
                }
            }
            else if (nowMode == MouseState.DRAG)
            {
                if (nowClickGO == null || nowClickGO.GetComponentInParent <ClickableSprite> () == null)
                {
                    //无法回调
                }
                else
                {
                    nowClickGO.GetComponentInParent <ClickableSprite> ().endDrag(Input.mousePosition);
                }
            }
            nowMode = MouseState.NONE;
        }
    }
}