Related Posts Plugin for WordPress, Blogger...

5-11 Play Scale Animation When Getting Item

今天要介紹大家一個簡單的功能,就是當玩家獲取物品時,背包內的Item會有簡單的縮放動畫,這個設計是讓玩家能較清楚的看出取得物品的變化。

如下圖,剛取得物品的時候會較大。

然後物品會再慢慢縮減到正常大小。

這個功能不難,我們主要修改ItemUI.cs:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
 
namespace RPG.Inventory{
 public class ItemUI : MonoBehaviour{
  public Item Item { get; set; }
  public int Amount { get; set; }
 
  [SerializeField] float smoothScaleMultiplier = 4f;
  float targetScale = 1f;
  Vector3 initialScale = new Vector3(1.4f, 1.4f, 1.4f);
 
  void Update(){
   // 獲取物品動畫
   if (transform.localScale.x != targetScale) {
    float scale = Mathf.Lerp (transform.localScale.x, targetScale, smoothScaleMultiplier*Time.deltaTime);
    transform.localScale = new Vector3 (scale, scale, scale);
    if (Mathf.Abs (transform.localScale.x - targetScale) < 0.01f) {
     transform.localScale = new Vector3 (targetScale, targetScale, targetScale);
    }
   }
  }
 
  public void SetItem(Item item, int amount = 1){
   transform.localScale = initialScale; // 獲取物品動畫的初始Scale
   this.Item = item;
   this.Amount = amount;
   GetComponentInChildren<img> ().sprite = Resources.Load<sprite> (item.Sprite);
   GetComponentInChildren<text> ().text = Amount.ToString();
  }
 
  public void AddAmount(int amount = 1){
   transform.localScale = initialScale; // 獲取物品動畫的初始Scale
   this.Amount += amount;
   GetComponentInChildren<text> ().text = Amount.ToString();
  }
 
  public void SetAmount(int amount){
   transform.localScale = initialScale; // 獲取物品動畫的初始Scale
   this.Amount = amount;
   GetComponentInChildren<text> ().text = Amount.ToString();
  }
 
  public void Show(){
   gameObject.SetActive (true);
  }
 
  public void Hide(){
   gameObject.SetActive (false);
  }
 
  public void SetLocalPosition(Vector3 localPosition){
   transform.localPosition = localPosition;
  }
 }
}
</text></text></text></sprite>


然後,大家可以修改InventorySystem.cs,在Update中增加GetKeyDown事件,按下G鍵的時候會隨機產生一個物體,能夠方便大家觀察動畫的模樣。

留言