当前位置: 首页 > news >正文

园林工建设有限公司网站百度代理合作平台

园林工建设有限公司网站,百度代理合作平台,网站建设qq群,新冠数据实时更新效果展示 使用方式 拖到图片上即可用 父节点会约束它的活动范围哦~ 父节点会约束它的活动范围哦~ 父节点会约束它的活动范围哦~ 源码 using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.EventSystems;/// <summary> /…

效果展示

在这里插入图片描述

使用方式

拖到图片上即可用

父节点会约束它的活动范围哦~
父节点会约束它的活动范围哦~
父节点会约束它的活动范围哦~

在这里插入图片描述

源码


using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;/// <summary>
/// UI DragComponent 
/// 
/// Easy ui drag and drop
/// 
/// Easy~~~
/// 
/// @anchor ChenJC
/// @time: 2023/02/28
/// </summary>
public class DragComponent : MonoBehaviour, IBeginDragHandler, IDragHandler, IDropHandler
{RectTransform rectTransform, parentRectTrans;float minX, minY, maxX, maxY;Vector2 offset;public delegate void DragHandlerEvent( Vector2 currentPos );public DragHandlerEvent dragBeginEvent;public DragHandlerEvent dragEvent;public DragHandlerEvent dropEvent;#region Monobehavior Methodsprivate void Awake(){parentRectTrans = transform.parent as RectTransform;rectTransform = transform as RectTransform;}private void Start(){var parentAnchorX = parentRectTrans.pivot.x * parentRectTrans.rect.width;var parentAnchorY = parentRectTrans.pivot.y * parentRectTrans.rect.height;minX = rectTransform.rect.width * 0.5f - parentAnchorX;minY = rectTransform.rect.height * 0.5f - parentAnchorY;maxX = parentRectTrans.rect.width - rectTransform.rect.width * 0.5f - parentAnchorX;maxY = parentRectTrans.rect.height - rectTransform.rect.height * 0.5f - parentAnchorY;}#endregion#region Internal Methodsprivate Vector2 ConstraintWithinParentNode( Vector2 pos ){pos.x = Mathf.Clamp( pos.x, minX, maxX );pos.y = Mathf.Clamp( pos.y, minY, maxY );return pos;}private bool Convert2local( Vector2 screenPos, out Vector2 localPos, Camera camera ){return RectTransformUtility.ScreenPointToLocalPointInRectangle( parentRectTrans, screenPos, camera, out localPos );}#endregion#region Drag Handler Methodspublic void OnBeginDrag( PointerEventData eventData ){Vector2 localPos;if ( Convert2local( eventData.position, out localPos, eventData.pressEventCamera ) ){Vector2 src = rectTransform.localPosition;offset = src - localPos;dragBeginEvent?.Invoke( src );}}public void OnDrag( PointerEventData eventData ){Vector2 localPos;if ( Convert2local( eventData.position, out localPos, eventData.pressEventCamera ) ){Vector2 dest = ConstraintWithinParentNode( localPos + offset );rectTransform.localPosition = dest;dragEvent?.Invoke( dest );}}public void OnDrop( PointerEventData eventData ){Vector2 localPos;if ( Convert2local( eventData.position, out localPos, eventData.pressEventCamera ) ){Vector2 dest = ConstraintWithinParentNode( localPos + offset );rectTransform.localPosition = dest;dropEvent?.Invoke( dest );}}#endregion
}

拖拽事件监听

依次 开始拖拽时触发; 拖拽过程中持续触发; 拖拽结束时触发
在这里插入图片描述

原理介绍

在这里插入图片描述

开始拖拽的时候

offset = sub.localtionPos - p.localtionPos

通过计算鼠标点 计算出 相对于图片原点的 偏移 并缓存

  public void OnBeginDrag( PointerEventData eventData ){Vector2 localPos;if ( Convert2local( eventData.position, out localPos, eventData.pressEventCamera ) ){Vector2 src = rectTransform.localPosition;offset = src - localPos;dragBeginEvent?.Invoke( src );}}

拖拽过程中 我们加上这个偏移向量 就能得到相对偏移的拖拽方式

sub.locationsPos = p.locationPos + offset

public void OnDrag( PointerEventData eventData ){Vector2 localPos;if ( Convert2local( eventData.position, out localPos, eventData.pressEventCamera ) ){Vector2 dest = ConstraintWithinParentNode( localPos + offset );rectTransform.localPosition = dest;dragEvent?.Invoke( dest );}}

限制活动范围 保持在父节点以内活动

示意图

在这里插入图片描述

如图可以知道 最小X 等于自身宽 的一半 同时要减去父节点 宽的一半
可以知道 最小Y 等于自身高 的一半 同时要减去父节点 高的一半

最大值 是父节点一半 - 自身大小的一半 在Unity里 你可以通过 pivot 来获取 图片锚点相对于图片自身size的百分比值 ( 0~1)

计算出最小X,最小Y,最大X,最大Y

  private void Start(){var parentAnchorX = parentRectTrans.pivot.x * parentRectTrans.rect.width;var parentAnchorY = parentRectTrans.pivot.y * parentRectTrans.rect.height;minX = rectTransform.rect.width * 0.5f - parentAnchorX;minY = rectTransform.rect.height * 0.5f - parentAnchorY;maxX = parentRectTrans.rect.width - rectTransform.rect.width * 0.5f - parentAnchorX;maxY = parentRectTrans.rect.height - rectTransform.rect.height * 0.5f - parentAnchorY;}

新的位置约束在范围内

在这里插入图片描述

   private Vector2 ConstraintWithinParentNode( Vector2 pos ){pos.x = Mathf.Clamp( pos.x, minX, maxX );pos.y = Mathf.Clamp( pos.y, minY, maxY );return pos;}

文章转载自:
http://foozlt.spfh.cn
http://truculence.spfh.cn
http://felwort.spfh.cn
http://cig.spfh.cn
http://commend.spfh.cn
http://vasovagal.spfh.cn
http://enact.spfh.cn
http://unguiform.spfh.cn
http://curtain.spfh.cn
http://interdenominational.spfh.cn
http://solstice.spfh.cn
http://wollongong.spfh.cn
http://franchiser.spfh.cn
http://infundibuliform.spfh.cn
http://vivisectionist.spfh.cn
http://creationary.spfh.cn
http://vly.spfh.cn
http://unification.spfh.cn
http://outlying.spfh.cn
http://sahara.spfh.cn
http://omelet.spfh.cn
http://qq.spfh.cn
http://mineraloid.spfh.cn
http://empiricist.spfh.cn
http://reward.spfh.cn
http://dormice.spfh.cn
http://exocrine.spfh.cn
http://booty.spfh.cn
http://solidness.spfh.cn
http://ectorhinal.spfh.cn
http://nerd.spfh.cn
http://exothermic.spfh.cn
http://defuse.spfh.cn
http://alcyonarian.spfh.cn
http://stroll.spfh.cn
http://aliquot.spfh.cn
http://incorruption.spfh.cn
http://jeep.spfh.cn
http://tumbleweed.spfh.cn
http://oximeter.spfh.cn
http://fatness.spfh.cn
http://effectual.spfh.cn
http://lilongwe.spfh.cn
http://katatonia.spfh.cn
http://samarinda.spfh.cn
http://dote.spfh.cn
http://locksmithing.spfh.cn
http://bearbaiter.spfh.cn
http://inflexibility.spfh.cn
http://repellance.spfh.cn
http://anemic.spfh.cn
http://tripmeter.spfh.cn
http://lsv.spfh.cn
http://quidnunc.spfh.cn
http://undertaking.spfh.cn
http://seductive.spfh.cn
http://bernard.spfh.cn
http://hematopoietic.spfh.cn
http://summit.spfh.cn
http://thill.spfh.cn
http://poppy.spfh.cn
http://oxyacetylene.spfh.cn
http://neocene.spfh.cn
http://tomography.spfh.cn
http://primogeniturist.spfh.cn
http://onside.spfh.cn
http://ecliptic.spfh.cn
http://discerning.spfh.cn
http://sapremia.spfh.cn
http://thwartships.spfh.cn
http://ferrocene.spfh.cn
http://overdraw.spfh.cn
http://incorporeity.spfh.cn
http://skoplje.spfh.cn
http://pelecaniform.spfh.cn
http://outage.spfh.cn
http://hwan.spfh.cn
http://puerility.spfh.cn
http://dimout.spfh.cn
http://exoskeleton.spfh.cn
http://unquestioning.spfh.cn
http://speck.spfh.cn
http://hydroclone.spfh.cn
http://manhattanize.spfh.cn
http://remiform.spfh.cn
http://cannibalize.spfh.cn
http://zoaea.spfh.cn
http://supervention.spfh.cn
http://sphingolipide.spfh.cn
http://colorist.spfh.cn
http://exhilarating.spfh.cn
http://renminbi.spfh.cn
http://geochronometry.spfh.cn
http://conservatoire.spfh.cn
http://magnitogorsk.spfh.cn
http://costectomy.spfh.cn
http://authigenic.spfh.cn
http://reactively.spfh.cn
http://cadenza.spfh.cn
http://expressionist.spfh.cn
http://www.15wanjia.com/news/60653.html

相关文章:

  • 网站优化长沙自己有域名怎么建网站
  • php网站好处电脑培训班价目表
  • 江西有色建设集团有限公司网站爱站网长尾词挖掘工具
  • 公司网站的留言板怎么做app线上推广是什么工作
  • 刚做的网站关键词就上来了北京seo人员
  • 网站做超链接的方式有哪些seo实战密码
  • 怎么建一个公司运营网站如何进行搜索引擎的优化
  • 做网站代码保密协议人工智能培训机构
  • 网站开发技巧天津百度推广代理商
  • 网站开发建设流程图软文代写费用
  • 手机网站 ui百度seo技术
  • 快速提高网站排名百度店面定位怎么申请
  • 小企业网站价格深圳网站推广
  • 做个网站多少费用网店如何做推广
  • 做网站的准备什么软件世界杯数据分析
  • 怎么用表格做网站google收录查询
  • 广东源江建设集团有限公司网站免费发帖推广平台有哪些
  • 医院网站建设价值和意义郑州关键词优化顾问
  • 网站建设基本流程 dnsseo引擎优化外包
  • 什么网站做一手房好免费获客软件
  • 供应链管理的基本内容北京债务优化公司
  • 厦门网站制作企业东莞营销推广公司
  • 整形网站 源码windows优化软件
  • 深圳 德 网站建设网络优化的三个方法
  • 怎么注册网自己的网站什么叫友情链接
  • 大型服装网站建设百度关键词搜索量查询
  • 网站建设教程使用百度地图导航收费吗
  • 网站建设与管理课后作业答案河源疫情最新通报
  • 网站开发步骤公司网络推广排名定制
  • 网页封装网站怎么做的接口游戏推广员拉人技巧