Unity3D 2D游戏开发 官方教程。(十五)
来源:互联网 发布:网络系统集成质量控制 编辑:程序博客网 时间:2024/05/17 08:09
十五移动端
给游戏添加移动端控制代码。首先修改游戏的编译目标平台从PC换成移动端。然后在Player脚本中增加对触屏的控制代码。
15.1修改配置
点击File/Building Settings…
系统弹出Build Sertting窗口,默认为PC平台(PC,Max & Linux Standalone)如图:
确认Main场景在Scenes In Build窗口中。如果没有点击“Add Scene”添加。
修改PlatForm为IOS如图:
点击”Switch Platform”确认,然后关闭窗口即可。
15.2修改脚本
修改Player脚本代码如下:
using UnityEngine;using System.Collections;using UnityEngine.UI;public class Player : MovingObject { public int wallDamage = 1; public int pointsPerFood = 10; public int pointsPerSoda = 20; public float restartLevelDelay = 1f; public Text foodText; public AudioClip moveSound1; public AudioClip moveSound2; public AudioClip eatSound1; public AudioClip eatSound2; public AudioClip drinkSound1; public AudioClip drinkSound2; public AudioClip gameOverSound; private Animator animator; private int food ; private Vector2 touchOrigin = -Vector2.one; // Use this for initialization protected override void Start () { animator = GetComponent<Animator>(); food = GameManager.instance.playerFoodPoints; foodText.text = "Food: "+food; base.Start(); } // Update is called once per frame void Update () { if(!GameManager.instance.playersTurn) return ; int horizontal = 0; int vertical = 0 ; //判断平台执行对应代码 #if UNITY_EDITOR || UNITY_STANDALONE || UNITY_WEBPLAYER horizontal = (int)Input.GetAxisRaw("Horizontal"); vertical = (int)Input.GetAxisRaw("Vertical"); if(horizontal!=0) vertical = 0; #else //初始无记录信息 if(Input.touchCount>0) { Touch myTouch = Input.touches[0]; if(myTouch.phase == TouchPhase.begin) { //记录第一个触点 touchOrigin = myTouch.position; } else if(myTouch.phase == TouchPhase.Ended && touchOrigin.x>=0) { //触屏接触,记录位置,计算两点距离,大的优先作为控制命令 Vector2 touchEnd = myTouch.position; float x = touchEnd.x - touchOrigin.x; float y = touchEnd.y - touchOrigin.y; touchOrigin.x = -1; if(Mathf.Abs(x)>Mathf.Abs(y)) horizontal = x > 0 ? 1 : -1; else vertical = y > 0 ? 1 : -1; } } #endif if(horizontal!=0 || vertical!=0) AttemptMove<Wall>(horizontal,vertical); } private void OnDisable() { GameManager.instance.playerFoodPoints = food; } protected override void OnCantMove<T>(T component) { Wall hitWall = component as Wall; hitWall.DamageWall(wallDamage); animator.SetTrigger("PlayerChop"); } private void Restart() { Application.LoadLevel(Application.loadedLevel); } private void OnTriggerEnter2D(Collider2D other) { if(other.tag == "Exit") { Invoke("Restart",restartLevelDelay); enabled = false; } else if(other.tag == "Food") { food += pointsPerFood; foodText.text = "+" + pointsPerFood + " Food: "+food; SoundManager.instance.RandomizeSfx(eatSound1,eatSound2); other.gameObject.SetActive(false); } else if(other.tag == "Soda") { food += pointsPerSoda; foodText.text = "+" + pointsPerSoda + "Food: "+food; SoundManager.instance.RandomizeSfx(drinkSound1,drinkSound2); other.gameObject.SetActive(false); } } public void LoseFood(int loss) { animator.SetTrigger("PlayerHit"); food -= loss; foodText.text = "-" + loss + "Food: "+food; CheckIfGameOver(); } protected override void AttemptMove<T>(int xDir,int yDir) { food--; foodText.text = "Food: "+food; base.AttemptMove<T>(xDir,yDir); RaycastHit2D hit; if(Move (xDir,yDir,out hit)) { SoundManager.instance.RandomizeSfx(moveSound1,moveSound2); } CheckIfGameOver(); GameManager.instance.playersTurn =false; } private void CheckIfGameOver() { if(food <= 0 ) { SoundManager.instance.PlaySingle(gameOverSound); SoundManager.instance.musicSource.Stop(); GameManager.instance.GameOver(); } }}
到此整个教程示例已经全部结束。在学习过程中总是有这样或者那样的问题,可能是对环境的不熟悉也可能是粗心没有注意教程中细节的设计。特此记录笔记供以后查找使用。
0 0
- Unity3D 2D游戏开发 官方教程。(十五)
- Unity3D 2D游戏开发 官方教程。(一)
- Unity3D 2D游戏开发 官方教程。(二)
- Unity3D 2D游戏开发 官方教程。(三)
- Unity3D 2D游戏开发 官方教程。(四)
- Unity3D 2D游戏开发 官方教程。(五)
- Unity3D 2D游戏开发 官方教程。(六)
- Unity3D 2D游戏开发 官方教程。(七)
- Unity3D 2D游戏开发 官方教程。(八)
- Unity3D 2D游戏开发 官方教程。(九)
- Unity3D 2D游戏开发 官方教程。(十)
- Unity3D 2D游戏开发 官方教程。(十一)
- Unity3D 2D游戏开发 官方教程。(十二)
- Unity3D 2D游戏开发 官方教程。(十三)
- Unity3D 2D游戏开发 官方教程。(十四)
- unity3d 2D游戏开发
- Unity3D系列教程--使用免费工具在Unity3D中开发2D游戏 第一节
- Unity3D系列教程–使用免费工具在Unity3D中开发2D游戏 第三节
- Java的接口与内部类
- BES中间件Spark协议格式
- iOS UITextField 使用全解
- 常见gcc编译警告整理(开始)
- 初学Qt之--带参数的信号和槽的实现(入门级)
- Unity3D 2D游戏开发 官方教程。(十五)
- 使用js Math.random()函数生成指定范围的随机数字
- 修改min.js
- DirectShow SDK下载
- filco minila air在debian下的蓝牙适配安装
- Harris角点检测代码
- android中修改tablayout中的字体大小和颜色
- Build Your Own Images
- txt数据转换成pcd数据