Functions and Methods Overview
KEY:
Start()
: when we hit the play button, this methd automatically gets calledUpdate()
: when we continue run the game, this method is called about 60 frames per second- 一个简单的damage system
public class DamageSystem : MonoBehaviour
{
// Start is called before the first frame update
public int health = 100;
private void Damage(int _damageAmout){
health -= _damageAmout;
if(health<1){
health = 0;
Destroy(this.gameObject);
}
}
// Update is called once per frame
void Update()
{
if(Input.GetKeyDown(KeyCode.Q)){
Damage(5);
}
}
}
Challenge: Pass the Obj with Color
题目附上 并做了点小小改变 颜色改为随机选取
public class PassObjWithColor : MonoBehaviour
{
/// PROBLEM
// create a program where you assign cube in the inspector and when u hit the space key,
// you pass that object into the function called ChangeColor
// the function should take both the gameobject and a color to change to and
// the opertation should be handled within the function
///
/// MORE
// choose the color randomly
///
// Start is called before the first frame update
public GameObject cubeObj;
public Color[] tarColors;
private void ChangeColor(GameObject obj, Color targetColor){
obj.GetComponent<MeshRenderer>().material.color = targetColor;
}
void Start()
{
tarColors = new Color[] {Color.blue, Color.red, Color.black, Color.green, Color.yellow, Color.magenta};
}
// Update is called once per frame
void Update()
{
if(Input.GetKeyDown(KeyCode.Space)){
Color tarColor = tarColors[Random.Range(0, tarColors.Length)];
ChangeColor(cubeObj, tarColor);
}
}
}
KEY:
这边改变颜色时选取部件用MeshRenderer 这个相当于Renderer的子类 然后就没啥要提的点了
Return Array
KEY:
数组作为返回值 没什么好说的
这边一个示例代码 讲讲其他点
public class GetCubes : MonoBehaviour
{
public GameObject[] players;
// Start is called before the first frame update
void Start()
{
players = GetAllPlayers();
foreach(var player in players){
Debug.Log(player.name +":"+player.GetComponent<MeshRenderer>().material.color);
}
}
// Update is called once per frame
void Update()
{
}
GameObject[] GetAllPlayers(){
GameObject[] allPlayers = GameObject.FindGameObjectsWithTag("Player");
foreach(var p in allPlayers){
p.GetComponent<MeshRenderer>().material.color = new Color(Random.value, Random.value, Random.value);
}
return allPlayers;
}
}
- 自写函数
GetAllPlayers()
首先使用GameObject.FindGameObjectsWithTag(string Tag);
获取所有标签为Player的物体 对于每个物体都赋予一个随机颜色 返回包含这些物体的数组 new Color(Random.value, Random.value, Random.value);
: Color包含四个参数rgba(red, green, blue, alpha透明度) 默认情况下a的值为1 在其他地方rgb的值通常都是0到255的整数 但是这边的话Color的参数都是float类型 所以范围是0到1 这个稍微注意一下 另外Random.value
的值是0到1之间任意一个float类型的数- 为了验证以上 写了
Start()
函数里面的内容 物体信息被很好的返回了 完毕
将数组作为参数传递
就学到这边时突然想起这件事情 毕竟在用c艹时这个地方一直迷迷糊糊的
先来个实验
public class Test : MonoBehaviour
{
// Start is called before the first frame update
public int[] testList = {1,2,3,4,5,6};
void Start()
{
ChangeNumber(testList);
}
// Update is called once per frame
void Update()
{
}
void ChangeNumber(int[] nums){
nums[0] = 100;
}
}
这说明数组作为参数传递时是实参 也就是说会改变数组的值
具体看看官方文档
数组可以作为实参传递给方法形参。 由于数组是引用类型,因此方法可以更改元素的值。