Home > Blockchain >  How to get string from .txt file and fill it to Label String in Cocos Creator
How to get string from .txt file and fill it to Label String in Cocos Creator

Time:09-11

I want to fill Label String with text from sometext.txt file in Cocos Creator.

I did this:

@property({type:Node})
cptxt = null;
.
.
.
this.copyrightText = jsb.fileUtils.getStringFromFile('Text/copyright.txt');
this.cptxt.string = this.copyrightText;

I filled cptxt with the label on the Scene. But it doesn't works. The Label String is not filled. I run on Android

CodePudding user response:

you need to pass the path of the text file in "jsb.fileUtils.getStringFromFile()" method, you can get the path of the text file by loading the text asset using "resources.load()" then using ".nativeUrl" property

P.S. to use the "resources.load()" you have to create a folder named "resources" under assets folder in your project directory then under "resources" you can create your sub folder "Text" and in that folder put your copyright.txt file

also type of cptxt property should be "Label" if you are trying to get the Label component's string property directly

import {. . ., Label} from 'cc';

 @property({type:Label})
    cptxt: Label = null;
    .
    .
    .
resources.load("Text/copyright", (err, textAsset) => {
    this.copyrightText = jsb.fileUtils.getStringFromFile(textAsset.nativeUrl);
    this.cptxt.string = this.copyrightText;
    });
  • Related