Unity游戏开发学习记录

Unity - Creator Kit: Cactus RPG

2024-01-01  本文已影响0人  我阿郑

https://learn.unity.com/project/creator-kit-beginner-code?uv=2020.3

导入资源 Creator Kit: Beginner Code assets

双击 ExampleScene 打开场景

image.png

点击Play按钮运行:

image.png

This is a hack-and-slash RPG game with a point-and-click control system.
这是一款带有点击式控制系统的砍杀RPG游戏。

This means you can click on:

Try attacking the fiendish Training Dummy to get started. You can zoom in and out using your scroll wheel.
尝试攻击恶魔般的训练假人以开始。您可以使用滚轮放大和缩小。

- hack-and-slash 砍杀
- point-and-click: n. 指向-点击
- add it to your inventory `/ˈɪnvəntɔːri/` 添加到您的库存中
- inventory 库存

✅ Inventory and character stats

Click on the inventory satchel in the bottom left-hand corner of the screen to: 点击屏幕左下角的【库存背包】

image.png image.png

You can also 【open/close】 the inventory with the keyboard shortcut I.

✅ Open the SpawnerSample script file

When you tested the game in the previous tutorial, you may have noticed that three health potions appear around the PotionSpawner GameObject at game start.

-  potions: n. 药剂,药水;魔药(potion的复数形式)
-  PotionSpawner 药水孵化器,可以产生药水
image.png

This component is the script which makes the three potions appear.

Double-click the SpawnerSample script. This is what you will see in the code editor:

using UnityEngine;
using CreatorKitCode;

public class SpawnerSample : MonoBehaviour
{
    public GameObject ObjectToSpawn;

    void Start()
    {
        int angle = 15;
        Vector3 spawnPosition = transform.position;

        Vector3 direction = Quaternion.Euler(0, angle, 0) * Vector3.right;
        spawnPosition = transform.position + direction * 2;
        Instantiate(ObjectToSpawn, spawnPosition, Quaternion.identity);

        angle = 55;
        direction = Quaternion.Euler(0, angle, 0) * Vector3.right;
        spawnPosition = transform.position + direction * 2;
        Instantiate(ObjectToSpawn, spawnPosition, Quaternion.identity);

        angle = 95;
        direction = Quaternion.Euler(0, angle, 0) * Vector3.right;
        spawnPosition = transform.position + direction * 2;
        Instantiate(ObjectToSpawn, spawnPosition, Quaternion.identity);
    }
}

✅ Write your own spawner class

using CreatorKitCode;

public class LootAngle
{
    int angle;
    int step;
    public LootAngle(int increment)
    {
        step = increment;
        angle = 0;
    }

    public int NextAngle()
    {
        int currentAngle = angle;
        angle = Helpers.WrapAngle(angle + step);

        return currentAngle;
    }
}

修改 SpawnerSample:

public class SpawnerSample : MonoBehaviour
{
    public GameObject ObjectToSpawn;

    void Start()
    {
        LootAngle myLootAngle = new LootAngle(45);
        //every call will advance the angle!
        SpawnPotion(myLootAngle.NextAngle());
        SpawnPotion(myLootAngle.NextAngle());
        SpawnPotion(myLootAngle.NextAngle());
        SpawnPotion(myLootAngle.NextAngle());
    }

    void SpawnPotion(int angle)
    {
        int radius = 5;

        Vector3 direction = Quaternion.Euler(0, angle, 0) * Vector3.right;
        Vector3 spawnPosition = transform.position + direction * radius;
        Instantiate(ObjectToSpawn, spawnPosition, Quaternion.identity);
    }
}

✅ Create a new spawning object

Let’s create a different GameObject which spawns money at game start:
让我们创建一个不同的游戏对象,它在游戏开始时【生成金钱】:

In the Hierarchy, right-click and select 3D Object > Sphere from the contextual menu. Rename your new GameObject MoneySpawner.

In the Scene view, use the Move Tool (shortcut: W) to move the MoneySpawner GameObject next to the PotionSpawner GameObject.

image.png

接着,给 MoneySpawner 添加一个已有的名为 Spawner Sample 的脚本:

image.png

In the Project window, go to Assets/Creator Kit - Beginner Code/Prefabs/Tutorial and find the MoneyLoot Prefab.

image.png

拖拽给 Spawner Sample 的脚本的 Object To Spawn 字段赋值:

image.png

✅ Customize the health potions

In this tutorial, you’ll write an effect for those potions so they give the player character an additional 10 health points when used.

image.png

为“药水”编写代码,玩家使用药水后增加“10 点生命值”。

In the top menu, go to Beginner Code > Create Item Effect .

image.png

Rename to “AddHealthEffect

image.png

生成新的脚本:


image.png

go to Assets/Creator Kit - Beginner Code/Prefabs /ItemDatabase. Select the Potion Prefab.

image.png

In the Inspector, use the Add New Effect drop-down menu to select the AddHealthEffect. This will appear in the Script field beneath.

image.png

✅ Write the effect function

修改AddHealthEffect脚本如下:

using CreatorKitCode;

public class AddHealthEffect : UsableItem.UsageEffect
{
    public int HealthAmount;

    public override bool Use(CharacterData user)
    {
        user.Stats.ChangeHealth(HealthAmount);
        return true;
    }
}

保存一下,然后就可以看到多了一个 Health Amount 字段,我们设置为10:

image.png

保存场景,运行。

✅ Customize your game

This tutorial is a reference guide for further customization of the Creator Kit: Beginner Code game.

It provides guidance to help you use script templates to create :

使用脚本模版创建:

➡️ Guide structure

This guide is a tool to help you consolidate your understanding and experiment with the game, rather than a step-by-step guide.
It might also be a useful environment to apply what you have learnt in other Unity tutorials!

This guide has the following structure:

  1. How to create an item type in Unity Editor 如何在Unity Editor中创建item类型
  2. How to create the custom effect script for that item, and basic guidance on using it 如何为该项目创建自定义效果脚本,以及使用它的基本指导
  3. How to apply the effect to the item 如何【将效果应用于item

✅ Create a Usable Item

What is a Usable Item?

A Usable Item is an item that the player can use by double-clicking on it in their inventory (for example, a health potion).
可用物品是玩家可以通过在【库存背包中】双击它来使用的物品(例如,健康药水)。

How do I create a Usable Item in Unity Editor?

image.png image.png

In the Inspector, you will see the following fields (exposed variables):

image.png

In the next step, you’ll learn how to write a Usage Effect script and apply it to your Usable Item.

下一步学习【如何编写使用Usage Effect 脚本】并将其应用于你自定义的Usable Item 上。

✅ Write a Usage Effect script

What is a Usage Effect script?

A Usage Effect script specifies what happens when a Usable Item is double-clicked in the player’s inventory. It is added to a Usable Item Asset in the Editor.

Usage Effect脚本用于指定玩家在物品栏中双击Usable Item时会发生什么。

How do I create a new Usage Effect script?

image.png

点击create :

image.png

This creates the script in Assets/Scripts/ItemEffect and selects it in the Editor.

image.png

生成的文件代码如下:

using CreatorKitCode;

public class MyItemEffect : UsableItem.UsageEffect
{
    public override bool Use(CharacterData user)
    {
        return false;
    }
}

How do I customize the Usage Effect? 如何自定义Usage Effect?

To customize the Usage Effect, you need to add instructions to the Use function:

public override bool Use(CharacterData user)
{
        return false;
}

How do I find out more about the custom CharacterData class?
如何找到有关自定义 CharacterData 类的更多信息?

image.png

点击 Open Documentation ,打开如下页面:

image.png image.png

➡️ How do I set the return value for the Usable Item?

The Use function returns a Boolean value (true or false). Set it to:

✅ Apply the Usage Effect script to the Usable Item

In the Project window, select the Usable Item you have created :
选择你创建的Usable Item

image.png

find the Add New Effect field. Select your Usage Effect from the drop-down menu.

image.png

✅ Create an Equipment Item

An Equipment Item is an item that gives the player character particular attributes when equipped. (Weapons are a special kind of Equipment Item with additional functionality; this is explored separately later in this guide.)

【装备物品】是一种在装备时赋予玩家角色特定属性的物品。(武器是一种特殊类型的装备物品,具有附加功能;本指南稍后将单独探讨。

➡️ How do I create an Equipment Item in Unity Editor?

Assets/Creator Kit - Beginner Code/Prefabs/InGameItem 路径下, 右键 Create | Beginner Code | Equipment Item,可以重命名一个好记的名字

image.png

In the Inspector, you will see the following fields (exposed variables):

image.png

大多数字段和创建UsableItem是同理的,有几个特有的如下:

有几个可选项:

image.png
Equipment Slot : 装备位置
`worn`: v. 穿;佩戴;磨损(`wear`的过去分词)
`Torso`:n. 躯干;裸体躯干雕像;未完成的作品;残缺不全的东西
`Accessory`:n. 附件, 配件

✅ Write an Equipped Effect script

➡️ What is an Equipped Effect script?

An Equipped Effect script enables a custom effect when the player equips an item. It is added to an Equipment Item or Weapon in the Editor.

➡️ How do I create a new Equipped Effect script?

go to Beginner Code > Create Equipped Effect.

image.png

This creates the script in Assets/Scripts/EquippedEffect and selects it in the Editor.

image.png

生产的脚本代码如下:

using CreatorKitCode;

public class MyEquippedEffect : EquipmentItem.EquippedEffect
{
     public override void Equipped(CharacterData user)
     {
     
     }
     
     public override void Removed(CharacterData user)
     {
     
     }
}
  1. void Equipped(CharacterData user) 方法

This function is called when the player (user) equips the item. Use the function to add the effect — for example, to give the character a +1 Strength stat modifier.
当玩家(用户)装备物品时,将调用此函数。使用该函数添加效果 - 例如,为角色提供 Strength属性+1 的修改。

TIP: You can use a private variable to store your chosen modifier.

  1. void Removed(CharacterData user) 方法

This function is called when the player (user) removes the item. Use this function to remove the effect on the player character.

✅ Apply the Equipped Effect script to an Equipment Item

Assets/Creator Kit - Beginner Code/Prefabs/InGameItem 路径下选择你创建的 Equipment Item ,点击 Add new Effect 就会添加1个 Effect:

image.png

✅ Create a Weapon

➡️ What is a Weapon?

A Weapon is a special Equipment Item that you can equip on the character Weapon Slot.

In addition to the features of an ordinary Equipment Item, you can also give a Weapon: 除了【普通装备物品的特性】外,您还可以赋予一个武器:

➡️ How do I create a Weapon in Unity Editor?

和创建 Equipment Item 步骤一样。

image.png

Weapon Stats:

image.png

武器攻击的速度。这是两次攻击之间的冷却时间,数值越低,下一次攻击越快。如果数值为0,在第一次动画完成后立即执行下一次攻击。

✅ Write a Weapon Attack Effect script

➡️ What is a Weapon Attack Effect script?

A Weapon Attack Effect script enables a customized effect when the Weapon hits a target. It is added to a Weapon.

image.png image.png

We’ve provided a template script to help you write a Weapon Attack Effect for items:

using CreatorKitCode;

public class MyWeaponEffect : Weapon.WeaponAttackEffect
{
    public override void OnAttack(CharacterData target, CharacterData user, ref Weapon.AttackData attackData)
    {
        
    }
    
    public override void OnPostAttack(CharacterData target, CharacterData user, Weapon.AttackData data)
    {
        
    }
}

This function is for instructions which control what happens when the Weapon hits an enemy. You can add damage or apply elemental effects to the target using the 【AddDamage function in attackData】.
这个函数用于控制武器击中敌人时发生的事件。您可以使用attackData中的AddDamage函数向目标造成伤害或施加元素效果。

This function is called after all the OnAttack function instructions are executed. This enables you to customize things that happen in reaction to the total amount of damage the target receives after all OnAttack effects are applied.

在执行完所有OnAttack函数的指令后,将调用此函数。这使您能够根据target在应用所有OnAttack效果后所受到的【总伤害】定制响应的事件。

➡️ How do I find out more about the custom Weapon.AttackData class?

同理,在 API Documentation 搜 Weapon.AttackData:

image.png

✅ Apply the Weapon Attack Effect script to a Weapon

image.png

save your changes.

TIP: If you want to remove the Weapon Attack Effect, select the minus button (-) to the right of the Script field in the Inspector.

image.png
上一篇下一篇

猜你喜欢

热点阅读