Unity 中 启动页 滚动 代码实现

来源:互联网 发布:小鲜肉演技知乎 编辑:程序博客网 时间:2024/06/05 15:20

启动页代码实现

1.创建一个Plane,并在Plane下放入三张图片


2.注意Canvas的缩放模式, 这样就能直接将Plane的缩放改为3倍了


3.编写脚本代码 挂在Plane上

(在Update中判断手机移动,然后使用iTween来移动UI)

using System.Collections;using System.Collections.Generic;using UnityEngine;using UnityEngine.SceneManagement;using UnityEngine.UI;public class PageControl : MonoBehaviour {public GameObject rectTrans;private Vector3 originPos;private Vector2 nowPos;public Image page0;public Image page1;public Image page2;public GameObject startBtn;int num;private Vector3 originTrans;public GameObject loadText;// Use this for initializationvoid Start () {SetPageNum ();originTrans = rectTrans.transform.position;Debug.Log ("x======"+ originPos.x);}// Update is called once per framevoid Update () { judgeFinger ();if (Application.platform == RuntimePlatform.Android && (Input.GetKeyDown (KeyCode.Escape))) {Application.Quit ();}if (Application.platform == RuntimePlatform.Android && (Input.GetKeyDown (KeyCode.Home))) {Application.Quit ();}}void judgeFinger(){if (Input.touchCount == 0 ) {return;}if (Input.GetTouch (0).phase == TouchPhase.Began) {originPos = Input.GetTouch (0).position;}if (Input.GetTouch (0).phase == TouchPhase.Moved) {nowPos = Input.GetTouch (0).position;}if (Input.GetTouch (0).phase == TouchPhase.Ended) {if (nowPos.x - originPos.x > 100) {Debug.Log ("后退"  );Retreat ();}if (nowPos.x - originPos.x < -100) {Debug.Log ("前进");Advance ();}nowPos = new Vector3 (0, 0, 0);originPos = new Vector3 (0, 0, 0);}}private void Retreat(){if (num == 0) {return;}num--;iTween.MoveTo(rectTrans,iTween.Hash("x",(originTrans.x - Screen.width * num),"time",0.7f));if (num == 2) {startBtn.SetActive (true);} else {startBtn.SetActive (false);}SetPageNum ();}private void Advance(){if (num == 2) {//SceneManager.LoadScene ("Main");return;}num++;iTween.MoveTo(rectTrans,iTween.Hash("x",(originTrans.x -Screen.width * num),"time",0.7f));if (num == 2) {startBtn.SetActive (true);}else {startBtn.SetActive (false);}SetPageNum ();}private void SetPageNum(){if (num == 0) {page0.enabled = true;page1.enabled = false;page2.enabled = false;}if (num == 1) {page0.enabled = false;page1.enabled = true;page2.enabled = false;}if (num == 2) {page0.enabled = false;page1.enabled = false;page2.enabled = true;}}public void StartGame(){SceneManager.LoadScene ("Main");}}


原创粉丝点击