Up to date

This page is up to date for Godot 4.2. If you still find outdated information, please open an issue.

PackedColorArray

Color 紧缩数组。

描述

专门设计用于存放 Color 的数组。数据是紧密存放的,因此能够在数组较大时节省内存。

备注

通过 C# 使用这个 API 时有显著的不同。详见 C# API 与 GDScript 的差异

构造函数

PackedColorArray

PackedColorArray ( )

PackedColorArray

PackedColorArray ( PackedColorArray from )

PackedColorArray

PackedColorArray ( Array from )

方法

bool

append ( Color value )

void

append_array ( PackedColorArray array )

int

bsearch ( Color value, bool before=true )

void

clear ( )

int

count ( Color value ) const

PackedColorArray

duplicate ( )

void

fill ( Color value )

int

find ( Color value, int from=0 ) const

bool

has ( Color value ) const

int

insert ( int at_index, Color value )

bool

is_empty ( ) const

bool

push_back ( Color value )

void

remove_at ( int index )

int

resize ( int new_size )

void

reverse ( )

int

rfind ( Color value, int from=-1 ) const

void

set ( int index, Color value )

int

size ( ) const

PackedColorArray

slice ( int begin, int end=2147483647 ) const

void

sort ( )

PackedByteArray

to_byte_array ( ) const

操作符

bool

operator != ( PackedColorArray right )

PackedColorArray

operator + ( PackedColorArray right )

bool

operator == ( PackedColorArray right )

Color

operator [] ( int index )


构造函数说明

PackedColorArray PackedColorArray ( )

构造空的 PackedColorArray


PackedColorArray PackedColorArray ( PackedColorArray from )

构造给定 PackedColorArray 的副本。


PackedColorArray PackedColorArray ( Array from )

构造新的 PackedColorArray。你也可以传一个通用 Array 进行转换。

注意:使用元素初始化 PackedColorArray 时,必须使用元素为 ColorArray

var array = PackedColorArray([Color(0.1, 0.2, 0.3), Color(0.4, 0.5, 0.6)])

方法说明

bool append ( Color value )

向数组末尾追加一个元素(push_back 的别名)。


void append_array ( PackedColorArray array )

在该数组的末尾追加一个 PackedColorArray


int bsearch ( Color value, bool before=true )

使用二进法查找已有值的索引(如果该值尚未存在于数组中,则为保持排序顺序的插入索引)。传递 before 说明符是可选的。如果该参数为 false,则返回的索引位于数组中该值的所有已有的条目之后。

注意:在未排序的数组上调用 bsearch 会产生预料之外的行为。


void clear ( )

清空数组。相当于调用 resize 时指定大小为 0


int count ( Color value ) const

返回元素在数组中出现的次数。


PackedColorArray duplicate ( )

创建该数组的副本,并将该副本返回。


void fill ( Color value )

将数组中的所有元素都设为给定的值。通常与 resize 一起使用,创建给定大小的数组并初始化元素。


int find ( Color value, int from=0 ) const

在数组中搜索值并返回其索引,如果未找到则返回 -1 。可选地,可以传递起始搜索索引。


bool has ( Color value ) const

如果该数组包含 value,则返回 true


int insert ( int at_index, Color value )

在数组中给定的位置插入一个新元素。这个位置必须是有效的,或者是在数组的末端(idx == size())。


bool is_empty ( ) const

该数组为空时,返回 true


bool push_back ( Color value )

将一个值添加到数组中。


void remove_at ( int index )

从数组中删除位于索引的元素。


int resize ( int new_size )

设置数组的大小。如果数组被增大,则保留数组末端的元素。如果数组被缩小,则将数组截断到新的大小。调用一次 resize 并分配新值比逐个添加新元素要快。


void reverse ( )

将数组中的元素逆序排列。


int rfind ( Color value, int from=-1 ) const

逆序搜索数组。还可以传递起始搜索位置索引。如果为负,则起始索引被视为相对于数组的结尾。


void set ( int index, Color value )

更改给定索引处的 Color


int size ( ) const

返回数组中元素的个数。


PackedColorArray slice ( int begin, int end=2147483647 ) const

返回该 PackedColorArray 的切片,是从 begin(含)到 end(不含)的全新 PackedColorArray

beginend 的绝对值会按数组大小进行限制,所以 end 的默认值会切到数组大小为止(即 arr.slice(1)arr.slice(1, arr.size()) 的简写)。

如果 beginend 为负,则表示相对于数组的末尾(即 arr.slice(0, -2)arr.slice(0, arr.size() - 2) 的简写)。


void sort ( )

将该数组中的元素按升序排列。


PackedByteArray to_byte_array ( ) const

返回 PackedByteArray,每个颜色都被编码为字节。


操作符说明

bool operator != ( PackedColorArray right )

如果数组内容不同,则返回 true


PackedColorArray operator + ( PackedColorArray right )

返回新的 PackedColorArray,新数组的内容为此数组在末尾加上 right。为了提高性能,请考虑改用 append_array


bool operator == ( PackedColorArray right )

如果两个数组的内容相同,即对应索引号的 Color 相等,则返回 true


Color operator [] ( int index )

返回索引 index 处的Color。负数索引可以从末端开始访问元素。使用超出数组范围的索引将导致出错。