【Unity】【CSharp(C#)基础】学习总结2——数组 函数

1 数组 Array

数组是有序的相同数据类型元素组成的有限集合,内存线性连续
(1)数组元素数据类型必须与数组数据类型相同
(2)数组元素数量有限
(3)数组下标索引从0开始,即一维数组的最后一个元素下标索引 = 数组容量 - 1(一维数组)

1.1 数组声明

1.1.1 一维数组声明

        <data_type>[ ] variable_name;

        < 数据类型 >[ ]  数组名称;

int[] array;                 // 数组声明,但是系统没有分配内存
int[] array2 = new int[5];   // 数组声明且分配了内存
1.1.2 二维数组声明

        <data_type>[ , ] variable_name;

        < 数据类型 >[ , ]  数组名称;

int[,] arr2d = new int[2, 3]
{
    { 1, 2, 3 },
    { 4, 5, 6 },
};
1.1.3 三维数组声明

        <data_type>[ , , ] variable_name;

        < 数据类型 >[ , , ]  数组名称;

int[,,] arr3d = new int[2, 3, 4];

1.2 一维数组

1.2.1 一维数组访问赋值
array2[0] = 10;
Debug.Log("\n [原始数据] 数组 array2 中第 1 个数据为: " + array2[0]);
array2[0] = 15;
Debug.Log("\n [修改数据] 数组 array2 中第 1 个数据为: " + array2[0]);

若给定数组大小为N(下段代码中 array2 的数组容量为5),但是访问了数组中第N+1个元素或之后,则会出现数组的越界访问,但是编译不报错,运行之后会报错

array2[6] = 100; // 不允许数组越界访问,编译不报错,运行报错

运行报错,显示索引越界

1.2.2 一维数组初始化

一般具有一下三种格式

int[] array3 = new int[4] { 1, 2, 3, 4 }; // 容量显示声明
int[] array4 = new int[ ] { 1, 2, 3, 4 }; // 容量隐式声明
int[] array5 = { 1, 2, 3, 4 };
1.2.3 数组容量

获取数组容量一般使用自带的Length

Debug.Log("\n 数组 array5 的容量为: " + array5.Length);

 1.3 二维数组

1.3.1 二维数组访问赋值
 Debug.Log("\n [原始数据] 数组 arr2d 中第 1 行第 2 列的数据为: " + arr2d[0, 1]);
 arr2d[0, 1] = 15;
 Debug.Log("\n [修改数据] 数组 arr2d 中第 1 行第 2 列的数据为: " + arr2d[0, 1]);

1.3.2 二维数组容量

获取二维数组所有容量一般使用自带的Length

Debug.Log("\n 数组 arr2d 的容量为: " + arr2d.Length);

如果获取二维数组的行数或者列数,一般使用自带的GetLength()方法,并传入维度参数

Debug.Log("\n 数组 arr2d 的行数为: " + arr2d.GetLength(0));
Debug.Log("\n 数组 arr2d 的列数为: " + arr2d.GetLength(1));

1.4 三维数组

1.4.1 三维数组访问赋值
arr3d[0, 1, 2] = 10; 
Debug.Log("\n [原始数据] 数组 arr3d 中第 1 行第 2 列第 3 层的数据为: " + arr3d[0, 1, 2]);
arr3d[0, 1, 2] = 15;
Debug.Log("\n [修改数据] 数组 arr3d 中第 1 行第 2 列第 3 层的数据为: " + arr3d[0, 1, 2]);

注意,一般多维数组的初始化和赋值会通过编写函数来遍历数组并赋值,基本不会用直接访问的形式逐个初始化

1.4.2 三维数组容量

获取三维数组所有容量一般使用自带的Length

Debug.Log("\n 数组 arr3d 的容量为: " + arr3d.Length);

如果获取三维数组的不同维度上的长度,一般使用自带的GetLength()方法,并传入维度参数

Debug.Log("\n 数组 arr3d 的行数为: " + arr3d.GetLength(0));
Debug.Log("\n 数组 arr3d 的列数为: " + arr3d.GetLength(1));
Debug.Log("\n 数组 arr3d 的层数为: " + arr3d.GetLength(2));


2 函数 Function

        函数(也称方法或接口),是可以直接被其他程序调用的一段代码

2.1 函数返回值分类

 2.1.1 无返回值
void FunctionName (args) 
{ 
    /** code **/
}
2.1.2 有返回值

return_type 为函数执行过后的返回值的类型,如int float bool等

<return_tpye> FunctionName (args) 
{ 
    /** code **/
}

2.2 函数调用

2.2.1 无返回值函数调用
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel.Design;
using UnityEngine;

public class FunctionDemo : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        int a = 1;
        float b = 1.2f;
        Func(a, b);
    }

    void Func(int arg1, float arg2)
    {
        Debug.Log("\n Func已运行");
    }
}

函数的调用在Start()中传入参数并调用函数即可,但是需要注意,传入参数的顺序必须与函数声明的传入参数顺序一致,否则报错:

2.2.2 有返回值函数调用
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel.Design;
using UnityEngine;

public class FunctionDemo : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        int c = 2;
        Debug.Log("\n c的值为: " + c);
        c = Double(c);
        Debug.Log("\n c的值为: " + c);
    }

    int Double(int arg)
    {
        Debug.Log("\n Double已运行");
        return arg * 2;
    }
}


声明:

1、欢迎各位读者通过讨论区或私信讨论,如有错误或疏漏恳请读者批评指正,共勉。

2、本文是作者个人的学习总结,转载请注明。

  • 22
    点赞
  • 28
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Unity中,可以使用UnityPlayer类中的UnitySendMessage方法来调用C#脚本中的方法。这个方法有三个参数:第一个参数是C#脚本附着的游戏物体的名称,第二个参数是C#脚本中的方法名,第三个参数是C#脚本方法的参数,如果没有参数则传入空字符串""。具体的代码如下所示: ```csharp public static void UnitySendMessage(String var0, String var1, String var2) { if (!o.c()) { com.unity3d.player.f.Log(5, "Native libraries not loaded - dropping message for " + var0 + "." + var1); } else { try { nativeUnitySendMessage(var0, var1, var2.getBytes("UTF-8")); } catch (UnsupportedEncodingException var3) { } } } ``` 引用<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [【Unity3D】Android Studio 工程中使用 Java 代码调用 UnityC# 脚本 ( Java 中调用 UnityPlayer#Unity...](https://blog.csdn.net/han1202012/article/details/127981676)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *3* [Unity 3D学习基础篇)——C#基础入门](https://blog.csdn.net/qq_43551910/article/details/122435361)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值