How to create a square using the graphics component through the editor?

:upside_down_face: Is it possible to draw a square and see it through the editor?

I don’t understand how to create a square using the graphics component through the editor? I added a graphics component to the empty node line width = 20 did, line cap = square did miter limit = 20 did. fill color = DF0000 but I see a square in the editor

It is possible, but you need write a new component.


Sorry. I forgot to add a screenshot :melting_face:

I added a graphical component but it doesn’t draw anything. what am I doing wrong?

Nothing wrong. If you wnt to draw something in graphics you must write code.

https://docs.cocos.com/creator/manual/en/ui-system/components/editor/graphics.html

1 Like

no way without code. -Did I understand correctly?

just the component settings in the editor, they say edit me and you will see the changes. :upside_down_face: :melting_face:

Yes. It’s a very low-level drawing tool. Use it with editor only limits its capabilities. But if you really need you can encapsulating a component to use it in editor. Like this

import { _decorator, CCFloat, Component, Graphics, Node } from 'cc';
const { ccclass, property , executeInEditMode, requireComponent} = _decorator;

@ccclass('GraphicsTest')
@executeInEditMode
@requireComponent(Graphics)
export class GraphicsTest extends Component {
    @property(CCFloat)
    private _radis: number = 20;

    @property(CCFloat)
    public get radius(){
        return this._radis;
    }

    public set radius(r: number){
        this._radis = r;
        this.draw();
    }


    draw(){
        let g = this.getComponent(Graphics);
        g.clear();
        g.circle(0, 0, this._radis);
        g.stroke();
    }

    start() {
        this.draw();
    }
}


1 Like