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 可以拖拽赋值