Unity中做放大镜 效果

转载:http://www.manew.com/thread-42935-1-1.html?_dsign=4597a598

其实和 小地图都差不多了。  还是要借助 另一个相机

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


unity中可以简单的实现放大镜效果啊 . 那么现在就来一步一步实现这个:
创建一个摄像机对象,设置 projection 类型为 perspective 或者 orthographic.
设置相机的 orthographicSize 或者 fieldOfView   (依赖于相机的 projection 类型 ).        
设置其 pixelrect  . 例如如果您想要在你鼠标位置显示放大境  和其大小是 100 x 100 , 然后设置pixelrect 为 :
magnifyCamera.pixelRect = new Rect (Input.mousePosition.x – 100f / 2.0f, Input.mousePosition.y – 100f / 2.0f, 100f, 100f);
设置相机的位置。  例如 如果你想在 你的鼠标位置显示放大镜效果  ,那么设置相机的位置为 mousePosition世界点。

你能看到最终的效果图:



下面的 C# 脚本将创建一个  MagnifyGlass,并将它移动到 mousePosition位置 。
MagnifyGlass 脚本:   添加到一个空的游戏对象。

[C#]  纯文本查看  复制代码
?
 
001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
046
047
048
049
050
051
052
053
054
055
056
057
058
059
060
061
062
063
064
065
066
067
068
069
070
071
072
073
074
075
076
077
078
079
080
081
082
083
084
085
086
087
088
089
090
091
092
093
094
095
096
097
098
099
100
101
102
103
104
105
106
107
108
109
110
111
112
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 borders
private float MGOX,MG0Y; // Magnify Glass Origin X and Y position
private float MGWidth = Screen.width/5f,MGHeight = Screen.width/5f; // Magnify glass width and height
private Vector3 mousePos;
 
void Start ()
{
createMagnifyGlass ();
}
void Update ()
{
// Following lines set the camera's pixelRect and camera position at mouse position
magnifyCamera.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 MagnifyGlass
private 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 MagnifyGlass
private 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 border
private LineRenderer getLine()
{
LineRenderer line = new GameObject( "Line" ).AddComponent<LineRenderer>();
line.material = new Material(Shader.Find( "Diffuse" ));
line.SetVertexCount(2);
line.SetWidth(0.2f,0.2f);
line.SetColors(Color.black, Color.black);
line.useWorldSpace = false ;
return line;
}
private void setLine(LineRenderer line)
{
line.material = new Material(Shader.Find( "Diffuse" ));
line.SetVertexCount(2);
line.SetWidth(0.2f,0.2f);
line.SetColors(Color.black, Color.black);
line.useWorldSpace = false ;
}
 
// Following method calculates world's point from screen point as per camera's projection type
public Vector3 getWorldPosition(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;
}
}


相信你可以通过这个 做的更好!

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是使用 Unity 实现放大镜效果的代码示例: ```csharp using UnityEngine; public class MagnifyingGlass : MonoBehaviour { public float magnification = 2.0f; // 放大镜的放大倍数 public float radius = 50.0f; // 放大镜的半径 private Texture2D texture; // 存储放大后的图像的纹理 private Rect rect; // 放大镜的矩形区域 void Start() { // 创建一个空的纹理来存储放大后的图像 texture = new Texture2D((int)radius * 2, (int)radius * 2, TextureFormat.RGB24, false); // 设置放大镜的矩形区域 rect = new Rect(0, 0, radius * 2, radius * 2); } void OnGUI() { // 获取鼠标当前位置 Vector2 mousePos = Event.current.mousePosition; // 在放大镜心点周围创建一个裁剪区域 Rect clipRect = new Rect(mousePos.x - radius, Screen.height - mousePos.y - radius, radius * 2, radius * 2); // 用裁剪区域的内容填充纹理 texture.ReadPixels(clipRect, 0, 0); // 缩放纹理以进行放大 texture.Apply(); GUI.DrawTexture(rect, texture, ScaleMode.ScaleToFit); // 绘制放大镜的边框 GUI.Box(new Rect(mousePos.x - radius, Screen.height - mousePos.y - radius, radius * 2, radius * 2), ""); } } ``` 该代码使用 `OnGUI()` 方法来绘制放大镜效果。在 `Start()` 方法,它创建了一个空的纹理来存储放大后的图像,并设置了放大镜的矩形区域。在 `OnGUI()` 方法,它获取鼠标当前位置,创建一个裁剪区域,然后用裁剪区域的内容填充纹理并缩放纹理以进行放大。最后,它绘制放大镜的边框。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值