[Unity] Unity中制作放大镜 效果

查看:3450 |回复:39 | 2015-9-22 00:12:02

您需要 登录 才可以下载或查看,没有账号?注册

x
原帖地址见底部
其实和 小地图都差不多了。  还是要借助 另一个相机


目的: 这篇文章的主要目的是  要给你一个想法  如何做放大境效果 。


在unity中可以简单的实现放大镜效果啊 . 那么现在就来一步一步实现这个:


1:创建一个摄像机对象,设置 projection 类型为 perspective 或者 orthographic.


2:设置相机的 orthographicSize 或者 fieldOfView   (依赖于相机的 projection 类型 ).        


3:设置其 pixelrect  . 例如如果您想要在你鼠标位置显示放大境  和其大小是 100 x 100 , 然后设置pixelrect 为 :


magnifyCamera.pixelRect = new Rect (Input.mousePosition.x – 100f / 2.0f, Input.mousePosition.y – 100f / 2.0f, 100f, 100f);
设置相机的位置。  例如 如果你想在 你的鼠标位置显示放大镜效果  ,那么设置相机的位置为 mousePosition世界点。


你能看到最终的效果图: 20150816155843798.png



下面的 C# 脚本将创建一个  MagnifyGlass,并将它移动到 mousePosition位置 。MagnifyGlass 脚本:   添加到一个空的游戏对象。
  1. using UnityEngine;
  2. using System.Collections;

  3. public class MagnifyGlass : MonoBehaviour
  4. {
  5. private Camera magnifyCamera;
  6. private GameObject magnifyBorders;
  7. private LineRenderer LeftBorder, RightBorder, TopBorder, BottomBorder; // Reference for lines of magnify glass borders
  8. private float MGOX,MG0Y; // Magnify Glass Origin X and Y position
  9. private float MGWidth = Screen.width/5f,MGHeight = Screen.width/5f; // Magnify glass width and height
  10. private Vector3 mousePos;

  11. void Start ()
  12. {
  13. createMagnifyGlass ();
  14. }
  15. void Update ()
  16. {
  17. // Following lines set the camera's pixelRect and camera position at mouse position
  18. magnifyCamera.pixelRect = new Rect (Input.mousePosition.x - MGWidth / 2.0f, Input.mousePosition.y - MGHeight / 2.0f, MGWidth, MGHeight);
  19. mousePos = getWorldPosition (Input.mousePosition);
  20. magnifyCamera.transform.position = mousePos;
  21. mousePos.z = 0;
  22. magnifyBorders.transform.position = mousePos;
  23. }

  24. // Following method creates MagnifyGlass
  25. private void createMagnifyGlass()
  26. {
  27. GameObject camera = new GameObject("MagnifyCamera");
  28. MGOX = Screen.width / 2f - MGWidth/2f;
  29. MG0Y = Screen.height / 2f - MGHeight/2f;
  30. magnifyCamera = camera.AddComponent<Camera>();
  31. magnifyCamera.pixelRect = new Rect(MGOX, MG0Y, MGWidth, MGHeight);
  32. magnifyCamera.transform.position = new Vector3(0,0,0);
  33. if(Camera.main.isOrthoGraphic)
  34. {
  35. magnifyCamera.orthographic = true;
  36. magnifyCamera.orthographicSize = Camera.main.orthographicSize / 5.0f;//+ 1.0f;
  37. createBordersForMagniyGlass ();
  38. }
  39. else
  40. {
  41. magnifyCamera.orthographic = false;
  42. magnifyCamera.fieldOfView = Camera.main.fieldOfView / 10.0f;//3.0f;
  43. }

  44. }

  45. // Following method sets border of MagnifyGlass
  46. private void createBordersForMagniyGlass()
  47. {
  48. magnifyBorders = new GameObject ();
  49. LeftBorder = getLine ();
  50. LeftBorder.SetVertexCount(2);
  51. LeftBorder.SetPosition(0,new Vector3(getWorldPosition(new Vector3(MGOX,MG0Y,0)).x,getWorldPosition(new Vector3(MGOX,MG0Y,0)).y-0.1f,-1));
  52. LeftBorder.SetPosition(1,new Vector3(getWorldPosition(new Vector3(MGOX,MG0Y+MGHeight,0)).x,getWorldPosition(new Vector3(MGOX,MG0Y+MGHeight,0)).y+0.1f,-1));
  53. LeftBorder.transform.parent = magnifyBorders.transform;
  54. TopBorder = getLine ();
  55. TopBorder.SetVertexCount(2);
  56. TopBorder.SetPosition(0,new Vector3(getWorldPosition(new Vector3(MGOX,MG0Y+MGHeight,0)).x,getWorldPosition(new Vector3(MGOX,MG0Y+MGHeight,0)).y,-1));
  57. TopBorder.SetPosition(1,new Vector3(getWorldPosition(new Vector3(MGOX+MGWidth,MG0Y+MGHeight,0)).x,getWorldPosition(new Vector3(MGOX+MGWidth,MG0Y+MGHeight,0)).y,-1));
  58. TopBorder.transform.parent = magnifyBorders.transform;
  59. RightBorder = getLine ();
  60. RightBorder.SetVertexCount(2);
  61. 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));
  62. RightBorder.SetPosition(1,new Vector3(getWorldPosition(new Vector3(MGOX+MGWidth,MG0Y,0)).x,getWorldPosition(new Vector3(MGOX+MGWidth,MG0Y,0)).y-0.1f,-1));
  63. RightBorder.transform.parent = magnifyBorders.transform;
  64. BottomBorder = getLine ();
  65. BottomBorder.SetVertexCount(2);
  66. BottomBorder.SetPosition(0,new Vector3(getWorldPosition(new Vector3(MGOX+MGWidth,MG0Y,0)).x,getWorldPosition(new Vector3(MGOX+MGWidth,MG0Y,0)).y,-1));
  67. BottomBorder.SetPosition(1,new Vector3(getWorldPosition(new Vector3(MGOX,MG0Y,0)).x,getWorldPosition(new Vector3(MGOX,MG0Y,0)).y,-1));
  68. BottomBorder.transform.parent = magnifyBorders.transform;
  69. }

  70. // Following method creates new line for MagnifyGlass's border
  71. private LineRenderer getLine()
  72. {
  73. LineRenderer line = new GameObject("Line").AddComponent<LineRenderer>();
  74. line.material = new Material(Shader.Find("Diffuse"));
  75. line.SetVertexCount(2);
  76. line.SetWidth(0.2f,0.2f);
  77. line.SetColors(Color.black, Color.black);
  78. line.useWorldSpace = false;
  79. return line;
  80. }
  81. private void setLine(LineRenderer line)
  82. {
  83. line.material = new Material(Shader.Find("Diffuse"));
  84. line.SetVertexCount(2);
  85. line.SetWidth(0.2f,0.2f);
  86. line.SetColors(Color.black, Color.black);
  87. line.useWorldSpace = false;
  88. }

  89. // Following method calculates world's point from screen point as per camera's projection type
  90. public Vector3 getWorldPosition(Vector3 screenPos)
  91. {
  92. Vector3 worldPos;
  93. if(Camera.main.isOrthoGraphic)
  94. {
  95. worldPos = Camera.main.ScreenToWorldPoint (screenPos);
  96. worldPos.z = Camera.main.transform.position.z;
  97. }
  98. else
  99. {
  100. worldPos = Camera.main.ScreenToWorldPoint (new Vector3 (screenPos.x, screenPos.y, Camera.main.transform.position.z));
  101. worldPos.x *= -1;
  102. worldPos.y *= -1;
  103. }
  104. return worldPos;
  105. }
  106. }
点击此处复制文本




原帖地址:http://www.manew.com/thread-42935-1-1.html



评分

参与人数 1元素币 +10 活跃度 +5 贡献值 +1 展开 理由
狼之独步 + 10 + 5 + 1 挺实用的,你那个微元素的底图是咋加上的?

查看全部评分

2015-9-22 00:12:02  
 赞 赞 1

使用道具 登录

39个回答,把该问题分享到群,邀请大神一起回答。
2#
666666666666666666666666666
回复 收起回复
2015-9-22 00:44:59   回复
 赞 赞 1

使用道具 登录

3#
想要成大触,天天上元素!
回复 收起回复
2015-9-22 07:05:37   回复
 赞 赞 1

使用道具 登录

4#
资源发布哪家强?元素首发称大王!
回复 收起回复
2015-9-22 07:51:35   回复
 赞 赞 1

使用道具 登录

5#
元素帖子强,满满正能量!
回复 收起回复
2015-9-22 08:24:22   回复
 赞 赞 1

使用道具 登录

6#
带你赚币带你飞,元素里面有正妹!
回复 收起回复
2015-9-22 08:31:59   回复
 赞 赞 1

使用道具 登录

8#
元素那么大,我想来看看!
回复 收起回复
2015-9-22 08:54:21   回复
 赞 赞 1

使用道具 登录

9#
为了元素币,拼了!
回复 收起回复
2015-9-22 09:06:27   回复
 赞 赞 1

使用道具 登录

10#
给力!元素有你更精彩
回复 收起回复
2015-9-22 09:47:47   回复
 赞 赞 1

使用道具 登录

11#
元素那么大,我想来看看!
回复 收起回复
2015-9-22 10:25:32   回复
 赞 赞 1

使用道具 登录

12#
想要成大触,天天上元素!
回复 收起回复
2015-9-22 10:53:34   回复
 赞 赞 1

使用道具 登录

13#
带你赚币带你飞,元素里面有正妹!
回复 收起回复
2015-9-22 10:55:18   回复
 赞 赞 1

使用道具 登录

14#
元素那么大,我想来看看!
回复 收起回复
2015-9-22 12:30:16   回复
 赞 赞 1

使用道具 登录

15#
楼主给你32个赞
回复 收起回复
2015-9-22 13:34:35   回复
 赞 赞 1

使用道具 登录

16#
666
回复 收起回复
2015-9-22 21:49:48   回复
 赞 赞 1

使用道具 登录

17#
给力!元素有你更精彩
回复 收起回复
2015-9-23 09:15:21   回复
 赞 赞 1

使用道具 登录

18#
想要成大触,天天上元素!
回复 收起回复
2015-9-29 14:35:40   回复
 赞 赞 1

使用道具 登录

19#
元素帖子强,满满正能量!
回复 收起回复
2015-9-29 15:00:31   回复
 赞 赞 1

使用道具 登录

20#
元素帖子强,满满正能量!
回复 收起回复
2015-10-13 16:59:57   回复
 赞 赞 1

使用道具 登录

CG 游戏行业专业问题

Unity3D技术手机游戏引擎手游引擎
123下一页
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表