Unity的C#編程教程_16_Variables 挑戰 2
- 2020 年 7 月 31 日
- AI
- C/C++/C#
- 第二個挑戰
- 假設一個 RPG 遊戲
- 需要創建一個道具的類,我們創建一個 C# Script 叫做 Item
- 如何在腳本里來創建道具呢?
- 我們知道,對於道具來說,有一些屬性,比如「名字」,「描述」,比如還可以有個 「圖標」,或許還有個 「價值」
- 這裡面其他都很容易理解,但是 「圖標」 該怎麼設置呢?在 Unity 中我們使用的是 Sprites
- 我們需要知道怎麼把 Sprites 影像添加到 變數 Variables 上
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Item : MonoBehaviour
{
public string itemName;
public string itemDescription;
public Sprite itemIcon;
public int itemPrice;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
}
- 寫好腳本,掛載到 Main Camera 下面
- 然後我們可以直接在 Unity 中輸入物品的名字,比如 Dark Golden Sword
- 輸入描述,比如 Unique powerful sword
- 輸入價格,比如 99999
- Sprite 可以拖拽賦值