塔防游戏 Day3

1. 添加按钮动画

  选择 Button->Transition 为 Animation ,然后自定义四种状态动画即可。

  

 

2. 控制升级面板的显示和隐藏

 1 //  升级处理
 2 // 若点击同一炮塔,并且面板已显示
 3 if (mapCube.turretGo == selectedTurret && upgradeCanvas.activeInHierarchy)
 4 {
 5     HideUpgradeUI();                // 隐藏面板
 6 }
 7 else     // 否则显示面部
 8 {
 9     ShowUpgradeUI(mapCube.transform.position, mapCube.isUpgraded);
10 }
11 selectedTurret = mapCube.turretGo;
 1 // 显示升级面板
 2 void ShowUpgradeUI(Vector3 pos, bool isDisableUpgrade)
 3 {
 4     upgradeCanvas.SetActive(true);                      // 显示面板
 5     upgradeCanvas.transform.position = pos;             // 放在合适的位置
 6     upgradeButton.interactable = !isDisableUpgrade;     // 升级按钮是否可用
 7 }
 8 
 9 // 隐藏升级面板
10 void HideUpgradeUI()
11 {
12     upgradeCanvas.SetActive(false);
13 }

 

 

 3. 给升级面板添加动画

 1 // 显示升级面板
 2 void ShowUpgradeUI(Vector3 pos, bool isDisableUpgrade)
 3 {
 4     StopCoroutine("HideUpgradeUI()");                   // 开始协程
 5     upgradeCanvas.SetActive(false);
 6     upgradeCanvas.SetActive(true);                      // 显示面板
 7     upgradeCanvas.transform.position = pos;             // 放在合适的位置
 8     upgradeButton.interactable = !isDisableUpgrade;     // 升级按钮是否可用
 9 }
10 
11 // 隐藏升级面板
12 IEnumerator HideUpgradeUI()
13 {
14     upgradeCanvasAnimator.SetTrigger("Hide");
15     yield return new WaitForSeconds(0.5f);
16     upgradeCanvas.SetActive(false);
17 }

 

 4. 控制升级和拆毁炮台

 1 // 点击按钮触发函数
 2 public void onUpgradeButtonDown()
 3 {
 4     selectedMapCube.UpdateTurret();
 5     StartCoroutine(HideUpgradeUI());
 6 }
 7 
 8 public void onDestroyButtonDown()
 9 {
10     selectedMapCube.DestroyTurret();
11     StartCoroutine(HideUpgradeUI());
12 }
 1 // 升级炮塔
 2 public void UpdateTurret()
 3 {
 4     if (isUpgraded) return;
 5     Destroy(turretGo);
 6     turretGo = GameObject.Instantiate(turretData.turretUpgradedPrefab, transform.position, Quaternion.identity);
 7     isUpgraded = true;
 8     // 产生特效
 9     GameObject effect = GameObject.Instantiate(buildEffect, transform.position, Quaternion.identity);
10     Destroy(effect, 1.5f);                     // 1.5秒后销毁特效
11 }
12 
13 // 拆毁炮台
14 public void DestroyTurret()
15 {
16     Destroy(turretGo);
17     isUpgraded = false;             // 初始化
18     turretGo = null;
19     turretData = null;
20     // 产生特效
21     GameObject effect = GameObject.Instantiate(buildEffect, transform.position, Quaternion.identity);
22     Destroy(effect, 1.5f);                     // 1.5秒后销毁特效
23 }

 

 

 5. 添加激光炮塔

   添加 LineRenderer 组件。

   

if (enemys.Count > 0)
{
    // 设置激光的开始位置和结尾位置
    laserRenderer.SetPositions(new Vector3[] { firePosition.position, enemys[0].transform.position });
}

 

4. 游戏失败情况处理

1 // 游戏失败
2 public void fail()
3 {
4     MessageText.text = "失 败";
5     endUI.SetActive(true);
6     enemySpawner.Stop();            // 停止生成敌人
7 }
1 public void Stop()
2 {
3     StopCoroutine(coroutine);                   // 停止协程
4 }

 

 

5. 菜单设计

 1 // 点击开始游戏按钮
 2 public void OnButtonStart()
 3 {
 4     SceneManager.LoadScene(1);
 5 }
 6 
 7 // 点击退出按钮
 8 public void OnButtonExit()
 9 {
10 #if UNITY_EDITOR
11     UnityEditor.EditorApplication.isPlaying = false;
12 #else
13     Application.Quit();      
14 #endif
15 }

 

 

完!!!

 

posted @ 2018-03-16 19:17  Just_for_Myself  阅读(476)  评论(0编辑  收藏  举报