//UGUI事件传播的原理首先获取第一个Raycast的节点,然后一直往父节点遍历是否有注册的可执行内容
        private static bool CanInputEventOverObject(List <BaseRaycaster> raycasters, GameObject obj, ref Rectangle rect)
        {
            //步骤1:获取到当前Gameobject的中心点
            //TODO:需要确认无法找到center点的情况。如果这个找不到原有的方法,其实也是找不到点的

            RectTransform rectTran = obj.GetComponent <RectTransform>();

            Vector3[] core = new Vector3[4];
            rectTran.GetWorldCorners(core);

            Vector2[] vectors = UGUITools.GetWorldCorner(obj);
            if (vectors == null)
            {
                return(false);
            }
            Vector2 bottomLeft  = vectors[0];
            Vector2 topLeft     = vectors[1];
            Vector2 topRight    = vectors[2];
            Vector2 bottomRight = vectors[3];

            rect.x      = topLeft.x;
            rect.y      = topLeft.y;
            rect.width  = topRight.x - topLeft.x;
            rect.height = topLeft.y - bottomLeft.y;

            bottomLeft.x += (bottomRight.x - bottomLeft.x) / 2;
            bottomLeft.y += (topRight.y - bottomRight.y) / 2;
            //步骤2,获取到所有Raycast到的点
            PointerEventData data = new PointerEventData(EventSystem.current);

            data.position = bottomLeft;


            List <RaycastResult> results = new List <RaycastResult>();

            Logger.d("Find objects on point===>(" + data.position.x + "," + data.position.y + ")");
            EventSystem.current.UpdateModules();

            EventSystem.current.RaycastAll(data, results);

            if (results.Count == 0)
            {
                return(false);
            }
            //foreach (RaycastResult res in results)
            //{
            //    Logger.d("gameobject name=" + res.gameObject + " index=" + res.index + " module=" + res.module + " depth=" + res.depth + " isvaild=" + res.isValid.ToString());
            //}
            GameObject firstGameObject = results[0].gameObject;

            Logger.d("Find obj " + firstGameObject + " orginal game obj " + obj);
            if (firstGameObject == obj || firstGameObject.transform.IsChildOf(obj.transform))
            {
                return(true);
            }

            return(false);
        }
Esempio n. 2
0
        public Rectangle GetBound(GameObject obj)
        {
            Rectangle rc = null;

            if (obj == null)
            {
                return(null);
            }
            if (UGUITools.isUGUIElement(obj))
            {
                rc = UGUITools.GetUGUIBound(obj);
            }
            if (rc == null)
            {
                Camera cm = FindBestCamera(obj);
                if (cm == null)
                {
                    return(null);
                }
                rc = GetGUIBound(cm, obj);
            }

            if (rc == null)
            {
                return(null);
            }

            Logger.v("GetBound gameobject =" + obj.name + "  rc.x=" + rc.x + ", rc.y=" + rc.y + ", wight = " + rc.width + ", height=" + rc.height);

            //坐标缩放
            float offsetx = 0, offsety = 0, scalex = 0, scaley = 0;

            if (RuntimePlatform.IPhonePlayer == Application.platform)//iOS 返回归一化的值
            {
                rc.x      = rc.x / Screen.width;
                rc.y      = rc.y / Screen.height;
                rc.width  = rc.width / Screen.width;
                rc.height = rc.height / Screen.height;
            }
            else if (CoordinateTool.GetCurrenScreenParam(ref offsetx, ref offsety, ref scalex, ref scaley))
            {
                rc.x = rc.x * scalex + offsetx;
                rc.y = rc.y * scaley + offsety;

                rc.width  = rc.width * scalex;
                rc.height = rc.height * scaley;
            }
            Logger.v("GetBound() after scale : rc.x=" + rc.x + ", rc.y=" + rc.y + ", wight = " + rc.width + ", height=" + rc.height);
            return(rc);
        }
Esempio n. 3
0
        public List <GameObject> FindGameObjectsByPoint(Point pt)
        {
            List <GameObject> result = new List <GameObject>();

            try
            {
                GameObject obj = UGUITools.GetUIElementbyRaycast(pt);
                if (obj != null)
                {
                    result.Add(obj);
                }
            }
            catch (System.Exception ex)
            {
                Logger.d(ex.Message + "\n" + ex.StackTrace);
            }

            return(result);
        }
        protected void NotifyTouchElement()
        {
            try
            {
                Cmd     cmd       = Cmd.TOUCH_NOTIFY;
                Command command   = new Command(cmd, recordSocket);
                Touch[] touchs    = Input.touches;
                int     fingerNum = touchs.Length;
                if (fingerNum == 0)
                {
                    return;
                }
                bool        finded      = false;
                TouchNotify touchNotify = new TouchNotify();
                string      scene       = Application.loadedLevelName;
                touchNotify.scene = scene;
                for (int i = 0; i < fingerNum && i < 5; ++i)
                {
                    Touch t = touchs[i];
                    Logger.d("Touch delta time = {0},x = {1},y={2} ,fingerId = {3},phase = {4}", t.deltaTime * Time.timeScale, t.position.x, t.position.y, t.fingerId, t.phase);
                    //只考虑,一个点的情况,Begin的时候
                    if (t.phase == TouchPhase.Began && !finded)
                    {
                        GameObject selectedObj = null;
                        try
                        {
                            selectedObj = UGUITools.GetUIElementbyRaycastByUnity(new Point(t.position.x, t.position.y));
                        }
                        catch (System.Exception ex)
                        {
                            Logger.v(ex.StackTrace);
                        }
                        if (selectedObj != null && UGUITools.IsInteraction(selectedObj))
                        {
                            finded = true;
                            string name = GameObjectTool.GenerateNamePath(selectedObj);
                            Logger.d("Touch UI = " + name);
                            touchNotify.name = name;
                        }
                    }
                    if (t.phase == TouchPhase.Canceled || t.phase == TouchPhase.Stationary || t.phase == TouchPhase.Moved)
                    {
                        continue;
                    }
                    TouchData td = new TouchData();
                    td.deltatime = DateTime.Now.Ticks / 10000 - startTime;
                    td.fingerId  = (short)t.fingerId;
                    Point point = CoordinateTool.ConvertUnity2Mobile(t.position);
                    td.x         = point.X;
                    td.y         = point.Y;
                    td.relativeX = t.position.x / Screen.width;
                    td.relativeY = (Screen.height - t.position.y) / Screen.height;
                    switch (t.phase)
                    {
                    case TouchPhase.Began:
                        td.phase = (byte)TouchType.TOUCH_DOWN;
                        break;

                    //case TouchPhase.Moved:
                    //    td.bPhase = (byte)ATTouchType.AT_TOUCH_MOVE;
                    //    break;
                    case TouchPhase.Ended:
                        td.phase = (byte)TouchType.TOUCH_UP;
                        break;
                    }
                    touchNotify.touches.Add(td);
                }
                if (touchNotify.touches.Count > 0)
                {
                    command.sendObj = touchNotify;
                    CommandDispatcher.SendCommand(command);
                }
            }
            catch (System.Exception ex)
            {
                Logger.w(ex.Message + "\n" + ex.StackTrace);
                Cmd     cmd     = Cmd.TOUCH_NOTIFY;
                Command command = new Command(cmd, recordSocket);
                command.sendObj = ex.Message + "\n" + ex.StackTrace;
                command.status  = ResponseStatus.UN_KNOW_ERROR;
                CommandDispatcher.SendCommand(command);
            }
        }