当前位置:网站首页>The use of three parameters of ref, out, and Params in Unity3D
The use of three parameters of ref, out, and Params in Unity3D
2022-08-05 06:32:00 【IT apprentice.】
ref
作用:
将一个变量传入一个函数中进行"处理","处理"完成后,再将"处理"后的值带出函数.
语法:
Both formal and actual parameters must be added when usedref关键字.
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参数.
语法:
It is not necessary to assign values to variables outside the function,But inside the function must assign a value to the function.And both formal parameters and actual parameters should be addedout关键字.
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()
{
}
}
输出:
注意:
outThe real assignment of the modified parameter is inside the function,Assignment outside is useless,具体如下:
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(数组参数)Allows us to dynamically pass in a different number of parameters.
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()
{
}
}
输出:
边栏推荐
- Transformer interprets and predicts instance records in detail
- Teach you simple steps to achieve industrial raspberries pie properly installed RS232 USB drive
- 系统基础-学习笔记(一些命令记录)
- 七种让盒子水平垂直居中的方法
- The size of the screen adaptation
- 7 steps to complete cloud monitoring
- Network wiring and digital-to-system conversion
- Logical volume creation
- 多线程之传递参数
- Wechat applet page jump to pass parameters
猜你喜欢

IP packet format (ICMP protocol and ARP protocol)

static routing

One-arm routing experiment and three-layer switch experiment

Unity realizes first-person roaming (nanny-level tutorial)

transport layer protocol

LeetCode练习及自己理解记录(1)

Network wiring and digital-to-system conversion

单臂路由实验和三层交换机实验

The problem come from line screening process
![[问题已处理]-jenkins流水线checkout超时](/img/3d/c14276d2b5ce18fc3d1288abb059c0.png)
[问题已处理]-jenkins流水线checkout超时
随机推荐
初识网页与浏览器
IP address and subnet division
LeetCode中常用语言的一些基本方法记录
有哪些事情是你做了运维才知道的?
Mongodb query analyzer parsing
Teach you simple steps to achieve industrial raspberries pie properly installed RS232 USB drive
RAID磁盘阵列
spark operator-textFile operator
磁盘管理与文件系统
Mina disconnects and reconnects
DevOps process demo (practical record)
Quick question and quick answer - FAQ of Tencent Cloud Server
IP packet format (ICMP protocol and ARP protocol)
VRRP overview and experiment
原生JS带你了解数组方法实现及使用
CIPU, what impact does it have on the cloud computing industry?
入职前,没想到他们玩的这么花
DevOps-了解学习
Mina断线重连
js dynamically get screen width and height