Exemplo n.º 1
0
        /// <summary>Fires an event into the scene of the camera.</summary>
        internal void FireIntoScene(UIEvent e)
        {
            if (e == null)
            {
                return;
            }

            // Get coords relative to the element. These are non-standard convenience properties:
            float localX = e.localX;
            float localY = e.localY;

            // Flip Y because Unity is upside down relative to input coords:
            localY = ScreenInfo.ScreenY - 1 - localY;

            // Fire off an input event at that point:
            RaycastHit worldUIHit;

            if (Physics.Raycast(Camera.ScreenPointToRay(new Vector2(localX, localY)), out worldUIHit))
            {
                // Did it hit a worldUI?
                WorldUI worldUI = WorldUI.Find(worldUIHit);

                if (worldUI == null)
                {
                    // Nope!
                    return;
                }

                // Resolve the hit into a -0.5 to +0.5 point:
                float x;
                float y;
                worldUI.ResolvePoint(worldUIHit, out x, out y);

                // Map it from a relative point to a 'real' one:
                Vector2 point = worldUI.RelativePoint(x, y);

                // Apply to x/y:
                x = point.x;
                y = point.y;

                // Pull an element from that worldUI:
                Element el = worldUI.document.elementFromPointOnScreen(x, y);

                if (el != null)
                {
                    // Fire the event for that element:
                    el.dispatchEvent(e);
                }
            }
        }
Exemplo n.º 2
0
		/// <summary>Finds an EventTarget from the given screen location.
		/// This fires a ray into the scene if the point isn't on the main UI and caches the hit in the given pointer.
		/// X and Y are updated with the document-relative point if it's on a WorldUI/ on the Unity UI.</summary>
		public static EventTarget TargetFromPoint(ref float x,ref float y,InputPointer pointer){
			
			Element result=null;
			
			if(UI.document!=null){
				
				// Test the main UI:
				result=UI.document.elementFromPointOnScreen(x,y);
				
				if(result!=null){
					
					// Is it transparent or not html/body?
					if(AcceptsInput(result)){
						// Great, we're done!
						
						if(pointer!=null){
							pointer.LatestHitSuccess=false;
						}
						
						return result;
					}
					
				}
				
			}
			
			// Handle a Unity UI if needed.
			Vector2 point;
			
			#if HANDLE_UNITY_UI
			
			if(UnityUICaster!=null){
				
				// Try hitting the Unity UI first.
				point=new Vector2(x,ScreenInfo.ScreenY-1-y);
				
				HtmlDocument panelDoc=MapToUIPanel(ref point);
				
				if(panelDoc!=null){
					
					// Test for an element there:
					result=panelDoc.elementFromPointOnScreen(point.x,point.y);
					
					if(result!=null){
						
						// Is it transparent?
						
						// We're still checking a UI so we might still pass "through" it.
						if(AcceptsInput(result)){
							
							// Got a hit! Update x/y:
							x=point.x;
							y=point.y;
							
							if(pointer!=null){
								pointer.LatestHitSuccess=false;
							}
							
							return result;
							
						}
						
					}
					
				}
			
			}
			
			#endif
			
			// Go after WorldUI's next.
			RaycastHit worldUIHit;
			Camera cam=CameraFor3DInput;
			
			if(cam==null){
				// No camera!
				
				if(pointer!=null){
					pointer.LatestHitSuccess=false;
				}
				
				return null;
			}
			
			Vector2 screenPoint = new Vector2(x,ScreenInfo.ScreenY-1-y);
			
			if(pointer == null){
				
				// Cast into the scene now:
				Ray ray=cam.ScreenPointToRay(screenPoint);
				if(!Physics.Raycast(ray,out worldUIHit)){
					
					// Nothing hit.
					return null;
					
				}
				
			}else if(!pointer.Raycast(out worldUIHit,cam,screenPoint)){
				
				// Nothing hit.
				pointer.LatestHitSuccess=false;
				return null;
				
			}
			
			// Cache it:
			if(pointer!=null){
				pointer.LatestHit=worldUIHit;
				pointer.LatestHitSuccess=true;
			}
			
			// Did it hit a worldUI?
			WorldUI worldUI=WorldUI.Find(worldUIHit);
			
			if(worldUI==null){
				
				// Nope! Get the eventTarget for the gameObject:
				#if Enable3DInput
				return worldUIHit.transform.gameObject.getEventTarget();
				#else
				return null;
				#endif
			}
			
			// Resolve the hit into a -0.5 to +0.5 point:
			// (we're ok to reuse x/y here):
			worldUI.ResolvePoint(worldUIHit,out x,out y);
			
			// Map it from a relative point to a 'real' one:
			point=worldUI.RelativePoint(x,y);
			
			// Apply to x/y:
			x=point.x;
			y=point.y;
			
			// Pull an element from that worldUI:
			return worldUI.document.elementFromPointOnScreen(x,y);
			
		}
        /// <summary>Resolves the given target to a document.</summary>
        /// <returns>The targeted document. Null if there is no document at all and the target is essentially outside of Unity.</returns>
        public HtmlDocument ResolveTarget(string target)
        {
            // Grab the document the element is in:
            HtmlDocument document = htmlDocument;

            if (target == null)
            {
                // No target - does the document define a default one?
                // Note that this is set with the "base" html tag.

                if (document.location != null)
                {
                    target = document.baseTarget;
                }
            }

            // Null target is the same as _self.
            if (string.IsNullOrEmpty(target))
            {
                target = "_self";
            }

            // Grab the window:
            Window window = document.window;

            switch (target)
            {
            case "_blank":

                // Open the given url outside Unity.
                return(null);

            case "_top":
                // Open the given URL at the top window.

                return(window.top.document);

            case "_parent":

                // Open it there:
                return(window.parent.document);

            case "_self":

                // Open it in this document:
                return(document);

            case "_main":

                // Open into the main UI:
                return(UI.document);

            default:
                // Anything else and it's the name of an iframe (preferred) or a WorldUI.

                // Get the element by name:
                HtmlElement iframeElement = document.getElementByAttribute("name", target) as HtmlElement;

                if (iframeElement == null)
                {
                    // WorldUI with this name?
                    WorldUI ui = WorldUI.Find(target);

                    if (ui == null)
                    {
                        // Not found - same as self:
                        return(document);
                    }

                    // Load into the WorldUI:
                    return(ui.document);
                }

                // Great, we have an iframe - grab the content document:
                return(iframeElement.contentDocument);
            }
        }