public static Point ConvertMobile2Unity(Point pt)
        {
            if (pt == null)
            {
                return(null);
            }
            float num   = 0f;
            float num2  = 0f;
            Point point = new Point(CoordinateType.UnityScreen);

            if (CoordinateTool.GetCurrenScreenScale(ref num, ref num2))
            {
                point.X = pt.X / num;
                point.Y = (float)Screen.height - pt.Y / num2;
                u.c(string.Concat(new object[]
                {
                    "point(",
                    pt.X,
                    ",",
                    pt.Y,
                    ") => (",
                    point.X,
                    ",",
                    point.Y,
                    ")"
                }));
            }
            return(point);
        }
Example #2
0
        protected void InjectTouchEvent()
        {
            try
            {
                List <TouchEvent> events = TouchEventHandler.INSTANCE.GetTouchEvents();

                if (events != null)
                {
                    float offsetx = 0, offsety = 0, scalex = 0, scaley = 0;
                    if (CoordinateTool.GetCurrenScreenParam(ref offsetx, ref offsety, ref scalex, ref scaley))
                    {
                        for (int i = 0; i < events.Count; ++i)
                        {
                            events[i].x -= offsetx;
                            events[i].y -= offsety;
                        }
                    }
                    for (int i = 0; i < events.Count; ++i)
                    {
                        TouchEvent touchEvent = events[i];
                        AndroidRobot.INSTANCE.InjectMotionEvent(touchEvent.x, touchEvent.y, touchEvent.type);
                    }
                }
            }
            catch (System.Exception ex)
            {
                Logger.w(ex.Message + "\n" + ex.StackTrace);
            }
        }
        public static Point ConvertUnity2Mobile(Vector2 pt)
        {
            Point point = new Point();
            float num   = (float)Screen.height - pt.y;
            float num2  = 0f;
            float num3  = 0f;

            if (CoordinateTool.GetCurrenScreenScale(ref num2, ref num3))
            {
                point.X = pt.x * num2;
                point.Y = num * num3;
                u.d(string.Concat(new object[]
                {
                    "ConvertUnity2Mobile from point =(",
                    pt.x,
                    ", ",
                    pt.y,
                    ") ==> ( ",
                    point.X,
                    ", ",
                    point.Y,
                    ")"
                }));
            }
            else
            {
                point.X = pt.x;
                point.Y = num;
            }
            return(point);
        }
 public static Rectangle WorldBoundsToScreenRect(Camera cm, Bounds bounds)
 {
     Vector3[] array = new Vector3[]
     {
         new Vector3(bounds.center.x + bounds.extents.x, bounds.center.y + bounds.extents.y, bounds.center.z + bounds.extents.z),
         new Vector3(bounds.center.x + bounds.extents.x, bounds.center.y + bounds.extents.y, bounds.center.z - bounds.extents.z),
         new Vector3(bounds.center.x + bounds.extents.x, bounds.center.y - bounds.extents.y, bounds.center.z + bounds.extents.z),
         new Vector3(bounds.center.x - bounds.extents.x, bounds.center.y + bounds.extents.y, bounds.center.z + bounds.extents.z),
         new Vector3(bounds.center.x + bounds.extents.x, bounds.center.y - bounds.extents.y, bounds.center.z - bounds.extents.z),
         new Vector3(bounds.center.x - bounds.extents.x, bounds.center.y + bounds.extents.y, bounds.center.z - bounds.extents.z),
         new Vector3(bounds.center.x - bounds.extents.x, bounds.center.y - bounds.extents.y, bounds.center.z + bounds.extents.z),
         new Vector3(bounds.center.x - bounds.extents.x, bounds.center.y - bounds.extents.y, bounds.center.z - bounds.extents.z)
     };
     return(CoordinateTool.a(new Point[]
     {
         CoordinateTool.a(cm, array[0]),
         CoordinateTool.a(cm, array[1]),
         CoordinateTool.a(cm, array[2]),
         CoordinateTool.a(cm, array[3]),
         CoordinateTool.a(cm, array[4]),
         CoordinateTool.a(cm, array[5]),
         CoordinateTool.a(cm, array[6]),
         CoordinateTool.a(cm, array[7])
     }));
 }
Example #5
0
        public static GameObject FindElementByUnityPos(Point pt)
        {
            Vector2    vector     = CoordinateTool.ConvertPoint2Vector(pt);
            bool       invalid    = false;
            GameObject gameObject = Raycast(vector, ref invalid);

            return(gameObject);
        }
Example #6
0
        public Rectangle GetBound(GameObject obj)
        {
            if (obj == null)
            {
                return(null);
            }
            Rectangle rc = null;
            Camera    cm = FindBestCamera(obj);

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

            rc = GetBoundByCollider(cm, obj);//主要是boxcolider2d物体的时候

            if (rc == null)
            {
                rc = NGUITools.GetNGUIBound(cm, obj);
            }

            if (rc == 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);
        }
Example #7
0
        public Rectangle GetBoundByCollider(Camera cm, GameObject obj, Boolean ignoreZ)
        {
            Collider collider = obj.GetComponent <Collider>();

            if (obj.activeInHierarchy && collider != null && collider.enabled)
            {
                return(CoordinateTool.WorldBoundsToScreenRect(cm, collider.bounds, ignoreZ));
            }

            return(null);
        }
Example #8
0
        public Rectangle GetBoundByRender(Camera cm, GameObject obj)
        {
            Renderer render = obj.GetComponent <Renderer>();

            if (render != null && render.isVisible)
            {
                return(CoordinateTool.WorldBoundsToScreenRect(cm, render.bounds));
            }

            return(null);
        }
Example #9
0
        public Rectangle GetBoundByMesh(Camera cm, GameObject obj)
        {
            MeshFilter meshFilter = obj.GetComponent <MeshFilter>();

            if (meshFilter != null && meshFilter.sharedMesh != null)
            {
                //Logger.d("By MeshFilter");
                Mesh mesh = meshFilter.sharedMesh;
                return(CoordinateTool.WorldBoundsToScreenRect(cm, mesh.bounds));
            }

            return(null);
        }
Example #10
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 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.GetCurrenScreenScale(ref scalex, ref scaley))
            {
                rc.x = rc.x * scalex;
                rc.y = rc.y * scaley;

                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);
        }
        public static Rectangle ConvertUnity2Mobile(Rectangle rect)
        {
            Rectangle rectangle = new Rectangle();
            float     num       = (float)Screen.height - rect.y;
            float     num2      = 0f;
            float     num3      = 0f;

            if (CoordinateTool.GetCurrenScreenScale(ref num2, ref num3))
            {
                rectangle.x      = rect.x * num2;
                rectangle.y      = num * num3;
                rectangle.width  = rect.width * num2;
                rectangle.height = rect.height * num3;
            }
            else
            {
                rectangle = rect;
            }
            return(rectangle);
        }
Example #12
0
        public static GameObject GetUIElementbyRaycast(Point point)
        {
            Point pt = new Point(0, 0);

            if (point.X < 1 && point.X > 0 && point.Y < 1 && point.Y > 0)
            {
                pt = new Point(Screen.width * point.X, Screen.height * (1 - point.Y));
            }
            else if (RuntimePlatform.Android == Application.platform)
            {
                pt = CoordinateTool.ConvertMobile2Unity(point);
            }
            else
            {
                pt = new Point(point.X, Screen.height - point.Y);
            }
            Vector2    vector     = CoordinateTool.ConvertPoint2Vector(pt);
            bool       invalid    = false;
            GameObject gameObject = Raycast(vector, ref invalid);

            return(gameObject);
        }
Example #13
0
        public static GameObject GetUIElementbyRaycast(Point point)
        {
            if (EventSystem.current == null)
            {
                Logger.d("No Event System");
                return(null);
            }
            Point pt = new Point(0, 0);

            if (point.X > 0 && point.X < 1 && point.Y > 0 && point.Y < 1)   //归一化的坐标
            {
                pt = new Point(Screen.width * point.X, Screen.height * (1 - point.Y));
            }
            else if (RuntimePlatform.Android == Application.platform)
            {//android绝对坐标
                pt = CoordinateTool.ConvertMobile2Unity(point);
            }
            else
            {//一般绝对坐标
                pt = new Point(point.X, (1 - point.Y));
            }

            return(GetUIElementbyRaycastByUnity(pt));
        }
Example #14
0
        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 = NGUITools.FindElementByUnityPos(new Point(t.position.x, t.position.y));
                        }
                        catch (System.Exception ex)
                        {
                            Logger.v(ex.StackTrace);
                        }
                        if (selectedObj != null && NGUITools.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);
            }
        }
Example #15
0
        public static List <UINode> GetInteractiveGameobject(List <string> buttonTypes)
        {
            //string[] typeNames = new string[] { UIButtonName, UIToggleName, UIEventListenerName };
            //string[] typeNamesFirst = new string[] { UIButtonNameFirstPass, UIToggleNameFirstPass, UIEventListenerNameFirstPass };

            List <string> typeNames      = new List <string>();
            List <string> typeNamesFirst = new List <string>();

            typeNames.Add(UIButtonName);
            typeNamesFirst.Add(UIButtonNameFirstPass);

            typeNames.Add(UIToggleName);
            typeNamesFirst.Add(UIToggleNameFirstPass);

            typeNames.Add(UIEventListenerName);
            typeNamesFirst.Add(UIEventListenerNameFirstPass);

            if (buttonTypes != null)
            {
                for (int i = 0; i < buttonTypes.Count; ++i)
                {
                    typeNames.Add(buttonTypes[i]);
                    typeNamesFirst.Add(buttonTypes[i]);
                }
            }

            HashSet <int> buffers     = new HashSet <int>();
            List <UINode> gameObjects = new List <UINode>();
            Rectangle     rect        = new Rectangle();

            for (int i = 0; i < typeNames.Count; ++i)
            {
                Type buttonType = Type.GetType(typeNames[i]);
                if (buttonType == null)
                {
                    buttonType = Type.GetType(typeNamesFirst[i]);
                }

                if (buttonType != null)
                {
                    Component[] objs = GameObject.FindObjectsOfType(buttonType) as Component[];
                    foreach (Component c in objs)
                    {
                        GameObject obj = c.gameObject;

                        Logger.v("Find GameObject " + obj.name);

                        if (!buffers.Contains(obj.GetInstanceID()) && OverUIElement(obj, ref rect))
                        {
                            Rectangle rc = new Rectangle();
                            if (RuntimePlatform.Android == Application.platform)//安卓默认返回绝对坐标
                            {
                                rc = CoordinateTool.ConvertUnity2Mobile(rect);
                            }
                            else if (RuntimePlatform.IPhonePlayer == Application.platform)//ios 默认返回归一化的坐标
                            {
                                rc.x      = rect.x / Screen.width;
                                rc.y      = 1 - rect.y / Screen.height;
                                rc.width  = rect.width / Screen.width;
                                rc.height = rect.height / Screen.height;
                            }


                            UINode node = new UINode(obj, rc);
                            buffers.Add(obj.GetInstanceID());
                            gameObjects.Add(node);
                        }
                    }
                }
            }

            return(gameObjects);
        }
        /// <summary>
        /// 获取所有潜在的可交互的节点
        ///
        ///
        /// </summary>
        /// <returns></returns>
        public static List <UINode> GetInteractiveGameobject(List <String> buttonTypes)
        {
            Logger.d("GetInteractiveGameobject");
            HashSet <int> buffer           = new HashSet <int>();
            List <UINode> vaildGameObjects = new List <UINode>();


            Canvas[]             canvas    = GameObject.FindObjectsOfType <Canvas>();
            List <BaseRaycaster> raycaster = GetVaildRaycast();

            for (int i = 0; i < canvas.Length; ++i)
            {
                GameObject gameobject = canvas[i].gameObject;

                //步骤1,找出当前界面下包含IEventSystemHandler的所有节点
                Component[] handlers = GetIEventSystemHandler(gameobject);
                for (int j = 0; j < handlers.Length; ++j)
                {
                    Component c = handlers[j] as Component;
                    if (c != null && !buffer.Contains(c.gameObject.GetInstanceID()))
                    {
                        buffer.Add(c.gameObject.GetInstanceID());
                        Logger.d("UGUI may Clickable gameobject => " + c.gameObject);

                        //步骤2,过滤掉所有被遮挡的节点。以点击中间点的方式
                        Rectangle rect = new Rectangle();
                        if (CanInputEventOverObject(raycaster, c.gameObject, ref rect))
                        {
                            if (RuntimePlatform.Android == Application.platform)
                            { //android 默认返回绝对坐标
                                rect = CoordinateTool.ConvertUnity2Mobile(rect);
                            }

                            else if (RuntimePlatform.IPhonePlayer == Application.platform)
                            {//ios 默认返回归一化的坐标
                                rect.x      /= Screen.width;
                                rect.y       = 1 - rect.y / Screen.height;
                                rect.width  /= Screen.width;
                                rect.height /= Screen.height;
                            }
                            UINode node = new UINode(c.gameObject, rect);
                            Logger.d("Gameobject can click " + c.gameObject);
                            vaildGameObjects.Add(node);
                        }
                    }
                }
            }

            List <Component> comps = new List <Component>();

            for (int i = 0; buttonTypes != null && i < buttonTypes.Count; ++i)
            {
                Type buttonType = Type.GetType(buttonTypes[i]);

                if (buttonType != null)
                {
                    Component[] handlers = GameObject.FindObjectsOfType(buttonType) as Component[];
                    for (int j = 0; j < handlers.Length; ++j)
                    {
                        Component c = handlers[j] as Component;
                        if (c != null && !buffer.Contains(c.gameObject.GetInstanceID()))
                        {
                            buffer.Add(c.gameObject.GetInstanceID());
                            Logger.d("UGUI may Clickable gameobject => " + c.gameObject);

                            //步骤2,过滤掉所有被遮挡的节点。以点击中间点的方式
                            Rectangle rect = new Rectangle();
                            if (CanInputEventOverObject(raycaster, c.gameObject, ref rect))
                            {
                                if (RuntimePlatform.Android == Application.platform)//android默认返回绝对坐标
                                {
                                    rect = CoordinateTool.ConvertUnity2Mobile(rect);
                                }

                                else if (RuntimePlatform.IPhonePlayer == Application.platform)
                                {//ios 默认返回绝对坐标
                                    rect.x      /= Screen.width;
                                    rect.y       = 1 - rect.y / Screen.height;
                                    rect.width  /= Screen.width;
                                    rect.height /= Screen.height;
                                }
                                UINode node = new UINode(c.gameObject, rect);
                                Logger.d("Gameobject can click " + c.gameObject);
                                vaildGameObjects.Add(node);
                            }
                        }
                    }
                }
            }
            return(vaildGameObjects);
        }