---------------------滑动到矩形---------------------
1、具体效果(后面补上)
2、三个接口IBeginDragHandler, IDragHandler, IEndDragHandler对应方法
public void OnBeginDrag(PointerEventData eventData){
//dragOffset = (Vector2)transform.position - eventData.position;
//transform.position = eventData.position + dragOffset;
}
public void OnDrag(PointerEventData eventData){
//transform.position = eventData.position + dragOffset;//是否需要跟随鼠标移动
//eventData会返回一堆东西,具体百度PointerEventData
//if (eventData.pointerCurrentRaycast.gameObject != null && eventData.pointerCurrentRaycast.gameObject.name == "Image"){
// image.color = new Color(1,1,1);
// isInCancel = true;
//}
//else
//{
// image.color = new Color(130 / 255, 130 / 255, 130 / 255, 1);
// isInCancel = false;
//}
}
public void OnEndDrag(PointerEventData eventData){
//这个在OnPointerUp方法之后
//在OnPointerUp方法内判断isInCancel 或者再写一遍OnDrag内的内容。
}
---------------------滑动到四分之一圆外---------------------
public void OnDrag(PointerEventData eventData){
//float dis = Vector2.Distance(eventData.position + dragOffset, RightButton);
//if (dis > imageWidth){
//image.color = new Color(1, 1, 1, 1);
//isInCancel = true;
//}
//else{
//image.color = new Color(130 / 255, 130 / 255, 130 / 255, 1);
//isInCancel = false;
//}
}
imageWidth 这个值需要根据分辨率来确定具体的大小 目前Canvas Scaler 设置是
//this.RealWidth = 1920f;
//this.RealHeight = this.RealWidth / (float)Screen.width * (float)Screen.height;
//Debug.LogFormat("real: width:{0} height:{1}", this.RealWidth, this.RealHeight);
这个算出来canvas当前缩放后的大小,具体的imageWidth还没算出来,这个方案就废弃了。。。 ---------------------滑动到不规则区域---------------------
public Image _image;
private Sprite _sprite;
_sprite = _image.sprite;
public float alpahThreshold = 0.5f;
Vector2 vector;
public void OnDrag(PointerEventData eventData){
RectTransformUtility.ScreenPointToLocalPointInRectangle(_image.rectTransform, eventData.position, null, out vector);
if (_sprite.texture.GetPixel((int)vector.x, (int)vector.y).a > alpahThreshold){
_image.color = new Color(1,1,1,1);
}
else{
_image.color = new Color(130 / 255, 130 / 255, 130 / 255, 1);
}
}
ScreenPointToLocalPointInRectangle方法 需要一个目标ui,需要被转换的坐标,摄像机,使用out将转换出来的坐标赋值到后面的变量内。 判断一张图片的_sprite.texture 还需要将图片资源设置成纹理可读。
这样好像不能打进图集啥的,具体的还没有了解到。 现在只是初学多多尝试,有错误和优化的地方多谢评论指正~