Explore Enterprise Education Gitee Premium Gitee AI AI teammates
Fetch the repository succeeded.
Donate
Please sign in before you donate.
Scan WeChat QR to Pay
Cancel
Complete
Prompt
Switch to Alipay.
OK
Cancel
1 Star 0 Fork 2

kevin101/Unity新手引导框架

Create your Gitee Account
Explore and code with more than 14 million developers,Free private repositories !:)
Sign up
Already have an account? Sign in
文件
master
Branches (1)
master
This repository doesn't specify license. Please pay attention to the specific project description and its upstream code dependency when using it.
The license selected for the repository is subject to the license used by the main branch of the repository.
master
Branches (1)
master
Clone or Download
Clone/Download
Prompt
To download the code, please copy the following command and execute it in the terminal
To ensure that your submitted code identity is correctly recognized by Gitee, please execute the following command.
When using the SSH protocol for the first time to clone or push code, follow the prompts below to complete the SSH configuration.
1 Generate RSA keys.
2 Obtain the content of the RSA public key and configure it in SSH Public Keys
To use SVN on Gitee, please visit the usage guide
When using the HTTPS protocol, the command line will prompt for account and password verification as follows. For security reasons, Gitee recommends configure and use personal access tokens instead of login passwords for cloning, pushing, and other operations.
Username for 'https://gitee.com': userName
Password for 'https://userName@gitee.com': # Private Token
master
Branches (1)
master
UnityGuideFrameWork
/
Assets
/
Script
/
RectGuidanceController.cs
UnityGuideFrameWork
/
Assets
/
Script
/
RectGuidanceController.cs
RectGuidanceController.cs 4.23 KB
Copy Edit Raw Blame History
黄焱 authored 2022年03月09日 17:56 +08:00 . 库文件调整
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
/// <summary>
/// 矩形引导组件
/// </summary>
public class RectGuidanceController : MonoBehaviour
{
//获取画布
private Canvas canvas;
/// <summary>
/// 高亮显示的目标
/// </summary>
public RectTransform target;
/// <summary>
/// 区域范围缓存
/// </summary>
private Vector3[] _corners = new Vector3[4];
/// <summary>
/// 镂空区域中心
/// </summary>
private Vector4 _center;
/// <summary>
/// 最终的偏移值X
/// </summary>
private float _targetOffsetX = 0f;
/// <summary>
/// 最终的偏移值Y
/// </summary>
private float _targetOffsetY = 0f;
/// <summary>
/// 遮罩材质
/// </summary>
private Material _material;
/// <summary>
/// 当前的偏移值X
/// </summary>
private float _currentOffsetX = 0f;
/// <summary>
/// 当前的偏移值Y
/// </summary>
private float _currentOffsetY = 0f;
/// <summary>
/// 动画收缩时间
/// </summary>
private float _shrinkTime = 0.5f;
/// <summary>
/// 世界坐标到画布坐标的转换
/// </summary>
/// <param name="canvas">画布</param>
/// <param name="world">世界坐标</param>
/// <returns>转换后在画布的坐标</returns>
private Vector2 WorldToCanvasPos(Canvas canvas, Vector3 world)
{
Vector2 position;
RectTransformUtility.ScreenPointToLocalPointInRectangle(canvas.transform as RectTransform, world,
canvas.GetComponent<Camera>(), out position);
return position;
}
public void SetTarget(RectTransform target)
{
this.target = target;
RefreshMask();
}
private void RefreshMask()
{
canvas = GameObject.FindGameObjectWithTag("Canvas").GetComponent<Canvas>();
//获取高亮区域四个顶点的世界坐标
target.GetWorldCorners(_corners);
//计算高亮显示区域咋画布中的范围
_targetOffsetX = Vector2.Distance(WorldToCanvasPos(canvas, _corners[0]), WorldToCanvasPos(canvas, _corners[3])) / 2f;
_targetOffsetY = Vector2.Distance(WorldToCanvasPos(canvas, _corners[0]), WorldToCanvasPos(canvas, _corners[1])) / 2f;
//计算高亮显示区域的中心
float x = _corners[0].x + ((_corners[3].x - _corners[0].x) / 2f);
float y = _corners[0].y + ((_corners[1].y - _corners[0].y) / 2f);
Vector3 centerWorld = new Vector3(x, y, 0);
Vector2 center = WorldToCanvasPos(canvas, centerWorld);
//设置遮罩材料中中心变量
Vector4 centerMat = new Vector4(center.x, center.y, 0, 0);
_material = GetComponent<Image>().material;
_material.SetVector("_Center", centerMat);
//计算当前偏移的初始值
RectTransform canvasRectTransform = (canvas.transform as RectTransform);
if (canvasRectTransform != null)
{
//获取画布区域的四个顶点
canvasRectTransform.GetWorldCorners(_corners);
//求偏移初始值
for (int i = 0; i < _corners.Length; i++)
{
if (i % 2 == 0)
_currentOffsetX = Mathf.Max(Vector3.Distance(WorldToCanvasPos(canvas, _corners[i]), center), _currentOffsetX);
else
_currentOffsetY = Mathf.Max(Vector3.Distance(WorldToCanvasPos(canvas, _corners[i]), center), _currentOffsetY);
}
}
//设置遮罩材质中当前偏移的变量
_material.SetFloat("_SliderX", _currentOffsetX);
_material.SetFloat("_SliderY", _currentOffsetY);
}
private float _shrinkVelocityX = 0f;
private float _shrinkVelocityY = 0f;
private void Update()
{
//从当前偏移值到目标偏移值差值显示收缩动画
float valueX = Mathf.SmoothDamp(_currentOffsetX, _targetOffsetX, ref _shrinkVelocityX, _shrinkTime);
float valueY = Mathf.SmoothDamp(_currentOffsetY, _targetOffsetY, ref _shrinkVelocityY, _shrinkTime);
if (!Mathf.Approximately(valueX, _currentOffsetX))
{
_currentOffsetX = valueX;
_material.SetFloat("_SliderX", _currentOffsetX);
}
if (!Mathf.Approximately(valueY, _currentOffsetY))
{
_currentOffsetY = valueY;
_material.SetFloat("_SliderY", _currentOffsetY);
}
}
}
Loading...
Report
Report success
We will send you the feedback within 2 working days through the letter!
Please fill in the reason for the report carefully. Provide as detailed a description as possible.
Please select a report type
Cancel
Send
误判申诉

此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。

如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。

取消
提交

About

基于UGUI的,Unity新手引导框架,严重参考知乎大佬文章,并进行魔改,可独立运行的测试工程 面向配置,易操作好上手 参考地址如下: https://zhuanlan.zhihu.com/p/151407153 https://zhuanlan.zhihu.com/p/151408255
Cancel

Releases

No release

Contributors

All

Activities

can not load any more
Edit
About
Homepage
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C#
1
https://gitee.com/bugman/UnityGuideFrameWork.git
git@gitee.com:bugman/UnityGuideFrameWork.git
bugman
UnityGuideFrameWork
Unity新手引导框架
master
Going to Help Center

Search

Comment
Repository Report
Back to the top
Login prompt
This operation requires login to the code cloud account. Please log in before operating.
Go to login
No account. Register

AltStyle によって変換されたページ (->オリジナル) /