window 克隆复制当前窗口
window 克隆复制当前窗口
转载于:https://www.cnblogs.com/yue31313/p/9887789.html
众所周知,PrintScreen键可以复制整个屏幕的内容到剪切板,但是如果我们只需要复制屏幕上某个窗口的内容呢?
一种方式是使用Windows自带的截图工具(win + R在“运行中输入snippingtool”)或者微信的截图工具(快捷键Alt + A)
另一种更方便的方式是使用Alt + PrintScreen键。Alt + PrintScreen键会复制当前活动窗口的内容到剪切板,例如只复制资源管理器窗口的内容:
当然,如果需要复制的是窗口中的局部内容,还是只能使用截图工具
Alt+V垂直复制当前窗口
Alt+Shift+V将当前窗口复制到另一边的分割窗口显示,Alt+Shift+M移动当前活动窗口到另一边
使用最后一次滑动的方向来判定目标方向,如果朝某边偏移后往回移动,会返回到原来的位置
然后点击运行,就可以看见上面的效果了
//跳转到指定节点,如果不存在会返回false
bool isSucceed = GetComponent<Slide>().ToAppointNode(int 指定节点)
//返回当前所在的节点
int x = GetComponent<Slide>().GetCurNodeCount()
//返回当前总节点数(也就是Content的子对象数,不会包含隐藏对象)
int x = GetComponent<Slide>().GetMaxNodeCount()
//注册回调,切换不同页面时触发
GetComponent<Slide>().SubscribeCallback(Action<int> callback);
//解除回调
GetComponent<Slide>().UnSubscribeCallback();
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
public class Slide : MonoBehaviour, IEndDragHandler, IDragHandler, IBeginDragHandler
{
//按下时判断目标方向,松开时根据当前偏移方向与目标方向,计算目标节点,然后移动至该节点
//脚本需要放置在Scroll View组件中,MovementType设为Unrestricted,关闭Inertia
//引用框
[Header("Content组件")]
public GameObject content;
//节点数量
int nodeCount = 0;
//节点宽度/切换长度
float nodeWidth = 0;
//切换速度
[Header("切换速度")]
public float speed = 12;
//当前所在节点
int curNode = 0;
//目标节点
int targetNode = 0;
//当前触摸坐标
float curTouchPos = 0;
//上一帧触摸坐标
float lastTouchPos = 0;
//当前触摸状态
bool isTouch = false;
//切换目标方向
bool switchTargetIsLeft = false;
//当前偏移方向
bool offsetIsLeft = false;
//是否到达目标
bool isArrive = true;
//切换瞬间的回调
public Action<int> switchCallback = null;
//按下
public void OnBeginDrag(PointerEventData eventData)
{
isTouch = true;
lastTouchPos = eventData.position.x;
curTouchPos = eventData.position.x;
}
//拖拽
public void OnDrag(PointerEventData eventData)
{
curTouchPos = eventData.position.x;
//设置滑动方向
if (lastTouchPos - curTouchPos < 0)
switchTargetIsLeft = true;
else if (lastTouchPos - curTouchPos > 0)
switchTargetIsLeft = false;
lastTouchPos = eventData.position.x;
}
//松开
public void OnEndDrag(PointerEventData eventData)
{
isTouch = false;
//设为未到达
isArrive = false;
//更新节点信息
UpdateNodeData();
//判断当前节点偏移方向
if (content.GetComponent<RectTransform>().anchoredPosition.x * -1 < curNode * nodeWidth)//如果当前UI坐标 < 当前节点坐标
offsetIsLeft = true;
else
offsetIsLeft = false;
//如果目标方向和偏移方向一致
if ((switchTargetIsLeft == offsetIsLeft) &&
((switchTargetIsLeft && (curNode - 1 >= 0)) || //且 朝左时没有超过最小值
(!switchTargetIsLeft && curNode + 1 <= nodeCount - 1)))//且 朝右时没有超过最大值
{
//设置目标节点
targetNode = switchTargetIsLeft ? curNode - 1 : curNode + 1;
//更新当前节点
curNode = targetNode;
//触发回调
switchCallback?.Invoke(targetNode);
}
//否则回到原始位置
else
{
//回到当前节点
targetNode = curNode;
}
}
//获取当前节点数
public int GetCurNodeCount()
{
//更新节点信息
UpdateNodeData();
return curNode;
}
//获取总节点数
public int GetMaxNodeCount()
{
//更新节点信息
UpdateNodeData();
return nodeCount;
}
//前往指定节点
public bool ToAppointNode(int appointNode)
{
//更新节点信息
UpdateNodeData();
if (appointNode < 0 || appointNode >= nodeCount)
{
//Debug.LogError("指定节点超出范围:");
return false;
}
isArrive = false;
//触发回调
if (appointNode != curNode)
switchCallback?.Invoke(appointNode);
//设置目标节点
targetNode = appointNode;
//更新当前节点
curNode = targetNode;
return true;
}
//注册回调
public void SubscribeCallback(Action<int> newCallback)
{
switchCallback = newCallback;
}
//清空回调
public void UnsubscribeCallback()
{
switchCallback = null;
}
//更新节点总数和每个节点的宽度
void UpdateNodeData()
{
//获取节点总数(所有显示的子类)
int x = 0;
for (int i = 0; i < content.transform.childCount; i++)
{
if (content.transform.GetChild(i).gameObject.activeSelf)
x++;
}
nodeCount = x;
//获取每个节点的宽度
nodeWidth = content.GetComponent<RectTransform>().rect.width / nodeCount;
}
void Update()
{
//如果 不在点击状态 且 没有到达目标节点
if (!isTouch && !isArrive)
{
//前往目标节点
Vector2 targetPos = new Vector2(
targetNode * nodeWidth * -1,
content.GetComponent<RectTransform>().anchoredPosition.y);
content.GetComponent<RectTransform>().anchoredPosition = Vector2.Lerp(content.GetComponent<RectTransform>().anchoredPosition, targetPos, Time.deltaTime * speed);
//如果很靠近
if (Mathf.Abs(content.GetComponent<RectTransform>().anchoredPosition.x - targetPos.x) <= 0.05)
isArrive = true;
}
}
}
萌新需要多多支持,如果对你有帮助,就请点个赞吧~