效果图
点击计时->再关闭面板->再次点击计时->发现时间继续上次的计算
在PopupPanelController的脚本中重写其基类的OnEnter和Onexit方法
PopupPanelController.cs:
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using DG.Tweening;
public class PopupPanelController : NBasePanel {
private Text text;
private float startTime;
private float curTime;
private float exitTime = 0;
private void Awake()
{
text = transform.Find("Image/Text").GetComponent<Text>();
}
void Start () {
}
void Update () {
curTime = Time.time - startTime+ exitTime;
text.text = "time:" +( curTime);
}
public void Close()
{
NUIManager.GetInstance().PopPanel();
}
public override void OnEnter()
{
base.OnEnter();
startTime = Time.time;
canvas.alpha = 0;
canvas.DOFade(1, 0.2f);
}
public override void OnExit()
{
base.OnExit();
exitTime = curTime;
canvas.alpha = 1;
canvas.DOFade(0, 0.2f);
}
}
这其中有几个注意点:
第一:你在重写基类的方法的时候,被重写的基类的函数要变成虚函数,即加一个virtual
第二:重写方法的时候前面要加一个override,并且在方法里面,如果你仍然要使用基类里面的你重写那个方法里面的功能,那么注意调用,调用的时候用的是base.xxx()方法。