Unity笔记(13):Android Movement of Characters[2D]
创始人
2024-03-07 00:06:21

目录

1、搭建一个测试场景

2、建立画布设置移动按钮

3、编写脚本绑定按钮

AxisTouchButton :
ButtonHandler :

4、编写脚本绑定角色

原来的按键移动

修改为触摸按钮

5、导出为APK文件,手机下载进行测试


 

1、搭建一个测试场景

2、建立画布设置移动按钮

 

 

3、编写脚本绑定按钮

脚本资源来自:

 因为资源已经弃用了,资源商店搜是搜不到的。但是我之前下载过,所以有些还能用。

本次学习只需要以下代码即可:

左右移动按钮挂载同一个脚本:

AxisTouchButton :

using System;
using UnityEngine;
using UnityEngine.EventSystems;namespace UnityStandardAssets.CrossPlatformInput
{public class AxisTouchButton : MonoBehaviour, IPointerDownHandler, IPointerUpHandler{// designed to work in a pair with another axis touch button// (typically with one having -1 and one having 1 axisValues)public string axisName = "Horizontal"; // The name of the axispublic float axisValue = 1; // The axis that the value haspublic float responseSpeed = 3; // The speed at which the axis touch button respondspublic float returnToCentreSpeed = 3; // The speed at which the button will return to its centreAxisTouchButton m_PairedWith; // Which button this one is paired withCrossPlatformInputManager.VirtualAxis m_Axis; // A reference to the virtual axis as it is in the cross platform inputvoid OnEnable(){if (!CrossPlatformInputManager.AxisExists(axisName)){// if the axis doesnt exist create a new one in cross platform inputm_Axis = new CrossPlatformInputManager.VirtualAxis(axisName);CrossPlatformInputManager.RegisterVirtualAxis(m_Axis);}else{m_Axis = CrossPlatformInputManager.VirtualAxisReference(axisName);}FindPairedButton();}void FindPairedButton(){// find the other button witch which this button should be paired// (it should have the same axisName)var otherAxisButtons = FindObjectsOfType(typeof(AxisTouchButton)) as AxisTouchButton[];if (otherAxisButtons != null){for (int i = 0; i < otherAxisButtons.Length; i++){if (otherAxisButtons[i].axisName == axisName && otherAxisButtons[i] != this){m_PairedWith = otherAxisButtons[i];}}}}void OnDisable(){// The object is disabled so remove it from the cross platform input systemm_Axis.Remove();}public void OnPointerDown(PointerEventData data){if (m_PairedWith == null){FindPairedButton();}// update the axis and record that the button has been pressed this framem_Axis.Update(Mathf.MoveTowards(m_Axis.GetValue, axisValue, responseSpeed * Time.deltaTime));}public void OnPointerUp(PointerEventData data){m_Axis.Update(Mathf.MoveTowards(m_Axis.GetValue, 0, responseSpeed * Time.deltaTime));}}
}

左边设置

右边设置

 

 

 跳跃按钮挂载这个脚本:

ButtonHandler :

using System;
using UnityEngine;namespace UnityStandardAssets.CrossPlatformInput
{public class ButtonHandler : MonoBehaviour{public string Name;void OnEnable(){}public void SetDownState(){CrossPlatformInputManager.SetButtonDown(Name);}public void SetUpState(){CrossPlatformInputManager.SetButtonUp(Name);}public void SetAxisPositiveState(){CrossPlatformInputManager.SetAxisPositive(Name);}public void SetAxisNeutralState(){CrossPlatformInputManager.SetAxisZero(Name);}public void SetAxisNegativeState(){CrossPlatformInputManager.SetAxisNegative(Name);}public void Update(){}}
}

跳跃脚本设置

 

4、编写脚本绑定角色

原来的按键移动

using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using UnityEngine;public class Char : MonoBehaviour
{private Rigidbody2D rb;private float moveSpeed;private float dirX;private bool facingRight = true;private Vector3 localScale;private void Start(){rb = GetComponent();localScale = transform.localScale;moveSpeed = 5f;}private void Update(){dirX = Input.GetAxisRaw("Horizontal") * moveSpeed;if (Input.GetButtonDown("Jump") && rb.velocity.y == 0)rb.AddForce(Vector2.up * 700f);}private void FixedUpdate(){rb.velocity = new Vector2(dirX, rb.velocity.y);}private void LateUpdate(){if (dirX > 0)facingRight = true;else if (dirX < 0)facingRight = false;if (((facingRight) && (localScale.x < 0)) || ((!facingRight) && (localScale.x > 0)))localScale.x *= -1;transform.localScale = localScale;}
}

修改为触摸按钮

using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using UnityEngine;
using UnityStandardAssets.CrossPlatformInput;public class Character : MonoBehaviour
{private Rigidbody2D rb;private float moveSpeed;private Animator anim;private float dirX;private bool facingRight = true;private Vector3 localScale;private void Start(){rb = GetComponent();anim = GetComponent();localScale = transform.localScale;moveSpeed = 15f;}private void Update(){dirX = CrossPlatformInputManager.GetAxisRaw("Horizontal") * moveSpeed;if (CrossPlatformInputManager.GetButton("Jump")){// if ( && rb.velocity.y == 0)rb.AddForce(Vector2.up * 5f);anim.SetBool("isjump", true);}if (rb.velocity.y == 0)anim.SetBool("isjump", false);}private void FixedUpdate(){rb.velocity = new Vector2(dirX, rb.velocity.y);}private void LateUpdate(){if (dirX > 0)facingRight = true;else if (dirX < 0)facingRight = false;if (((facingRight) && (localScale.x < 0)) || ((!facingRight) && (localScale.x > 0)))localScale.x *= -1;transform.localScale = localScale;}
}

5、导出为APK文件,手机下载进行测试

 

相关内容

热门资讯

猫咪吃了塑料袋怎么办 猫咪误食... 你知道吗?塑料袋放久了会长猫哦!要说猫咪对塑料袋的喜爱程度完完全全可以媲美纸箱家里只要一有塑料袋的响...
埃菲尔铁塔在哪 中国仿建埃菲尔... 2019年4月26日,广西南宁市,街头惊现一座巨型山寨版埃菲尔铁塔,高约20米,白色塔身,造型逼真,...
苗族的传统节日 贵州苗族节日有... 【岜沙苗族芦笙节】岜沙,苗语叫“分送”,距从江县城7.5公里,是世界上最崇拜树木并以树为神的枪手部落...
北京的名胜古迹 北京最著名的景... 北京从元代开始,逐渐走上帝国首都的道路,先是成为大辽朝五大首都之一的南京城,随着金灭辽,金代从海陵王...
世界上最漂亮的人 世界上最漂亮... 此前在某网上,选出了全球265万颜值姣好的女性。从这些数量庞大的女性群体中,人们投票选出了心目中最美...
应用未安装解决办法 平板应用未... ---IT小技术,每天Get一个小技能!一、前言描述苹果IPad2居然不能安装怎么办?与此IPad不...
长白山自助游攻略 吉林长白山游... 昨天介绍了西坡的景点详细请看链接:一个人的旅行,据说能看到长白山天池全凭运气,您的运气如何?今日介绍...
脚上的穴位图 脚面经络图对应的... 人体穴位作用图解大全更清晰直观的标注了各个人体穴位的作用,包括头部穴位图、胸部穴位图、背部穴位图、胳...
demo什么意思 demo版本... 618快到了,各位的小金库大概也在准备开闸放水了吧。没有小金库的,也该向老婆撒娇卖萌服个软了,一切只...
埃菲尔铁塔在哪 中国仿建埃菲尔... 2019年4月26日,广西南宁市,街头惊现一座巨型山寨版埃菲尔铁塔,高约20米,白色塔身,造型逼真,...
苗族的传统节日 贵州苗族节日有... 【岜沙苗族芦笙节】岜沙,苗语叫“分送”,距从江县城7.5公里,是世界上最崇拜树木并以树为神的枪手部落...
北京的名胜古迹 北京最著名的景... 北京从元代开始,逐渐走上帝国首都的道路,先是成为大辽朝五大首都之一的南京城,随着金灭辽,金代从海陵王...
应用未安装解决办法 平板应用未... ---IT小技术,每天Get一个小技能!一、前言描述苹果IPad2居然不能安装怎么办?与此IPad不...