龙空技术网

6.Unity脚本初步

青少年编程ABC 100

前言:

今天咱们对“unity脚本入门”大体比较关切,朋友们都需要剖析一些“unity脚本入门”的相关资讯。那么小编在网摘上汇集了一些对于“unity脚本入门””的相关知识,希望各位老铁们能喜欢,看官们一起来学习一下吧!

6.脚本初步6.1 输入

可以使用 Input Manager 查看、编辑和添加不同的轴。

选择菜单 Edit -> Project Settings -> Input ,打开 Input Manager,如下图所示:

可以根据需要添加自己的输入轴。

在 Input Manager 中设置的轴都可以在代码中处理。

6.1.1编写输入脚本

访问输入需要使用 Input 对象。

GetAxis() - 将轴名称作为字符串参数,返回该轴的值。

示例:

获取箭头键或者 A、S、D、W 键的输入。代码如下:

using System.Collections;using System.Collections.Generic;using UnityEngine;public class TestAxis : MonoBehaviour{    // Start is called before the first frame update    void Start()    {            }    // Update is called once per frame    void Update()    {        float hVal = Input.GetAxis("Horizontal");        float vVal = Input.GetAxis("Vertical");        if (hVal > 0) print("Right");        if (hVal < 0) print("Left");        if (vVal > 0) print("Up");        if (vVal < 0) print("Down");    }}
6.1.2 键盘输入GetKey() - 获取是否按下了某个键,参数为某个键,如:KeyCode.K。

示例:判断运行过程中是否按下了 M 键。代码如下:

if (Input.GetKey(KeyCode.M)) print("Press the M");
6.1.3 鼠标输入

鼠标输入包括:鼠标键和鼠标移动。

GetMouseButtonDown() - 是否按下了鼠标键,参数为 0~2。GetAxis("Mouse X") 和 GetAxis("Mouse Y") - 获取沿 x 轴和 y 轴的移动,只是自上一帧起移动的距离。

示例代码:

if (Input.GetMouseButtonDown(0)) print("Press the left mouse button."); // 鼠标左键if (Input.GetMouseButtonDown(1)) print("Press the right mouse button."); // 鼠标右键if (Input.GetMouseButtonDown(2)) print("Press the center mouse button."); // 鼠标中键print(Input.GetAxis("Mouse X"));print(Input.GetAxis("Mouse Y"));
6.2 变换对象

对象的变换由它的位置、旋转和缩放组成,使用内置的 Translate、Rotate 方法和 localScale 变量来实现。

示例代码:

using System.Collections;using System.Collections.Generic;using UnityEngine;public class CubeController : MonoBehaviour{    // Start is called before the first frame update    void Start()    {            }    // Update is called once per frame    void Update()    {        transform.Translate(0.05f, 0f, 0f);        transform.Rotate(0f, 0f, 1f);        transform.localScale = new Vector3(1.5f, 1.5f, 1.5f);    }}
6.3 访问对象使用 GameObject 类型的公共变量,在 Inspector 窗口中把相应的对象拖拽到脚本组件上。GameObject.Find() 以对象的名称为参数,返回第一个具有该名称的对象。GameObject.FindWithTag() 以标签名称为参数,返回与标签名称相同的对象。

标签的使用:选择菜单 Edit -> Project Settings -> Tags and Layers,打开 Tags and Layers,如下图所示:

可以在此处添加新标签,添加好后,在选中游戏对象后可以在 Tag 属性中选择到设置的标签。

示例:创建一个空物体用于挂载脚本,在场景中创建立方体,创建脚本实现如下功能:

1.按左右键,沿 x 轴负、正方向移动立方体,按上下键,沿 y 轴负、正方向移动立方体。

2.沿 y 轴移动鼠标时,绕 x 轴旋转立方体,沿 x 轴移动鼠标时,绕 y 轴旋转立方体。

3.按下 M 键时放大立方体,按下 N 键时缩小立方体。

代码如下:

using System.Collections;using System.Collections.Generic;using UnityEngine;public class CubeController : MonoBehaviour{    GameObject cube;        // Start is called before the first frame update    void Start()    {        cube = GameObject.Find("Cube");    }    // Update is called once per frame    void Update()    {        float hVal = Input.GetAxis("Horizontal");        float vVal = Input.GetAxis("Vertical");        cube.transform.Translate(hVal/100, 0f, 0f);        cube.transform.Translate(0f, vVal/100, 0f);        float xVal = Input.GetAxis("Mouse X");        float yVal = Input.GetAxis("Mouse Y");        cube.transform.Rotate(yVal*10, 0f, 0f);        cube.transform.Rotate(0f, xVal*10, 0f);        float oldScale = cube.transform.localScale.x;        float bigScale =  oldScale * 1.025f;        float smallScale = oldScale * 0.99f;         if (Input.GetKey(KeyCode.M)) cube.transform.localScale = new Vector3(bigScale, bigScale, bigScale);        if (Input.GetKey(KeyCode.N)) cube.transform.localScale = new Vector3(smallScale, smallScale, smallScale);    }}

标签: #unity脚本入门