UnigineEditor
Interface Overview
Assets Workflow
Settings and Preferences
Adjusting Node Parameters
Setting Up Materials
Setting Up Properties
Landscape Tool
Using Editor Tools for Specific Tasks
FAQ
编程
Fundamentals
Setting Up Development Environment
Usage Examples
C++
C#
UUSL (Unified UNIGINE Shader Language)
File Formats
Rebuilding the Engine and Tools
GUI
Double Precision Coordinates
应用程序接口
Containers
Common Functionality
Controls-Related Classes
Engine-Related Classes
Filesystem Functionality
GUI-Related Classes
Math Functionality
Node-Related Classes
Objects-Related Classes
Networking Functionality
Pathfinding-Related Classes
Physics-Related Classes
Plugins-Related Classes
CIGI Client Plugin
Rendering-Related Classes

Foreachkey语句

foreachkey架构提供另一种方法对矢量贴图进行迭代。通过这种语句为每个索引或所给容器内的键执行某些代码。

语法#

源代码 (UnigineScript)
foreachkey(key_variable; map; loop_increment) {
	// some_code;
 }

部分#

  • key_variable为当前间(或索引)。内部容器的游标会移至下一个键值对(或者索引项)所在处。
  • key_variable的类型无关紧要。
  • 每个迭代的最后执行loop_increment,此为可选选项。

示例#

  • 源代码 (UnigineScript)
    int map[] = ( "begin" : "flower", "middle" : "fruit", "end" : "tree" );
    foreachkey(int i; map) {
    	log.message("%s\t=>\t%s\n",i,map[i]);
    }
    结果为:
    输出
    begin   =>      flower
    end     =>      tree
    middle  =>      fruit
  • 源代码 (UnigineScript)
    int map[] = ( "begin" : "flower", "middle" : "fruit", "end" : "tree" );
    foreachkey(int i, j = 0; map; j++) {
    	log.message("%d: %s\t=>\t%s\n",j,i,map[i]);
    }
    结果为:
    输出
    0: begin   =>      flower
    1: end     =>      tree
    2: middle  =>      fruit
注意
在执行之前,以升序的方式将键存储起来。

foreachkey模块内部键的更改不会影响到贴图,但可直接更改值。

源代码 (UnigineScript)
int map[] = ( "begin" : "flower", "middle" : "fruit", "end" : "tree" );
foreachkey(int i; map) {
	i = "first";	// 贴图不受影响
	map[i] = -9.6;	// 贴图发生变化
}

嵌套foreachkey#

如果在另一个foreachkey结构内调用了foreachkey 结构,对于每个模块而言应将每个贴图的副本存储起来。(这种方法需要的性能更高。)

源代码 (UnigineScript)
int map[] = ( "1" : "head", "2" : "foot" );

int copy[];
copy.copy(map);

foreachkey(int i; map) {
	foreachkey(int j; copy)	// foreachkey(int j; map)会生成一个错误
	{
		log.message("%s\t->\t%s\t%s\t->\t%s\n",i,map[i],j,map[j]);
	}
}
此示例的输出结果为:
输出
1	->	head	1	->	head
1	->	head    2	->	foot
2	->	foot	1	->	head
2	->	foot	2	->	foot
最新更新: 2017-07-03