cocos / cocos-engine

Cocos simplifies game creation and distribution with Cocos Creator, a free, open-source, cross-platform game engine. Empowering millions of developers to create high-performance, engaging 2D/3D games and instant web entertainment.
https://www.cocos.com/en/creator
Other
7.09k stars 1.82k forks source link

Mark component dirty in editor #17034

Open ze0nni opened 3 months ago

ze0nni commented 3 months ago

Use Case

I am create component, who provides access to other compoent data.

Like this:

class MyBytton {
    @property(MyText)
    private _text: MyText;

    @property(CCString)
    get string(): string { retunr this._text.string; }
    set string(value: string) { this._text.string = value; }
}

Problem Description

This is work good in play mode.

I have problem in edit mode: Then I change MyBytton.string in editor inspector, I see how text changed in MyText. But then I save prefab -- MyText нет saved. I understand that I need to mark the component as "dirty" but I couldn't find how.

I saw there is a flag CCObject.Flags.Dirty and use it like this:

this._text.string = value;
this._text.node._objFlags |= CCObject.Flags.Dirty;
this._text._objFlags |= CCObject.Flags.Dirty;

But no result

Proposed Solution

No response

How it works

No response

Alternatives Considered

Avoid to complex components?

Additional Information

No response

wuzhiming commented 3 months ago

When you change MyBytton.string and focus to another place editor will mark as dirty and the title will have change to xx.scene*

but if you want to know your text change event at inspector, you can use getter and setter as you provided and just add @executeInEditMode decorator

//add this decorator and this component will run on editor and you can use you setter and getter when on editor mode
@executeInEditMode
export class testComponent extends Component {
@property(MyText)
    private _text: MyText;

    @property(CCString)
    get string(): string { retunr this._text.string; }
    set string(value: string) { this._text.string = value; }
}
ze0nni commented 3 months ago

Tried create minimal project to reproduct bug. But No problem not reproduced.

I think the problem is in my project. This is what it looks like: https://www.youtube.com/watch?v=vJX2dJir9M8

I will research problem more

wuzhiming commented 3 months ago

or you can upload a simple demo and we will try this https://www.youtube.com/watch?v=vJX2dJir9M8

ze0nni commented 3 months ago

Success. This happens if you use prefabs

CocosTest.zip