Unity C#代码入门
Unity C#代码入门
1. 脚本基本结构
1.1 unity生成的模板
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Test : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
//设置初始值,相当于构造函数
}
// Update is called once per frame
void Update()
{
//不停循环执行的代码
}
}
1.2 常用的注解属性
[SerializeField]
float moveSpeed = 8;
Debug.Log("Celling");//控制台输出
other.gameObject.tag == "Celling"//获得tag
csharp如果不标明类别, 默认pravite
加上SerializeField, 能让pravite的变量, 在unity右侧直接调节
Time.deltaTime
不同的机器, 游戏帧数不同, Time.deltaTime可以让机器用相同的帧数执行
1.3 常见的类
类名 | 解释 |
---|---|
Text | 得到UI的text(需要导入using UnityEngine.UI;) |
Animator | 动画 |
SpriteRenderer | 渲染器(改变物体的外观?) |
GameObject | 获取游戏内物体 |
2. 源码解析
public class Player : MonoBehaviour
{
//类名必须一致
}
2.1 初始值定义
void Start()
{
HP = 10;
score = 0;
scoreTime = 0f;
anim = GetComponent<Animator>();
render = GetComponent<SpriteRenderer>();
deathSound = GetComponent<AudioSource>();
}
GetComponent
2.2 循环执行函数
void Update()
{
if (Input.GetKey(KeyCode.RightArrow))
{
transform.Translate(moveSpeed * Time.deltaTime, 0, 0);
render.flipX = false;
anim.SetBool("run", true);
}
else if (Input.GetKey(KeyCode.LeftArrow))
{
transform.Translate(-moveSpeed * Time.deltaTime, 0, 0);
render.flipX = true;
anim.SetBool("run", true);
}
else
{
anim.SetBool("run", false);
}
UpdateScore();
}
2.2.1 键盘监听函数
Input.GetKey(KeyCode.RightArrow) //输入的是右边箭头
2.2.2 变换属性 transform
transform.Translate(moveSpeed * Time.deltaTime, 0, 0);
将向x轴移动一定的距离
其实就是右侧的这些属性, 因此Rotation旋转, Scale缩放都能调整
render.flipX = false;
GetComponent<SpriteRenderer>().flipX = false;
同理:
anim.SetBool("run", true);
GetComponent<Animator>().SetBool("run", true);
动画也是如此
当然动画要复杂些
2.3 碰撞函数
void OnCollisionEnter2D(Collision2D other)
{
if (other.gameObject.tag == "Normal")
{
if (other.contacts[0].normal == new Vector2(0f, 1f))
{
currentFloor = other.gameObject;
ModifyHp(1);
other.gameObject.GetComponent<AudioSource>().Play();
Debug.Log("floor1");
}
}
else if (other.gameObject.tag == "Nails")
{
if (other.contacts[0].normal == new Vector2(0f, 1f))
{
currentFloor = other.gameObject;
ModifyHp(-2);
Debug.Log("floor2");
anim.SetTrigger("hurt");
other.gameObject.GetComponent<AudioSource>().Play();
}
}
else if (other.gameObject.tag == "Celling")
{
if (other.contacts[0].normal == new Vector2(0f, -1f))
{
currentFloor.GetComponent<BoxCollider2D>().enabled = false;
ModifyHp(-2);
Debug.Log("Celling");
anim.SetTrigger("hurt");
other.gameObject.GetComponent<AudioSource>().Play();
}
}
}
所谓碰撞就是, 遇到other这个物体会和它发生物理效果, 而不是穿过
2.3.1 碰撞检测精确到边
other.contacts[0].normal == new Vector2(0f, 1f)
//normal就是法向量
2.4 穿过函数
void OnTriggerEnter2D(Collider2D other)
{
if (other.gameObject.tag == "DeadLine")
{
ModifyHp(-12);
Die();
}
}
2.5 操作子组件
void UpdateHpBar()
{
for (int i = 0; i < HpBar.transform.childCount; i++)
{
if (HP > i)
{
HpBar.transform.GetChild(i).gameObject.SetActive(true);
}
else
{
HpBar.transform.GetChild(i).gameObject.SetActive(false);
}
}
}
主要是对gameObject的操作
3. 常用函数
3.1 结束游戏
void Die()
{
deathSound.Play();
Time.timeScale = 0f; //时间静止
btnReplay.SetActive(true);
}
3.2 重启游戏
//需要使用以下命名空间
using UnityEngine.SceneManagement;
public void Replay()
{
Time.timeScale = 1;
/*重新加载场景*/
SceneManager.LoadScene("SampleScene");
}
3.3 销毁物体
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Floor : MonoBehaviour
{
[SerializeField] float moveSpeed = 1f;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
transform.Translate(0,moveSpeed * Time.deltaTime,0);
if(transform.position.y>=4.7f)
{
Destroy(gameObject);
transform.parent.GetComponent<FloorManager>().SpwanFloor();
}
}
}
3.4 生成新的物体
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FloorManager : MonoBehaviour
{
[SerializeField] GameObject[] floorPerfabs;
public void SpwanFloor()
{
int r = Random.Range(0,2);
GameObject floor = Instantiate(floorPerfabs[r],transform);
floor.transform.position = new Vector3(Random.Range(-3.8f,3.8f),-5,0);
}
}