1. //定义函数
    2. private Vector3 CenterPoint()
    3. {
    4. Vector3 point = new Vector3 ();
    5. foreach (Transform GameObject in GameObjects)
    6. {
    7. point += GameObject.position;
    8. }
    9. point = point / GameObjects.Length;
    10. return point;
    11. }
    12. //使用函数
    13. private void Start()
    14. {
    15. Vector3 centerPoint = CenterPoint();//使用函数的返回值
    16. }
    1. //设定函数
    2. private void SetHealth(float a)
    3. {
    4. health.value = a;
    5. }
    6. //使用函数
    7. private void Start()
    8. {
    9. SetHealth(60);//传入参数并引用
    10. }