您给我的好像不是单例模板(泛型单例),我参照官方写了一个,但是NetMgr.Instance().; //这里无法调用到NetMgr.ts中的方法Hello()。这是为什么?
代码如下:
import { _decorator, Component, Node } from “cc”;
const { ccclass, property } = _decorator;
@ccclass(“SingletonPattern”)
export default class SingletonPattern {
private static instance:any=null;
public static Instance(type:{new ():T}){
if(this.instance==null){
this.instance=new type();
}
return this.instance;
}
}
继承自单例模板:
import { _decorator, Component, Node } from “cc”;
import SingletonPattern from “./Help/SingletonPattern”;
const { ccclass, property } = _decorator;
//这个脚本用于对网络服务的管理
@ccclass(“NetMgr”)
export class NetMgr extends SingletonPattern< NetMgr> {
//可能会单独把微信开放域相关的代码放在另一个脚本
//而这个脚本则是对这个进行管理
public Log()
{
console.log("单例模板成功使用");
}
}
调用单例子类的方法:
import { _decorator, Component, Node } from “cc”;
const { ccclass, property } = _decorator;
import{ UIMgr } from “./UIMgr”;
import{NetMgr} from “./NetMgr”;
//这个脚本负责游戏逻辑
@ccclass(“GameMgr”)
export class GameMgr extends Component {
//游戏初始化、场景跳转
//游戏结算等
//使用中介者模式
@property
public isCross:boolean;
@property
public Score=0;//得分
/**
* 计算得分
*/
public CalScore() {
//是否穿过平台
if (this.isCross = true) {
this.Score += 1;
}
}
/**
* 游戏结束
*/
public GameOver() {
//结算奖励
//返回主界面
NetMgr.Instance().; //这里无法调用。
UIMgr.Instance().SetAcitve();
console.log("游戏结束");
}
}










或者能推荐一下博客之类的。。。