本文實(shí)例為大家分享了UGUI實(shí)現(xiàn)卡片橢圓方向滾動(dòng)的具體代碼,供大家參考,具體內(nèi)容如下
我們提供的服務(wù)有:成都做網(wǎng)站、網(wǎng)站制作、微信公眾號(hào)開(kāi)發(fā)、網(wǎng)站優(yōu)化、網(wǎng)站認(rèn)證、海勃灣ssl等。為上千企事業(yè)單位解決了網(wǎng)站和推廣的問(wèn)題。提供周到的售前咨詢(xún)和貼心的售后服務(wù),是有科學(xué)管理、有技術(shù)的海勃灣網(wǎng)站制作公司
搭建簡(jiǎn)單的場(chǎng)景
運(yùn)行效果
卡片移動(dòng)動(dòng)畫(huà)通過(guò)插件DoTween實(shí)現(xiàn)
控制腳本:
using UnityEngine; using System.Collections; using UnityEngine.UI; using DG.Tweening; public class CardMove : MonoBehaviour { GameObject[] sprites; int halfSize; Vector2 screenCenterPos; public float startAngle;//中間卡牌的角度 public float deltaAngle;//相鄰卡牌的角度差值 public float moveSpeed;//移動(dòng)動(dòng)畫(huà)的速度 public Vector3 center;//橢圓中心點(diǎn) public float A = 1;//long axis public float B = 1;//short axis int cardcount; // Use this for initialization void Start () { init (); } // Update is called once per frame void Update () { } /// <summary> /// 初始化卡牌顯示位置 /// </summary> void init(){ screenCenterPos = new Vector2 (Screen.width*0.5f,Screen.height*0.5f); cardcount = transform.childCount; halfSize = (cardcount - 1) / 2; sprites=new GameObject[cardcount]; for (int i = 0; i < cardcount; i++) { sprites [i] = transform.GetChild (i).gameObject; setPosition (i,false); setDeeps (i); } } /// <summary> /// 橢圓的半長(zhǎng)軸為A,半短軸為B,計(jì)算橢圓上一點(diǎn)的位置 /// x=A*cos(angle),y=B*sin(angle) /// </summary> /// <param name="index">Index.</param> /// <param name="userTweener">是否使用tween動(dòng)畫(huà).</param> void setPosition(int index,bool userTweener=true){ //計(jì)算每一張卡片在橢圓上相對(duì)中間卡牌的角度 float angle = 0; if(index<halfSize){//left angle=startAngle-(halfSize-index)*deltaAngle; }else if(index>halfSize){//right angle = startAngle + (index - halfSize) * deltaAngle; }else{//medim angle=startAngle; } //通過(guò)卡牌的角度,計(jì)算對(duì)應(yīng)的位置 float xpos = A*Mathf.Cos((angle/180)*Mathf.PI);//+center.x; float ypos = B*Mathf.Sin((angle/180)*Mathf.PI);//+center.y; Debug.Log ("index="+index+",xpos="+xpos+",ypos="+ypos); Vector2 pos = new Vector2 (xpos,ypos); // Debug.Log ("screenPos="+screenPos+",wordPos="+wordPos); //通過(guò)doTween控制卡片移動(dòng)動(dòng)畫(huà) if(!userTweener){ sprites [index].GetComponent<Image> ().rectTransform.DOMove(new Vector2(screenCenterPos.x+pos.x,screenCenterPos.y+pos.y),0f); }else sprites [index].GetComponent<Image> ().rectTransform.DOMove(new Vector2(screenCenterPos.x+pos.x,screenCenterPos.y+pos.y),1f); } /// <summary> /// 計(jì)算每一張卡片的層級(jí) /// </summary> /// <param name="index">Index.</param> void setDeeps(int index){ int deep = 0; if (index < halfSize) {//左側(cè)卡牌層級(jí),從左側(cè)到中間,層級(jí)依此遞增 deep=index; } else if (deep > halfSize) {//右側(cè)卡牌層級(jí),從中間到右側(cè),層級(jí)依此遞減 deep=sprites.Length-(index+1); } else { deep = halfSize; } sprites [index].GetComponent<RectTransform> ().SetSiblingIndex (deep); } /// <summary> /// 左側(cè)按鈕點(diǎn)擊,向左移動(dòng) /// </summary> public void OnLeftBtnClick(){ int length = sprites.Length; GameObject temp=sprites[0]; for (int i = 0; i < length; i++) {//移動(dòng)卡片在數(shù)組中的位置,依此向前移動(dòng)一位 if (i == length - 1) sprites [i] = temp; else sprites [i] = sprites [i + 1]; } for (int i = 0; i < length; i++) {//跟新數(shù)組卡片需要顯示的位置和層級(jí) setPosition (i); setDeeps (i); } } /// <summary> /// 右側(cè)按鈕點(diǎn)擊,向右移動(dòng) /// </summary> public void RightBtnClick(){ int length = sprites.Length; GameObject temp=sprites[length-1]; for (int i = length-1; i >=0; i--) { if (i == 0) sprites [i] = temp; else sprites [i] = sprites [i - 1]; } for (int i = 0; i < length; i++) { setPosition (i); setDeeps (i); } } }
源碼下載:地址
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。
當(dāng)前標(biāo)題:UnityUGUI實(shí)現(xiàn)卡片橢圓方向滾動(dòng)
網(wǎng)頁(yè)URL:http://sd-ha.com/article38/gcjisp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供手機(jī)網(wǎng)站建設(shè)、定制開(kāi)發(fā)、網(wǎng)站設(shè)計(jì)公司、動(dòng)態(tài)網(wǎng)站、網(wǎng)站收錄、微信小程序
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)