Skip to content
MaterialSlot

Gameplay / MaterialSlot

MaterialSlot Class

Model slot, perform material related operations


  • model Model referenced by material slot

  • index Index of material slot

  • materialAssetID Asset used by material slot

  • color Color of material slot

  • resetColor Method to reset the material slot color

  • resetMaterial Method Reset material asset

  • getCustomMaterial Method to obtain customize material

Usage example: create a script named "ModelExample", place it in the object bar, open the script, enter the following code to save, run the game, and you can use F1 key to dynamically generate a model in the scene and simulate physics. The code is as follows:

ts
@Component
export default class ModelExample extends Script {
    //When the script is instanced, this function will be call before the first frame update
    protected async onStart(): Promise<void> {
        if(SystemUtil.isClient())
            {
                InputUtil.onKeyDown(Keys.F1,()=>{
                    // F1 key notifies the server to execute an event
                    mw.Event.dispatchToServer("Model");
                });
            }
        if(SystemUtil.isServer()){
            mw.Event.addClientListener("Model",()=>{
                let box = GameObject.spawn("197386",{
                    transform:new Transform(new Vector(500,0,100),new Rotation(0,0,0),new Vector(1,1,1)),
                    replicates:true
                }) as Model;
                // Set transparency
                box.opacity = 0.8;
                // Set color
                box.color = new mw.LinearColor(1.0,0.0,0.0,0.0);

                // Get the 0 th slot
                let matSlot0 = box.getMaterialSlot(0);
                // Set material slot color
                matSlot0.color = new mw.LinearColor(1.0,0.0,0.0,0.0);
                // Reset color
                matSlot0.resetColor();
                // Set material slot material asset
                matSlot0.materialAssetID = "128569";
                // Reset slot material asset
                matSlot0.resetMaterial();
                // Get slot customize material
                let customMat = matSlot0.getCustomMaterial();
            });
        }
    }
}
@Component
export default class ModelExample extends Script {
    //When the script is instanced, this function will be call before the first frame update
    protected async onStart(): Promise<void> {
        if(SystemUtil.isClient())
            {
                InputUtil.onKeyDown(Keys.F1,()=>{
                    // F1 key notifies the server to execute an event
                    mw.Event.dispatchToServer("Model");
                });
            }
        if(SystemUtil.isServer()){
            mw.Event.addClientListener("Model",()=>{
                let box = GameObject.spawn("197386",{
                    transform:new Transform(new Vector(500,0,100),new Rotation(0,0,0),new Vector(1,1,1)),
                    replicates:true
                }) as Model;
                // Set transparency
                box.opacity = 0.8;
                // Set color
                box.color = new mw.LinearColor(1.0,0.0,0.0,0.0);

                // Get the 0 th slot
                let matSlot0 = box.getMaterialSlot(0);
                // Set material slot color
                matSlot0.color = new mw.LinearColor(1.0,0.0,0.0,0.0);
                // Reset color
                matSlot0.resetColor();
                // Set material slot material asset
                matSlot0.materialAssetID = "128569";
                // Reset slot material asset
                matSlot0.resetMaterial();
                // Get slot customize material
                let customMat = matSlot0.getCustomMaterial();
            });
        }
    }
}

Table of contents

Accessors

color(): LinearColor other
The color of the slot
index(): number other
Index of slot, read-only
materialAssetID(): string other
Asset ID of the slot
model(): Model other
Slot model, read-only

Methods

getCustomMaterial(): MaterialInstance other
Get customize material
resetColor(): void other
Reset slot Color
resetMaterial(): void

Parameters

indexnumber
modelModel

Accessors

color

get color(): LinearColor other

set color(value): void other

The color of the slot

Returns

LinearColorGet slot color

Set the color of the slot

Parameters

value LinearColorSlot color

index

get index(): number other

Index of slot, read-only

Returns

numberGet the index of the slot

materialAssetID

get materialAssetID(): string other

set materialAssetID(value): void other

Asset ID of the slot

Returns

stringGet slot Asset ID

Set asset of slot

Parameters

value stringSlot Asset ID

model

get model(): Model other

Slot model, read-only

Returns

ModelGet the model of slot

Methods

getCustomMaterial

getCustomMaterial(): MaterialInstance other

Get customize material

Returns

MaterialInstanceReturn custom material instance

Network material will return null


resetColor

resetColor(): void other

Reset slot Color


resetMaterial

resetMaterial(): void