创造你的世界
玩家与角色:提示

更改ZEPETO角色颜色和穿戴物品颜色

7
本指南旨在帮助创建引人入胜的内容。 更改zepeto角色的颜色 zepeto角色在运行时作为zepetocharacter对象实例化。 如果你检查zepetocharacter的结构,你会发现一个名为zepeto context的子对象。在zepeto context内部,还有一个名为body的对象。 通过在运行时使用脚本替换body对象的body(clone)材质,你可以更改zepeto角色的颜色。 对于非动画头像,这种方法会改变整个面部和身体的颜色。 然而,对于动画头像,你还需要修改anime basemodel的body(clone)材质,它是body对象的子对象。 请注意,材质替换在相同长度的材质数组中是可行的。 📘 unity 材料 https //docs unity3d com/2020 3/documentation/manual/materials html https //docs unity3d com/2020 3/documentation/manual/materials html 以下是一个示例代码,用于更改本地玩家的身体颜色。 确保实现逻辑,以便角色的颜色仅在角色加载后更改。 import { zepetoscriptbehaviour } from 'zepeto script' import {localplayer, spawninfo, zepetocharacter, zepetocharactercreator, zepetoplayers} from "zepeto character controller"; import {zepetopropertyflag} from "zepeto"; import { gameobject, material, renderer, skinnedmeshrenderer, transform, waitforseconds } from 'unityengine'; import { button } from 'unityengine ui'; export default class changeskincolor extends zepetoscriptbehaviour { public newcolormaterial material; public changecolorbutton button; public originalcolorbutton button; private originalcolormaterial material; private originalanimematerials material\[]; private animerend renderer; private bodyrend renderer; private bodyrends renderer\[]; private localcharacter zepetocharacter; start() { // 找到本地玩家并将其设置为 localcharacter zepetoplayers instance onaddedlocalplayer addlistener(() => { this localcharacter = zepetoplayers instance localplayer zepetoplayer character; // 找到本地玩家的 zepeto 上下文的材质 this bodyrend= this localcharacter context getcomponentinchildren\<skinnedmeshrenderer>(); // 存储原始材质 this originalcolormaterial = this bodyrend material; // 确定是否为动画头像并保存相关信息 this bodyrends= this localcharacter getcomponentsinchildren\<skinnedmeshrenderer>(); this bodyrends foreach((currentrenderer) =>{ if(currentrenderer name includes("anime basemodel")){ this animerend = currentrenderer; this originalanimematerials = this animerend sharedmaterials; } }); }); // 按下按钮时替换为预设材质 this changecolorbutton onclick addlistener(() => { if(this localcharacter != null) { this bodyrend material = this newcolormaterial; if(this animerend != null) { let tempmaterials material\[] = \[this animerend sharedmaterials\[0],this animerend sharedmaterials\[1],this newcolormaterial,this animerend sharedmaterials\[3]]; this animerend sharedmaterials = tempmaterials; } } }); // 按下按钮时返回原始材质 this originalcolorbutton onclick addlistener(() => { if(this localcharacter != null) { this bodyrend material = this originalcolormaterial; if(this animerend != null) { this animerend sharedmaterials = this originalanimematerials; } } }); } } 更改身体材料可能导致zepeto头像的妆容无法正确显示。 更改磨损物品的颜色 磨损物品作为身体对象下的子对象实例化。 您可以从物品对象的材料部分检查每个物品应用的材料。 通过在运行时使用脚本替换材料,您可以更改物品的颜色。 下面是一个示例代码,用于更改本地玩家所穿戴的第一个物品的颜色。 确保在角色加载后才更改物品的颜色。 import { zepetoscriptbehaviour } from 'zepeto script' import {localplayer, zepetocharacter, zepetoplayers} from "zepeto character controller"; import { gameobject, material, renderer, skinnedmeshrenderer, transform, waitforseconds } from 'unityengine'; import { button } from 'unityengine ui'; export default class changeskincolor extends zepetoscriptbehaviour { public newcolormaterial material; public changecolorbutton button; public originalcolorbutton button; private originalcolormaterial material; private body gameobject; private itemrend renderer; private localcharacter zepetocharacter; start() { // 找到本地玩家并将其设置为 localcharacter zepetoplayers instance onaddedlocalplayer addlistener(() => { this localcharacter = zepetoplayers instance localplayer zepetoplayer character; // 访问本地玩家的 zepeto 上下文的身体 this body = this localcharacter context getcomponentinchildren\<skinnedmeshrenderer>() gameobject; // 访问身体第一个子物品的材质 this itemrend = this body transform getchild(0) getcomponent\<skinnedmeshrenderer>(); // 存储原始材质 this originalcolormaterial = this itemrend material; }); // 当按钮被按下时替换为预设材质 this changecolorbutton onclick addlistener(() => { if(this localcharacter != null) { this itemrend material = this newcolormaterial; } }); // 当按钮被按下时返回原始材质 this originalcolorbutton onclick addlistener(() => { if(this localcharacter != null) { this itemrend material = this originalcolormaterial; } }); } } 代码解释 this body transform getchild(0) 指的是所穿戴物品中的第一个物品,即索引为 0 的物品。 您可以将其调整为更改其他穿戴物品的颜色。 对于使用多个材质的物品,请记住材质替换需要相同长度的材质数组。 更改zepeto角色和所有物品的颜色 通过利用我们迄今为止获得的见解,您可以统一修改角色和所有物品的颜色。 这是一个示例代码,当点击“更改颜色”按钮时,会完全改变本地玩家角色和物品的颜色,当点击“原始颜色”按钮时,会恢复到原始颜色: import { zepetoscriptbehaviour } from 'zepeto script' import {localplayer, zepetocharacter, zepetoplayers} from "zepeto character controller"; import { gameobject, material, renderer, skinnedmeshrenderer, transform, waitforseconds } from 'unityengine'; import { button } from 'unityengine ui'; export default class changeallmaterial extends zepetoscriptbehaviour { public newcolormaterial material; public changecolorbutton button; public originalcolorbutton button; private originalmaterials material\[] = new array(); private bodyrends renderer\[]; private localcharacter zepetocharacter; start() { // 找到本地玩家并将其设置为 localcharacter zepetoplayers instance onaddedlocalplayer addlistener(() => { this localcharacter = zepetoplayers instance localplayer zepetoplayer character; // 保存以保持原始材料 this bodyrends= this localcharacter getcomponentsinchildren\<skinnedmeshrenderer>(); this bodyrends foreach((currentrenderer) =>{ for(let i=0; i\<currentrenderer sharedmaterials length;i++){ this originalmaterials push(currentrenderer sharedmaterials\[i]); } }); }); // 当按钮被按下时用预设材料替换 this changecolorbutton onclick addlistener(() => { if(this localcharacter != null) { this bodyrends foreach((currentrenderer) =>{ let tempmaterials material\[] = new array(); for(let i=0; i\<currentrenderer sharedmaterials length;i++){ tempmaterials push(this newcolormaterial); } currentrenderer sharedmaterials = tempmaterials; }); } }); // 当按钮被按下时返回到原始材料 this originalcolorbutton onclick addlistener(() => { if(this localcharacter != null) { let indexnum = 0; this bodyrends= this localcharacter getcomponentsinchildren\<skinnedmeshrenderer>(); this bodyrends foreach((currentrenderer) =>{ let tempmaterials material\[] = new array(); for(let i=0; i\<currentrenderer sharedmaterials length;i++){ tempmaterials push(this originalmaterials\[indexnum]); indexnum++; } currentrenderer sharedmaterials = tempmaterials; }); } }); } } 这种方法不仅适用于本地玩家,还适用于运行时创建的任何zepeto角色,包括npc角色。 尽情发挥创意,以有趣的方式应用它!