あなたの世界を作りなさい
プレイヤーとキャラクター: ヒント
ZEPETOキャラクターの色と着用アイテムの色の変更
7 分
このガイドは、魅力的なコンテンツを作成する手助けを目的としています。 zepetoキャラクターの色を変更する zepetoキャラクターは、実行時にzepetocharacterオブジェクトとしてインスタンス化されます。 zepetocharacterの構造を調べると、zepeto contextというサブオブジェクトが見つかります。zepeto contextの中には、bodyという別のオブジェクトがあります。 スクリプトを使用して実行時にbodyオブジェクトのbody(clone)マテリアルを置き換えることで、zepetoキャラクターの色を変更できます。 アニメーションされていないアバターの場合、このアプローチは顔全体と体の色を変更します。 ただし、アニメーションされたアバターの場合、bodyオブジェクトの子であるanime basemodelのbody(clone)マテリアルも変更する必要があります。 マテリアルの置き換えは、同じ長さのマテリアル配列で可能であることに注意してください。 📘 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キャラクターを含むにも適用できます。 自由に創造的になり、楽しい方法で適用してください!