加载游戏场景##
加载游戏场景的处理
代码展示:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class LoadingScene : MonoBehaviour {
public Image progressImage;
public Text labelText;
private AsyncOperation async;
private int curProgressValue;
void Start () {
StartCoroutine(LoadScene());
}
IEnumerator LoadScene()
{
//跳转场景有个问题,当加载到90%的时候就会自动跳转到下一个场景,这样就不协调。
//我们要进行人为的控制
async=SceneManager.LoadSceneAsync(1);
async.allowSceneActivation = false;//设置完加载(90%)后不能自己跳转场景
yield return async;
}
void Update () {
if(async==null)
{
return;
}
int progressValue = 0;
if (async.progress < 0.9f)
{
progressValue = (int)async.progress;
}
else
{
progressValue = 100;
}
if (curProgressValue<progressValue)
{
curProgressValue++;
}
labelText.text = curProgressValue + "%";
progressImage.fillAmount = curProgressValue / 100f;
if (curProgressValue==100)
{
async.allowSceneActivation = true;
}
}
}
开发技能图片冷却##
效果展示
图层显示
通过点击鼠标实现:
public class CDSkill : MonoBehaviour
{
public Button mSkillBtn;
public Image mMaskimg;
public Text mCDTest;
int count;
const float MaxCODETime = 3;
float curTime;
void Start()
{
EndSkill();
mSkillBtn.onClick.AddListener(OnClickSkillBtnEvent);
//StartSkill();
}
void Update()
{
//count++;
//mSkillBtn.onClick.AddListener(OnClickSkillBtnEvent);
//mSkillBtn.onClick.AddListener(p1);
//mSkillBtn.onClick.AddListener(p2);
if (mSkillBtn.interactable==false)
{
if (mMaskimg.fillAmount<=1f&&mMaskimg.fillAmount>0f)
{
curTime += Time.deltaTime;
mMaskimg.fillAmount = (MaxCODETime - curTime) / MaxCODETime;
mCDTest.text = Mathf.CeilToInt(MaxCODETime-curTime).ToString();
if (mMaskimg.fillAmount==0f)
{
EndSkill();
}
}
}
}
void p1()
{
print(11111);
}
void p2()
{
print(count);
}
void OnClickSkillBtnEvent()
{
StartSkill();
}
void StartSkill()
{
mSkillBtn.interactable = false;
mMaskimg.fillAmount = 1f;
mCDTest.text = MaxCODETime.ToString();
curTime = 0f;
}
void EndSkill()
{
mSkillBtn.interactable = true;
mMaskimg.fillAmount = 0f;
//mCDTest.text = null;
mCDTest.text = string.Empty;
curTime = 0f;
}
}
通过按键实现##
按键实现
布局展示
public class KeySkill : MonoBehaviour {
public Button keyButton;
public Image keyImage;
public Text CDtext;
float CodingTime = 3f;
bool isCoding = false;
float curTime;
void Start () {
keyImage.gameObject.SetActive(false);
CDtext.gameObject.SetActive(false);
}
void Update () {
if (Input.GetKeyDown(KeyCode.A)&& !isCoding)
{
isCoding = true;
keyImage.gameObject.SetActive(true);
CDtext.text = CodingTime.ToString();
keyImage.fillAmount = 1;
CDtext.gameObject.SetActive(true);
curTime = CodingTime;
//curTime += Time.deltaTime;
//CDtext.text = Mathf.CeilToInt((CodingTime - curTime)).ToString();
}
if (isCoding==true)
{
curTime -= Time.deltaTime;
CDtext.text = ((int)curTime).ToString();
keyImage.fillAmount = curTime / CodingTime;
if (curTime<=0.05f)
{
CDtext.gameObject.SetActive(false);
keyImage.gameObject.SetActive(false);
isCoding = false;
}
//keyImage.fillAmount -= (1f / CodingTime) * Time.deltaTime;
//if (keyImage.fillAmount<=0.05f)
//{
// isCoding = false;
// keyImage.fillAmount = 0;
//}
}
}
}
通过点击和按键实现相同效果,两种不同的方法,对比分析. mSkillBtn.onClick.AddListener(OnClickSkillBtnEvent);放在Start方法里面。########
网友评论