当前位置:网站首页>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()
{
}
}
输出:
边栏推荐
- 基于STM32F407的一个温度传感器报警系统(用的是DS18B20温度传感器,4针0.96寸OLED显示屏,并且附带日期显示)
- [Pytorch study notes] 10. How to quickly create your own Dataset dataset object (inherit the Dataset class and override the corresponding method)
- LeetCode刷题之第416题
- MySQL主从复制—有手就能学会的MySQL集群搭建教程
- TinyFlashDB:一种超轻量的可纠错的通用单片机flash存储方案
- Contextual non-local alignment of full-scale representations
- 电子产品量产工具(1)- 显示系统实现
- 【nodejs】第一章:nodejs架构
- 浅谈遇到的小问题
- 每日一题-三数之和-0716(2)
猜你喜欢
随机推荐
六、请求处理—获取请求参数系列注解是怎样工作的?
将一句话的单词进行倒置(C语言纯代码)
吞吐?带宽?傻傻分不清楚
《基于机器视觉测量系统的工业在线检测研究》论文笔记
网络信息安全运营方法论 (下)
PID详解
《基于机器视觉的输电线路交叉点在线测量方法及技术方案》论文笔记
HuiFer 带你读懂 BeanFactory getBean 方法
常用 crud 的思考和设计
多边形等分
“元宇宙”是个啥?都有哪些大招?
Polygon计算每一个角的角度
LeetCode刷题之第701题
Jupyter notebook选择不同的Anaconda环境作为内核运行
idea 快速日志
LeetCode刷题之第530题
LeetCode刷题之第55题
Leetcode刷题——对链表进行插入排序
framebuffer应用编程及文字显示(1)
亲身实感十多年的面试官面试的题目