需求:游戏中展示卡牌这种效果也是蛮酷炫并且使用的一种常见效果,下面我们就来实现以下这个效果是如何实现。

思考:第一看看到这个效果,我们首先会想到UGUI里面的ScrollRect,当然也可以用ScrollRect来实现缩短ContentSize的width来自动实现重叠效果,然后中间左右的卡牌通过计算来显示缩放,这里我并没有用这种思路来实现,我提供另外一种思路,就是自己去计算当前每个卡牌的位置和缩放值,不用UGUI的内置组件。

CODE:

1.卡牌拖动组件:

using UnityEngine;

using UnityEngine.EventSystems;

using UnityEngine.UI;

public enum DragPosition

{

Left,

Right,

Up,

Down,

}

[RequireComponent(typeof(Image))]

public class CDragOnCard : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler

{

public bool dragOnSurfaces = true;

public ScrollRect m_ScrollRect = null;

public CFixGridRect m_FixGridRect = null;

private RectTransform m_DraggingPlane;

public bool isVertical = false;

private bool isSelf = false;

private DragPosition m_dragPosition = DragPosition.Left;

public System.ActionDragCallBack = null;

public void OnBeginDrag(PointerEventData eventData)

{

Vector2 touchDeltaPosition = Vector2.zero;

#if UNITY_EDITOR

float delta_x = Input.GetAxis("Mouse X");

float delta_y = Input.GetAxis("Mouse Y");

touchDeltaPosition = new Vector2(delta_x, delta_y);

#elif UNITY_ANDROID || UNITY_IPHONE

touchDeltaPosition = Input.GetTouch(0).deltaPosition;

#endif

if (isVertical)

{

if(touchDeltaPosition.y > 0)

{

UnityEngine.Debug.Log("上拖");

m_dragPosition = DragPosition.Up;

}

else

{

UnityEngine.Debug.Log("下拖");

m_dragPosition = DragPosition.Down;

}

if (Mathf.Abs(touchDeltaPosition.x) > Mathf.Abs(touchDeltaPosition.y))

{

isSelf = true;

var canvas = FindInParents(gameObject);

if (canvas == null)

return;

if (dragOnSurfaces)

m_DraggingPlane = transform as RectTransform;

else

m_DraggingPlane = canvas.transform as RectTransform;

}

else

{

isSelf = false;

if (m_ScrollRect != null)

m_ScrollRect.OnBeginDrag(eventData);

}

}

else //水平

{

if (touchDeltaPosition.x > 0)

{

UnityEngine.Debug.Log("右移");

m_dragPosition = DragPosition.Right;

}

else

{

UnityEngine.Debug.Log("左移");

m_dragPosition = DragPosition.Left;

}

if (Mathf.Abs(touchDeltaPosition.x) < Mathf.Abs(touchDeltaPosition.y))

{

isSelf = true;

var canvas = FindInParents(gameObject);

if (canvas == null)

return;

if (dragOnSurfaces)

m_DraggingPlane = transform as RectTransform;

else

m_DraggingPlane = canvas.transform as RectTransform;

}

else

{

isSelf = false;

if (m_ScrollRect != null)

m_ScrollRect.OnBeginDrag(eventData);

}

}

}

public void OnDrag(PointerEventData data)

{

if (isSelf)

{

}

else

{

if (m_ScrollRect != null)

m_ScrollRect.OnDrag(data);

}

}

public void OnEndDrag(PointerEventData eventData)

{

if (isSelf)

{

}

else

{

if (m_ScrollRect != null)

m_ScrollRect.OnEndDrag(eventData);

if (m_FixGridRect != null)

m_FixGridRect.OnEndDrag(eventData);

}

if (DragCallBack != null)

DragCallBack(m_dragPosition);

}

static public T FindInParents(GameObject go) where T : Component

{

if (go == null) return null;

var comp = go.GetComponent();

if (comp != null)

return comp;

Transform t = go.transform.parent;

while (t != null && comp == null)

{

comp = t.gameObject.GetComponent();

t = t.parent;

}

return comp;

}

}

2.卡牌组件

using UnityEngine;

using System.Collections;

using UnityEngine.UI;

public class EnhanceItem : MonoBehaviour

{

// 在ScrollViewitem中的索引

// 定位当前的位置和缩放

public int scrollViewItemIndex = 0;

public bool inRightArea = false;

private Vector3 targetPos = Vector3.one;

private Vector3 targetScale = Vector3.one;

private Transform mTrs;

private Image mImage;

private int index = 1;

public void Init(int cardIndex = 1)

{

index = cardIndex;

mTrs = this.transform;

mImage = this.GetComponent();

mImage.sprite = Resources.Load(string.Format("Texture/card_bg_big_{0}", cardIndex % 6 + 1));

this.gameObject.GetComponent().onClick.AddListener(delegate () { OnClickScrollViewItem(); });

}

// 当点击Item,将该item移动到中间位置

private void OnClickScrollViewItem()

{

Debug.LogError("点击" + index);

EnhancelScrollView.GetInstance().SetHorizontalTargetItemIndex(scrollViewItemIndex);

}

/// /// 更新该Item的缩放和位移

///

public void UpdateScrollViewItems(float xValue, float yValue, float scaleValue)

{

targetPos.x = xValue;

targetPos.y = yValue;

targetScale.x = targetScale.y = scaleValue;

mTrs.localPosition = targetPos;

mTrs.localScale = targetScale;

}

public void SetSelectColor(bool isCenter)

{

if (mImage == null)

mImage = this.GetComponent();

if (isCenter)

mImage.color = Color.white;

else

mImage.color = Color.gray;

}

}

3.自定义的ScrollView组件

using UnityEngine;

using System.Collections;

using System.Collections.Generic;

using UnityEngine.UI;

public class EnhancelScrollView : MonoBehaviour

{

public AnimationCurve scaleCurve;

public AnimationCurve positionCurve;

public float posCurveFactor = 500.0f;

public float yPositionValue = 46.0f;

public ListscrollViewItems = new List();

private ListimageTargets = new List();

private EnhanceItem centerItem;

private EnhanceItem preCenterItem;

private bool canChangeItem = true;

public float dFactor = 0.2f;

private float[] moveHorizontalValues;

private float[] dHorizontalValues;

public float horizontalValue = 0.0f;

public float horizontalTargetValue = 0.1f;

private float originHorizontalValue = 0.1f;

public float duration = 0.2f;

private float currentDuration = 0.0f;

private static EnhancelScrollView instance;

private bool isInit = false;

public static EnhancelScrollView GetInstance()

{

return instance;

}

void Awake()

{

instance = this;

}

public void Init()

{

if ((scrollViewItems.Count % 2) == 0)

{

Debug.LogError("item count is invaild,please set odd count! just support odd count.");

}

if (moveHorizontalValues == null)

moveHorizontalValues = new float[scrollViewItems.Count];

if (dHorizontalValues == null)

dHorizontalValues = new float[scrollViewItems.Count];

if (imageTargets == null)

imageTargets = new List();

int centerIndex = scrollViewItems.Count / 2;

for (int i = 0; i < scrollViewItems.Count; i++)

{

scrollViewItems[i].scrollViewItemIndex = i;

Image tempImage = scrollViewItems[i].gameObject.GetComponent();

imageTargets.Add(tempImage);

dHorizontalValues[i] = dFactor * (centerIndex - i);

dHorizontalValues[centerIndex] = 0.0f;

moveHorizontalValues[i] = 0.5f - dHorizontalValues[i];

scrollViewItems[i].SetSelectColor(false);

}

//centerItem = scrollViewItems[centerIndex];

canChangeItem = true;

isInit = true;

}

public void UpdateEnhanceScrollView(float fValue)

{

for (int i = 0; i < scrollViewItems.Count; i++)

{

EnhanceItem itemScript = scrollViewItems[i];

float xValue = GetXPosValue(fValue, dHorizontalValues[itemScript.scrollViewItemIndex]);

float scaleValue = GetScaleValue(fValue, dHorizontalValues[itemScript.scrollViewItemIndex]);

itemScript.UpdateScrollViewItems(xValue, yPositionValue, scaleValue);

}

}

void Update()

{

if (!isInit)

return;

currentDuration += Time.deltaTime;

SortDepth();

if (currentDuration > duration)

{

currentDuration = duration;

//if (centerItem != null)

//{

// centerItem.SetSelectColor(true);

//}

if (centerItem == null)

{

var obj = transform.GetChild(transform.childCount - 1);

if (obj != null)

centerItem = obj.GetComponent();

if (centerItem != null)

centerItem.SetSelectColor(true);

}

else

centerItem.SetSelectColor(true);

if (preCenterItem != null)

preCenterItem.SetSelectColor(false);

canChangeItem = true;

}

float percent = currentDuration / duration;

horizontalValue = Mathf.Lerp(originHorizontalValue, horizontalTargetValue, percent);

UpdateEnhanceScrollView(horizontalValue);

}

private float GetScaleValue(float sliderValue, float added)

{

float scaleValue = scaleCurve.Evaluate(sliderValue + added);

return scaleValue;

}

private float GetXPosValue(float sliderValue, float added)

{

float evaluateValue = positionCurve.Evaluate(sliderValue + added) * posCurveFactor;

return evaluateValue;

}

public void SortDepth()

{

imageTargets.Sort(new CompareDepthMethod());

for (int i = 0; i < imageTargets.Count; i++)

imageTargets[i].transform.SetSiblingIndex(i);

}

public class CompareDepthMethod : IComparer{

public int Compare(Image left, Image right)

{

if (left.transform.localScale.x > right.transform.localScale.x)

return 1;

else if (left.transform.localScale.x < right.transform.localScale.x)

return -1;

else

return 0;

}

}

private int GetMoveCurveFactorCount(float targetXPos)

{

int centerIndex = scrollViewItems.Count / 2;

for (int i = 0; i < scrollViewItems.Count; i++)

{

float factor = (0.5f - dFactor * (centerIndex - i));

float tempPosX = positionCurve.Evaluate(factor) * posCurveFactor;

if (Mathf.Abs(targetXPos - tempPosX) < 0.01f)

return Mathf.Abs(i - centerIndex);

}

return -1;

}

public void SetHorizontalTargetItemIndex(int itemIndex)

{

if (!canChangeItem)

return;

EnhanceItem item = scrollViewItems[itemIndex];

if (centerItem == item)

return;

canChangeItem = false;

preCenterItem = centerItem;

centerItem = item;

float centerXValue = positionCurve.Evaluate(0.5f) * posCurveFactor;

bool isRight = false;

if (item.transform.localPosition.x > centerXValue)

isRight = true;

int moveIndexCount = GetMoveCurveFactorCount(item.transform.localPosition.x);

if (moveIndexCount == -1)

{

moveIndexCount = 1;

}

float dvalue = 0.0f;

if (isRight)

dvalue = -dFactor * moveIndexCount;

else

dvalue = dFactor * moveIndexCount;

horizontalTargetValue += dvalue;

currentDuration = 0.0f;

originHorizontalValue = horizontalValue;

}

public void OnBtnRightClick()

{

if (!canChangeItem)

return;

int targetIndex = centerItem.scrollViewItemIndex + 1;

if (targetIndex > scrollViewItems.Count - 1)

targetIndex = 0;

SetHorizontalTargetItemIndex(targetIndex);

}

public void OnBtnLeftClick()

{

if (!canChangeItem)

return;

int targetIndex = centerItem.scrollViewItemIndex - 1;

if (targetIndex < 0)

targetIndex = scrollViewItems.Count - 1;

SetHorizontalTargetItemIndex(targetIndex);

}

}

上面的代码好像不能用 我自己写了两种方法

1  使用 Scroll View 实现效果如下

代码如下

public int totalItemNum;//共有几个单元格

public int currentIndex;//当前单元格的索引

private float bilv ;

public float currentBilv = 0;

private void Awake()

{

scrollRect = GetComponent();

scrollRect.horizontalNormalizedPosition =0;

}

// Use this for initialization

void Start () {

Button[] button = scrollRect.content.GetComponentsInChildren();

totalItemNum= button.Length;

bilv = 1 / (totalItemNum - 1.00f);

}

public void OnBeginDrag(PointerEventData eventData)

{

beginMousePositionX = Input.mousePosition.x;

}

public void OnEndDrag(PointerEventData eventData)

{

float offSetX = 0;

endMousePositionX = Input.mousePosition.x;

offSetX = beginMousePositionX - endMousePositionX;

if (Mathf.Abs(offSetX)>firstItemLength)

{

if (offSetX>0)

{

//当前的单元格大于等于单元格的总个数

if (currentIndex >= totalItemNum-1 )

{

Debug.Log("左滑动-------");

return;

}

currentIndex += 1;

scrollRect.DOHorizontalNormalizedPos(currentBilv += bilv, 0.2f);

Debug.Log("左滑动");

}

else

{

Debug.Log("右滑动");

//当前的单元格大于等于单元格的总个数

if ( currentIndex < 1)

{

return;

}

currentIndex -= 1;

scrollRect.DOHorizontalNormalizedPos(currentBilv -= bilv, 0.2f);

}

}

}

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

using DG.Tweening;

using UnityEngine.UI;

public class ThreePage1 : MonoBehaviour {

public GameObject[] images;

int currentIndex;

public float DistancesNum = 250;

public float min = 0.8f;

public float max = 1.4f;

public float speed = 0.2f;

public float Alpha = 0.8f;

void Start () {

InitRectTranform();

}

public void InitRectTranform()

{

currentIndex = images.Length / 2;

for (int i = currentIndex + 1; i < images.Length; i++)

{

images[i].GetComponent().anchoredPosition = new Vector3((i - currentIndex) * DistancesNum, 0, 0);

}

int num = 0;

for (int i = currentIndex; i >= 0; i--)

{

images[i].GetComponent().anchoredPosition = new Vector3(-num * DistancesNum, 0, 0);

num++;

}

foreach (var item in images)

{

if (item != images[currentIndex])

{

item.GetComponent().localScale = new Vector3(min, min);

item.GetComponent().color = new Color(1, 1, 1, Alpha);

}

else

{

item.GetComponent().localScale = new Vector3(max, max);

}

}

}

public void Left()

{

ButtonManager._Instances.PlayInput();

OnLeftButtonClick();

}

public void OnLeftButtonClick()

{

if (currentIndex < images.Length-1)

{

foreach (GameObject item in images)

{

(item.GetComponent()).DOAnchorPosX(item.GetComponent().anchoredPosition.x- DistancesNum, speed);

}

images[currentIndex].GetComponent().color = new Color(1, 1, 1, Alpha);

images[currentIndex].GetComponent().DOScale(min, speed);

currentIndex += 1;

images[currentIndex].GetComponent().DOScale(max, speed);

images[currentIndex].GetComponent().color = new Color(1, 1, 1, 1f);

}

}

public void Right()

{

ButtonManager._Instances.PlayInput();

OnRightButtonClick();

}

public void OnRightButtonClick()

{

if (currentIndex > 0)

{

foreach (GameObject item in images)

{

(item.GetComponent()).DOAnchorPosX(item.GetComponent().anchoredPosition.x + DistancesNum, speed);

}

images[currentIndex].GetComponent().DOScale(min, speed);

images[currentIndex].GetComponent().color = new Color(1, 1, 1, Alpha);

currentIndex -= 1;

images[currentIndex].GetComponent().DOScale(max, speed);

images[currentIndex].GetComponent().color = new Color(1, 1, 1, 1f);

}

}

private void OnEnable()

{

isOn = true;

timess = 0;

//jarodInputController.IsShiYong = true;

}

private void OnDisable()

{

InitRectTranform();

jarodInputController.IsShiYong = false;

}

}

ugui unity 图片缩放循环_Unity3d UGUI缩放循环拖动展示卡牌效果相关推荐

  1. ugui unity 图片缩放循环_Unity基础系列(四)——构造分形(递归的实现细节)...

    点击蓝字关注我们吧! 目录 1 如何构建分形 2 展示内容 3 构造子节点 4 塑造子节点 5 创建多个子节点 6 更多的子节点,更好的代码 7 爆炸性生长 8 添加颜色 9.随机化Mesh 10 使 ...

  2. unity图片拖不进去_Unity UGUI实现简单拖拽图片功能

    这一篇博客我们来使用 UGUI 实现图片的拖拽功能. 说到拖拽,那必然离不开坐标,UGUI 的坐标有点不一样,它有两种坐标,一种是屏幕坐标,还有一种就是 UI 在Canvas内的坐标(暂时叫做ugui ...

  3. Unity NGUI高端游戏小地图6组图标Unity游戏素材资源(UGUI可用图片素材)

    Unity NGUI高端游戏小地图6组图标Unity游戏素材资源(UGUI可用图片素材) 项目下载 Demo下载地址 效果图 例子场景 项目下载 Demo下载地址

  4. Unity实现以鼠标为中心缩放物体(一)

    Unity实现以鼠标为中心缩放物体(一) 方法一:UGUI下动态设置Pivot Canvas.renderMode = ScreenSpaceCamera Canvas.renderMode = Sc ...

  5. 图片的多点触控缩放与移动

    整理自 鸿洋大神的慕课网视频 加了很多自己理解的注注释 package MyView; import android.content.Context; import android.graphics. ...

  6. unity游戏引擎下的UGUI(2)

    unity游戏引擎下的UGUI(1)unity游戏引擎下的UGUI_m0_57997518的博客-CSDN博客 4.Image Source Image(放图片)(图片要转换为2D and UI) C ...

  7. Unity中游戏卡牌滚动效果,EnhanceScrollview(适用于NGUI,UGUI)

    最近项目中的活动面板要做来回滚动卡牌预览效果,感觉自己来写的话,也能写,但是可能会比较耗时,看到Github上有开源的项目,于是就借用了,Github的资源地址是:https://github.com ...

  8. js实现移动端图片预览:手势缩放, 手势拖动,双击放大...

    原文:js实现移动端图片预览:手势缩放, 手势拖动,双击放大... 前言 本文将介绍如何通过js实现移动端图片预览,包括图片的 预览模式,手势缩放,手势拖动,双击放大等基本功能: 扫码查看示例效果: ...

  9. ugui unity 取消选择_关于Unity中的UGUI优化,你可能遇到这些问题

    ​关键字 界面制作 ​网格重建 ​界面切换 ​加载相关 ​字体 ​ 一.界面制作 Q1:UGUI里的这个选项 ,应该是ETC2拆分Alpha通道的意思,但是在使用中并没起作用?请问有没有什么拆分的标准 ...

最新文章

  1. 自定义定时器的一点总结
  2. mysql读书笔记---mysql safe update mode
  3. mybatis使用过程遇到的一些问题及解决方法
  4. Gartner磁盘阵列魔力象限:华为、昆腾、Infinidat势头迅猛
  5. 32岁转行还来得及吗?
  6. 开机一直转圈_天气转凉,电脑早上开机也需要预热了吗?
  7. 管理感悟:就事不论事
  8. SoapUI 入门指南
  9. html弹出广告设计,全屏弹出广告交互设计探讨
  10. html读取fbx文件,读取Fbx文件中的信息.doc
  11. 显示器用软件调整亮度_如何使用正确的软件调整电脑显示器的亮度
  12. Android应用 中英文切换
  13. “小糊涂“为何携手”无穷小”飞进大学校园
  14. SSR 实战:官网开发指南
  15. NBS-Predict:基于脑网络的机器学习预测
  16. 每日三省吾身:2014-1-16
  17. pg_stat_database的字段tup_returned,tup_fetched含义
  18. GitHub页面接管并利用-子域名接管
  19. 数据结构实验三 用栈实现进制转换和计算器
  20. 微信公众平台在高校教育工作中的使用

热门文章

  1. Mysql创建数据库并设置字符集
  2. Winform cs 快速开发框架源码,仿网页风格,纯C#实现
  3. 关于win10电脑下,HP网络打印机已连接不打印,需要用户干预,或者用户注意等处理方案
  4. 微信小程序开发实战(页面导航)
  5. 腾讯在海外游戏和短视频广告领域的新增长机会
  6. 第6章第3节:颜色搭配:幻灯片常用的配色技巧 [PowerPoint精美幻灯片实战教程]
  7. 今日头条这么厉害,被称为宇宙条!到底用了什么牛逼技术!
  8. 文件宝iOS/iPhone/iPad客户端简介
  9. 100天精通Java (基础篇) —面向对象编程1.0【Java分水岭】
  10. No shutdown animation in the electricity display only 1%