Unity writes a skill editor from scratch_01_Analyze requirements

I have always wanted to implement a skills editor since I joined the company. After accumulating some experience, I decided to develop one using ScriptableObject. Here is a record of
1. Simple requirements analysis
. In game development, the skill system is a crucial component. Skills determine the various actions a game character can perform, such as attacking, healing, moving, etc. Usually, skills are composed of multiple elements, including skill effect (Effect), skill trigger condition (triggerr), skill cooldown time (Cooldown), etc. In order to better manage and adjust these skills, we need a visual editor.
2. Roughly implement what functions
Buff is similar to the timer function startscript endscript delaytime (start delay time) lasttime (buff duration -1 is permanent) TrickTime (script execution interval) TrickScript (scripts that are refreshed and executed according to the interval within the duration)

Trigger: Some events trigger trigger conditions to determine whether to execute subsequent scripts. Scripts that are executed after triggering must bury the event at a specific location.

Effect generates bullet and executes Script (design a skillbyEffect to release skills so that monster NPC can use Buff to release skills)

Bullet bullet bullet form link track

Skill Skills should have various properties including damage, cooldown, target, range, etc. The editor needs to allow users to define these properties and associate Effects, Buffs, and Bullets with skills.

3. The meaning of ScriptableObject
You can create multiple different types of ScriptableObject, such as Skill, Buff, Effect, Bullet, etc., to meet different needs.
ScriptableObject's data is independent, they are not dependent on a specific scene or game object. This allows skills to be easily shared and reused across different scenes and game objects.
Scriptable operations: ScriptableObjects can be created, modified and managed at runtime through scripts.

For example

using UnityEngine;
using UnityEngine.Events;

[CreateAssetMenu(fileName = "New Skill", menuName = "Skill System/Skill")]
public class SkillSo : ScriptableObject
{
    
    
	[SerializeField]
	public SkillType skillType;

	[SerializeField]
	public AttackType attackType;

	[SerializeField]
	public float spiritCost;

	[SerializeField]
	public float damageRatio;

	[SerializeField]
	public float skillCD;

	public float lifeTime;

	public Vector3 direction;

	public float speed;

	public float damage = 1f;
}


[CreateAssetMenu(fileName = “New Skill”, menuName = “Skill System/Skill”)] is an attribute (Attribute) used to create new skill assets (Asset) in the Unity editor. It specifies the default filename and menu path when creating new skills in a Unity project. This makes it possible to create a new skill with a right click in the Unity Editor and save it as a ScriptableObject.
The following series of public fields are used to store various attributes and parameters of skills. These fields include:

skillType: The type of skill, which may be a custom enumeration type SkillType.
attackType: The attack type of the skill, which may be a custom enumeration type AttackType.
spiritCost: The energy consumption required to use the skill.
damageRatio: The damage ratio of the skill.
skillCD: skill cooldown time.
lifeTime: How long the skill exists in the game.
direction: the direction of the skill.
speed: the speed of the skill.
damage: The damage value of the skill, the default is 1.
I may extend it and the SkillType enumeration in the future

After writing this code, you can right-click to create the so file
Insert image description here

using System;

// 技能类型枚举
public enum SkillType
{
    
    
    MeteorSword,             // 陨剑术
    SkySword,           // 天剑
    SpeedBuff,          // 速度增益
    SwordRain,          // 剑雨
    WaterDrawSword,     // 提水剑
    None                // 无
}

A simple skillType enumeration
Insert image description here
and then design a skill base class to read and utilize the information in it

public abstract class SkillBase : MonoBehaviour
{
    
    
	public SkillSo so;

	public SkillType GetSkillType()
	{
    
    
		return so.skillType;
	}


	public AttackType GetAttackType()
	{
    
    
		return so.attackType;
	}


	public float SpiritCost()
	{
    
    
		return so.spiritCost;
	}


	public float DamageRatio()
	{
    
    
		return so.damageRatio;
	}


	public float SkillCD()
	{
    
    
		return so.skillCD;
	}


	protected void Awake()
	{
    
    
		so.sr = base.GetComponentInChildren<SpriteRenderer>();
	}




	protected virtual void SkillFinish()
	{
    
    
        UnityEngine.Object.Destroy(base.gameObject);
	}


}

In the future, some logic should be written in skillbase to handle it, such as providing skill icons to the outside world for UImanager to load, and causing damage to enemies within collision or range.

Posting an attackType

using System;

// 攻击类型枚举
public enum AttackType
{
    
    
    Metal,    // 金
    Wood,     // 木
    Water,    // 水
    Fire,     // 火
    Earth,    // 土
    Thunder,  // 雷
    Sword,    // 剑
    None      // 无
}

Then I will design palyerManager to read the SkillSo file of the instance on the prefabricated body and process it accordingly.

I will share it here today. No matter whether it can be used or not, I will write it first and then talk about it.

Guess you like

Origin blog.csdn.net/qq_45498613/article/details/132785403