Example #1
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);
        }
Example #2
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);
            }
        }
        /// <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);
        }