Make a magnifying glass effect in unity

Source: Internet
Author: User

Sun Guangdong 2015.8.16


Actually, it's almost like a small map. Or do you want to use another camera?


Purpose: The main purpose of this article is to give you an idea of how to do the magnifying effect.


In unity, you can simply implement the magnifying glass effect. So let's do this one step at a time:

Create a Camera object, set the projection type to perspective or orthographic.

Set the camera's orthographicsize or fieldofview (dependent on the camera's projection type).

Set its pixelrect. For example, if you want to display the magnification in your mouse position and its size is 100 x, then set Pixelrect to:

Magnifycamera.pixelrect = new Rect (input.mouseposition.x–100f/2.0f, input.mouseposition.y–100f/2.0f, 100f, 100f);

Sets the location of the camera. For example, if you want to display a magnifying glass effect in your mouse position, then set the camera position to MousePosition World point.


You can see the final:



The following C # script creates a magnifyglass and moves it to the mouseposition location.

Magnifyglass script: Add to an empty game object.

Using unityengine;using System.Collections; public class Magnifyglass:monobehaviour{private Camera magnifycamera;private gameobject magnifyborders;private Linerenderer Leftborder, Rightborder, Topborder, Bottomborder; Reference for lines of magnify glass bordersprivate float mgox,mg0y; Magnify Glass Origin X and Y positionprivate float mgwidth = Screen.width/5f,mgheight = screen.width/5f; Magnify glass width and heightprivate Vector3 mousepos; void Start () {Createmagnifyglass ();} void Update () {///following lines set the camera ' s pixelrect and camera position at mouse Positionmagnifycamera.pixelrect  = new Rect (input.mouseposition.x-mgwidth/2.0f, input.mouseposition.y-mgheight/2.0f, Mgwidth, MGHeight); MousePos = Getworldposition (input.mouseposition); magnifyCamera.transform.position = mousepos;mousepos.z = 0; MagnifyBorders.transform.position = Mousepos;} Following method creates magnifyglassprivate void Createmagnifyglass () {gameobject camera = new Gameobject ("MagnIfycamera "); Mgox = screen.width/2f-mgwidth/2f; mg0y = Screen.height/2f-mgheight/2f;magnifycamera = camera. Addcomponent<camera> (); magnifycamera.pixelrect = new Rect (Mgox, mg0y, Mgwidth, mgheight); MagnifyCamera.transform.position = new Vector3 (0,0,0); if (Camera.main.isOrthoGraphic) {magnifycamera.orthographic = True;magnifycamera.orthographicsize = camera.main.orthographicsize/5.0f;//+ 1.0f;createbordersformagniyglass ();} else{magnifycamera.orthographic = False;magnifycamera.fieldofview = camera.main.fieldofview/10.0f;//3.0f;}}  Following method sets border of Magnifyglassprivate void Createbordersformagniyglass () {magnifyborders = new Gameobject (); Leftborder = GetLine (); Leftborder.setvertexcount (2); Leftborder.setposition (0,new Vector3 (getworldposition (New Vector3 (mgox,mg0y,0)). X,getworldposition (New Vector3 ( mgox,mg0y,0)) (y-0.1f,-1)); Leftborder.setposition (1,new Vector3 (getworldposition (New Vector3 (mgox,mg0y+mgheight,0)). X,getworldposition (New Vector3 (Mgox,mg0y+mgheight,0)) (y+0.1f,-1)); LeftBorder.transform.parent = Magnifyborders.transform; Topborder = GetLine (); Topborder.setvertexcount (2); Topborder.setposition (0,new Vector3 (getworldposition (New Vector3 (mgox,mg0y+mgheight,0)). X,getworldposition (New Vector3 (mgox,mg0y+mgheight,0)). y,-1)); Topborder.setposition (1,new Vector3 (getworldposition (New Vector3 (mgox+mgwidth,mg0y+mgheight,0)). x, Getworldposition (New Vector3 (mgox+mgwidth,mg0y+mgheight,0)). y,-1)); TopBorder.transform.parent = Magnifyborders.transform; Rightborder = GetLine (); Rightborder.setvertexcount (2); Rightborder.setposition (0,new Vector3 (getworldposition (New Vector3 (mgox+mgwidth,mg0y+mgwidth,0)). x, Getworldposition (New Vector3 (mgox+mgwidth,mg0y+mgwidth,0)). y+0.1f,-1)); Rightborder.setposition (1,new Vector3 (getworldposition (New Vector3 (mgox+mgwidth,mg0y,0)). X,getworldposition (New Vector3 (mgox+mgwidth,mg0y,0)). y-0.1f,-1)); RightBorder.transform.parent = Magnifyborders.transform; Bottomborder = GetLine (); Bottomborder.setvertexcount (2); Bottomborder.setPosition (0,new Vector3 (getworldposition (New Vector3 (mgox+mgwidth,mg0y,0)). X,getworldposition (New Vector3 (mgox+ mgwidth,mg0y,0)) (y,-1)); Bottomborder.setposition (1,new Vector3 (getworldposition (New Vector3 (mgox,mg0y,0)). X,getworldposition (New Vector3 ( mgox,mg0y,0)) (y,-1)); BottomBorder.transform.parent = Magnifyborders.transform;} Following method creates new line for Magnifyglass ' s borderprivate linerenderer getLine () {Linerenderer line = new Gameo Bject ("line"). Addcomponent<linerenderer> (); line.material = new material (Shader.find ("diffuse")); Setvertexcount (2); line. SetWidth (0.2f,0.2f); Setcolors (Color.Black, color.black); line.useworldspace = False;return line;} private void Setline (Linerenderer line) {line.material = new material (Shader.find ("diffuse")); Setvertexcount (2); line. SetWidth (0.2f,0.2f); Setcolors (Color.Black, color.black); line.useworldspace = false;} Following method calculates world's point from screen point as per camera ' s projection typepublic Vector3Getworldposition (Vector3 screenpos) {Vector3 worldpos;if (Camera.main.isOrthoGraphic) {Worldpos = Camera.main.ScreenToWorldPoint (screenpos); worldpos.z = Camera.main.transform.position.z;} Else{worldpos = Camera.main.ScreenToWorldPoint (New Vector3 (Screenpos.x, Screenpos.y, CAMERA.MAIN.TRANSFORM.POSITION.Z)); worldpos.x *= -1;worldpos.y *=-1;} return worldpos;}}

Believe you can do better through this!



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Make a magnifying glass effect in unity

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.