创造你的世界
脚本编写
游戏对象和组件
13min
在这个世界上,创建对象或修改或添加特定组件到对象是非常常见的。 zepetoscript 支持与 unity 脚本相同的接口。 gameobject 和组件的基本示例 创建和移除游戏对象: gameobject // gameobject 创建 const tempobj = new gameobject(); const obj = object instantiate(tempobj); // gameobject 销毁 object destroy(obj); 要获取 gameobjects 组件或添加新的组件,请使用 getcomponent 或 addcomponent。 component // 使用泛型的 getcomponent const mytransform = this getcomponent\<transform>(); // 使用泛型的 addcomponent const animator = this gameobject addcomponent\<animator>(); 查看 gameobject 组件的完整代码示例。 这是一个示例,涵盖了 gameobject 的创建和属性修改、组件的添加以及销毁。 gameobjectsample import { gameobject, object, transform, animator, vector3, waitforseconds } from 'unityengine'; import { zepetoscriptbehaviour } from 'zepeto script'; export default class gameobjectsample extends zepetoscriptbehaviour { private testobject gameobject; start() { // 创建一个空的 gameobject const tempobj = new gameobject(); this testobject = object instantiate(tempobj) as gameobject; // 改变对象的名称 this testobject name = "testobject"; // 改变对象的变换信息 this testobject transform position = new vector3(3,3,3); // 将组件附加到对象 this testobject gameobject addcomponent\<animator>(); // 使用 getcomponent 的示例 const animator = this testobject getcomponent\<animator>(); if(animator != null) { console log("getcomponent 成功"); } // 使用协程在 5 秒后销毁对象 this startcoroutine(this destroyobject()); } destroyobject() { yield new waitforseconds(5); // 销毁 gameobject object destroy(this testobject); } } 利用查找 find 相关方法在 zepetoscript 中也支持与 unity 脚本相同的接口风格。 方法 描述 gameobject find() 它根据当前场景中的名称查找并返回一个活动的 gameobject 对象。 \ 如果未找到,则返回 null。 gameobject findgameobjectwithtag() 它根据当前场景中的标签查找并返回一个活动的 gameobject 对象。 \ 在活动对象中查找并返回第一个匹配指定标签的对象,如果未找到则返回 null。 gameobject findgameobjectswithtag() 查找当前场景中具有特定标签的所有活动 gameobject 对象,并将它们作为数组返回。 \ 如果未找到,则返回空数组。 例如,设置场景如下: 添加多个 3d 对象,并将它们的所有标签指定为 3d。 创建一个空对象以附加脚本,并将其重命名为 findsample。 typescript import { zepetoscriptbehaviour } from 'zepeto script'; import { gameobject, transform } from 'unityengine'; export default class findsample extends zepetoscriptbehaviour { public tempobj1 gameobject; public tempobj2 gameobject; public tempobj3 gameobject\[]; start() { // 查找具有指定名称的目标 gameobject。 // 如果多个 gameobject 具有相同的名称,将返回找到的第一个。 this tempobj1 = gameobject find("cube"); if(this tempobj1 != null) { console log("gameobject 查找成功"); } // 查找具有指定标签的目标 gameobject。 // 如果多个 gameobject 具有相同的标签,将返回找到的第一个。 this tempobj2 = gameobject findgameobjectwithtag("3d"); if(this tempobj2 != null) { console log("findgameobjectwithtag 成功"); } // 返回具有指定标签的 gameobject 数组。 this tempobj3 = gameobject findgameobjectswithtag("3d"); if(this tempobj3 != null) { console log("findgameobjectswithtag 成功"); } } } 当您在 findsample 对象中添加脚本时,检查器是空的。 按下播放按钮以运行它,您可以在检查器窗口中确认每个对象已被分配,并通过控制台窗口检查查找对象的成功。 创建预制件 让我们探索如何在运行时创建预制件。 首先,将一个 3d 对象 > 立方体 添加到场景中。 在立方体的检查器中按下添加组件,并添加刚体。 然后将立方体拖到项目区域以使其成为预制件。 由于我们将使用预制件,请删除场景中的立方体。 请编写以下脚本。 typescript import { gameobject, object, vector3, waitforseconds } from 'unityengine'; import { zepetoscriptbehaviour } from 'zepeto script'; export default class clonesample extends zepetoscriptbehaviour { public cloneprefab gameobject; start() { this startcoroutine(this doroutine()); } doroutine() { while(true) { yield null; // create a clone const clone = object instantiate(this cloneprefab) as gameobject; // set the height of the clone object clone transform position = new vector3(0, 10, 0); // destroy it after 5 seconds of creation gameobject destroy(clone, 5); yield new waitforseconds(1); } } } 然后返回到unity编辑器,将预制件拖到脚本检查器中以添加它。 按下播放按钮以检查每秒创建一个3d对象并从上方掉落。 点击下面的链接以了解zepetoscript提供的unity gameobjects和组件。 📘 创建gameobjects https //docs unity3d com/2020 3/documentation/scriptreference/object instantiate html https //docs unity3d com/2020 3/documentation/scriptreference/object instantiate html 📘 销毁gameobjects https //docs unity3d com/2020 3/documentation/scriptreference/object destroy html https //docs unity3d com/2020 3/documentation/scriptreference/object destroy html 📘 使用组件 https //docs unity3d com/kr/current/manual/usingcomponents html https //docs unity3d com/kr/current/manual/usingcomponents html