[U3D Learning Note] Unity C# Survival Guide (8) -- Functions and Methods

Functions and Methods Overview

KEY:

  • Start(): when we hit the play button, this methd automatically gets called
  • Update(): 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;
    }
}

在这里插入图片描述
这说明数组作为参数传递时是实参 也就是说会改变数组的值
具体看看官方文档

数组可以作为实参传递给方法形参。 由于数组是引用类型,因此方法可以更改元素的值。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值