当前位置:网站首页>Unity3D中的ref、out、Params三种参数的使用
Unity3D中的ref、out、Params三种参数的使用
2022-08-05 05:18:00 【IT学徒.】
ref
作用:
将一个变量传入一个函数中进行"处理","处理"完成后,再将"处理"后的值带出函数。
语法:
使用时形参和实参都要添加ref关键字。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Test: MonoBehaviour {
void Start () {
int a = 1;
int b = 2;
refTest(ref a, b);
Debug.Log("a:" + a + " " + "b:" + b);
}
private void refTest(ref int num1,int num2)
{
num1 = 5;
num2 = 10;
}
void Update () {
}
}
输出:
out
作用:
一个函数中如果返回多个不同类型的值,就需要用到out参数。
语法:
函数外部可以不为变量赋值,但是函数内部必须为函数赋值。且形参和实参都要添加out关键字。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Test : MonoBehaviour
{
void Start()
{
int b;
outTest(out b);
Debug.Log("b:" + b);
}
private void outTest(out int num1)
{
num1 = 5;
}
void Update()
{
}
}
输出:
注意:
out修饰的参数真正赋值是在函数内部,在外部赋值没有用处,具体如下:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Test : MonoBehaviour
{
void Start()
{
int b;
b = 10;
outTest(out b);
Debug.Log("b:" + b);
}
private void outTest(out int num1)
{
num1 = 5;
}
void Update()
{
}
}
输出
Params
可变参数params(数组参数)允许我们动态传入不同个数的参数。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Test : MonoBehaviour
{
public void Getdd(params int[] pp)
{
foreach (var p in pp)
{
print(p);
}
}
void Start()
{
Getdd(1, 2, 3);
}
void Update()
{
}
}
输出:
边栏推荐
猜你喜欢

论那些给得出高薪的游戏公司底气到底在哪里?
![[Kaggle project actual combat record] Steps and ideas sharing of a picture classification project - taking leaf classification as an example (using Pytorch)](/img/7d/7f1301c30034f1c247d41f04c40244.png)
[Kaggle project actual combat record] Steps and ideas sharing of a picture classification project - taking leaf classification as an example (using Pytorch)

如何使用Houdini进行程序化优化?

你要找的cocos面试答案都在这里了!

1008 数组元素循环右移问题 (20 分)

CVPR2020 - 自校准卷积

C语言—三子棋的实现

LeetCode刷题之第24题

电子产品量产工具(2)- 输入系统实现

Redis集群(docker版)——从原理到实战超详细
随机推荐
LeetCode刷题之第24题
[Intensive reading of the paper] R-CNN's Bounding box regression problem is detailed
硬核!Cocos开发面试必备十问,让你offer拿到手软
【ts】typescript高阶:联合类型与交叉类型
[Pytorch study notes] 10. How to quickly create your own Dataset dataset object (inherit the Dataset class and override the corresponding method)
九、响应处理——内容协商底层原理
【UiPath2022+C#】UiPath Switch
ACL 的一点心得
Polygon计算每一个角的角度
网络信息安全运营方法论 (中)
UE4美术你有必要了解的数学基础
每日一题-两数相加-0711
每日一题-下一个排列-0723
《基于机器视觉测量系统的工业在线检测研究》论文笔记
多边形等分
tensorflow的session和内存溢出
LeetCode刷题之第746题
最简单的防抖节流理解法
C语言—扫雷的实现
LeetCode刷题之第1024题