简体   繁体   English

将从scriptableobject继承的脚本添加到游戏对象

[英]Add script inheriting from scriptableobject to game object

I have a script called Weapon.cs, which is a scriptable object. 我有一个名为Weapon.cs的脚本,这是一个可编写脚本的对象。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public abstract class Weapon : ScriptableObject {

protected Camera _mainCamera;
protected Transform _weaponTransform;
protected int _damage;
protected int _fireRatePerSecond;
protected bool _isAutomaticWeapon;

protected void FireWeapon()
{
    //if the weapon is automatic
    if (_isAutomaticWeapon)
    {
        ShootRapidFireOnMouseHold();
    }
    //if the weapon is semi automatic
    else
    {
        ShootSingleBulletOnMouseClick();
    }
}

protected void MoveWeaponWithCamera(Transform weaponTransform)
{
    _weaponTransform.rotation = _mainCamera.transform.rotation; //temporary way of making sure the gun moves with the camera
}

protected void ShootSingleBulletOnMouseClick()
{
    if (Input.GetKeyDown(KeyCode.Mouse0)) //if left mouse button is clicked
    {
        CastRay();
    }
}

protected void ShootRapidFireOnMouseHold()
{
    if (Input.GetKey(KeyCode.Mouse0)) //if left mouse button is held down
    {
        CastRay(); //rapid fire
        //PlayShootAnimation();
    }
}

protected void CastRay()
{
    RaycastHit hit;
    if (Physics.Raycast(_mainCamera.transform.position, _mainCamera.transform.forward, out hit, 100))
    {
        Debug.Log(hit.transform.name);
    }
}
//protected abstract void PlayShootAnimation();
}

I have a second script called MachineGun.cs, which inherits from Weapon.cs, and thus indirectly from scriptable object. 我有另一个名为MachineGun.cs的脚本,它是从Weapon.cs继承的,因此是间接从可脚本编写的对象继承的。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MachineGun : Weapon{

private GameObject _machineGunBarrel;

//private float _animationVelocity;

// Use this for initialization
void Start () {
    //initialize the basic properties of a weapon
    _damage = 7;
    _fireRatePerSecond = 10;
    _isAutomaticWeapon = true;

    _weaponTransform = GameObject.Find("Weapon_MachineGun").transform;
    _machineGunBarrel = GameObject.Find("machinegun_p1");

    _mainCamera = Camera.main;

}

// Update is called once per frame
void Update () {
    MoveWeaponWithCamera(_weaponTransform);
    FireWeapon();
}

//protected override void Shoot()
//{
//    RaycastHit hit;
//    if(Physics.Raycast(_mainCamera.transform.position, _mainCamera.transform.forward, out hit, 100))
//    {
//        Debug.Log(hit.transform.name);
//    }
//}

void PlayShootAnimation()
{
    _machineGunBarrel.transform.RotateAround(_machineGunBarrel.transform.up, _fireRatePerSecond * Time.deltaTime);
    PlayShootAnimation(); //play the shoot animation of the gun
}
}

It's currently impossible, since MachineGun.cs doesn't inherit from monobehaviour anymore. 由于MachineGun.cs不再从monobehaviour继承,目前这是不可能的。

I have a weapon gameobject in my scene, and so here's my question: How do I go about adding the MachineGun.cs script as a component to my weapon gameobject? 我的场景中有一个武器游戏对象,所以这是我的问题:如何将MachineGun.cs脚本作为组件添加到我的武器游戏对象中? Or since this is impossible, 或者由于这是不可能的,

How should I build a weapon system with a general Weapon.cs script from which all weapons can inherit basic functions and fields/variables? 我应该如何使用通用的Weapon.cs脚本构建武器系统,所有武器都可以从中继承基本功能和字段/变量?

EDIT 1: provided code to my post and added the "why I wanna do this". 编辑1:向我的帖子提供了代码,并添加了“我为什么要这样做”。

Thanks in advance! 提前致谢!

ScriptableObject says: ScriptableObject说:

Description 描述

A class you can derive from if you want to create objects that don't need to be attached to game objects . 如果要创建不需要附加到游戏对象上的对象,可以从中派生一个类。

So either you should not attach this script to game object or derive your object from "MonoBehaviour" if you want to attach it to game object. 因此,如果您不希望将此脚本附加到游戏对象,则不要将该脚本附加到游戏对象,也不要从“ MonoBehaviour”派生您的对象。

Why did you derive it from ScriptableObject ? 为什么要从ScriptableObject派生它?

ScriptableObjects should mainly be used in a data oriented way, they are very convenient and quite efficient for the task. ScriptableObjects应该主要以面向数据的方式使用,它们非常方便且非常高效。 Also, plaguing your project of MonoBehaviour is a very bad (and wide-spread) practice. 另外,困扰您的MonoBehaviour项目是一种非常糟糕的做法(而且广为流传)。

IMO , you should have a MonoBehaviour with weapon logic management and your ScriptableObjects should be your weapon data (which is interpreted by loading them up in your Weapon MonoBehaviour ), such that you have Minigun, Glock, Katana.. Scriptable objects which have data like, attack speed, reload speed, charger size, weapon model/textures, reference to model hitbox, yadayadayada. 海事组织 ,你应该有武器的逻辑管理MonoBehaviour和你ScriptableObjects应该是你的武器的数据(这是由您支招MonoBehaviour装起来解释),其中有这样的数据,你有机枪,格洛克,武士刀.. 可编程对象 ,攻击速度,重装速度,充电器大小,武器模型/纹理,对模型Hitbox的引用,yadayadayada。 (You might have a generic Weapon MonoBehaviour , but derive a Gun one, a Blade etc.. for very specific management, but which will still need data from ScriptableObjects ) (您可能具有通用的武器MonoBehaviour ,但派生了Gun Gun,Blade等用于非常特定的管理,但仍需要ScriptableObjects的数据)

In short your MonoBehaviours define usage and interaction, while your ScriptableObjects define characteristics 简而言之,您的MonoBehaviours定义用法和交互,而ScriptableObjects定义特征

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM