U3D 点击按钮控制物体上下左右移动,并变化按钮文本
2019-07-17 20:26
387 查看
版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/weixin_42872122/article/details/96357524
1、创建控制物体上下左右移动的C#脚本:“move”
[code]using System.Collections; using System.Collections.Generic; using UnityEngine; public class move : MonoBehaviour //调用“move”脚本 { bool start = false; //布尔函数判断是否两种状态 // Use this for initialization void Start () { start = false; //将开始点击状态赋值为否 } // Update is called once per frame void Update () { if (start == true) //检测到点击按键 if (Input.GetKey(KeyCode.UpArrow)) //上键 { transform.eulerAngles = new Vector3(0, 0, 0); //欧拉角:确定定点转动刚体位置的3个一组独立角参量 //vector3代表三维空间 transform.position += transform.forward * 10; //进给量:10 } if (Input.GetKey(KeyCode.LeftArrow)) { transform.eulerAngles = new Vector3(0, 270, 0); transform.position += transform.forward * 10; } if (Input.GetKey(KeyCode.DownArrow)) { transform.eulerAngles = new Vector3(0, 180, 0); transform.position += transform.forward * 10; } if (Input.GetKey(KeyCode.RightArrow)) { transform.eulerAngles = new Vector3(0, 90, 0); transform.position += transform.forward * 10; } } public void OnButtonClick() { start = !start; //赋予点击”开始“按钮时的状态是否变化(点击”开始“按钮可以移动,再点击不能移动) } }
2、创建控制点击按钮文本发生变化的C#脚本:”BText“
[code]using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;//调用UI public class BText : MonoBehaviour { public Text Bt; //以下为原来的代码不用管 // Use this for initialization void Start () { } // Update is called once per frame void Update () { } //简单的条件语句判断 public void OnButtonClick() { if (Bt.text == "开始") { Bt.text = "结束"; } else { Bt.text = "开始"; } } }
3、操作具体步骤:
3.1、“move”控制cube的移动,将其添加到cube上;
“BText”控制Button的变化,将其添加到Button上。
3.2、添加Button里的点击事件:
第一步将cube托进对象里,选择上面脚本里调用的函数。即实现点击按钮cube可以移动,再次点击按钮cube不能移动。
第二步将Button托进对象里,选择上面脚本里调用的函数。即实现点击按钮Button的文本发生变化,为“开始”和“结束”。
3.3、运行即可看到效果
相关文章推荐
- 点击上下左右按钮让背景上下左右移动
- Unity 3D 控制物体上下左右均匀移动脚本
- js实现简单的动画(4个按钮控制图片上下左右移动)
- unity 点击按钮控制物体移动
- Isometric Game Programming 笔记(二)按键控制地图上下左右方向移动
- js 键盘控制表格input上下左右移动
- 镜头变换01_U3D_让camera上下左右移动和拉远拉近
- js实现按键球,小球随键盘上下左右控制移动
- html中上下左右控制对象移动Demo
- 上下左右 键控制人移动
- unity3d 鼠标控制物体上下、左右、旋转
- 上下左右 键控制人移动
- Echarts使用心得(增加全屏按钮,点击一个图另外一个图变化,双环内外环关联,全屏左右切换)
- 点击按钮控制div上下运动——函数封装2
- u3d 鼠标点击位置,物体移动过去。 U3d mouse clicks position, objects move past.
- js各种图片轮播滑动插件 上下,左右缩略图按钮点击图
- Unity 之 上下左右键控制物体移动(通过加速度、速度进行移动)
- <Unity3D>上下左右 键控制人移动
- jQuery实现通过方向键控制div块上下左右移动的方法【测试可用】
- U3D (一)鼠标点击 物体移动