【发布时间】:2018-05-15 09:36:38
【问题描述】:
我有一个名为 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();
}
我还有一个名为 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
}
}
目前这是不可能的,因为 MachineGun.cs 不再从 monobehaviour 继承。
我的场景中有一个武器游戏对象,所以这是我的问题: 如何将 MachineGun.cs 脚本作为组件添加到我的武器游戏对象中?或者既然这是不可能的,
我应该如何使用通用的 Weapon.cs 脚本构建一个武器系统,所有武器都可以从中继承基本功能和字段/变量?
编辑 1:在我的帖子中提供了代码并添加了“我为什么要这样做”。
提前致谢!
【问题讨论】:
-
如果脚本不继承自
Component(或MonoBehaviour),则无法将其添加为游戏对象的组件。 ScriptableObjects 是您项目中的资产(在大多数情况下保存数据)。然后,您可以在其他脚本中使用它们,这些脚本继承自 MonoBehaviour,附加到 GameObjects。 -
我已经编辑了我的帖子以明确我的意图。
-
我需要让
Weapon继承自ScriptableObject吗?你不使用CreateAssetMenu属性....所以我猜你没有在你的项目选项卡中创建一个真正的 ScriptableObject ? (除非您有相应的编辑器脚本来执行此操作) -
我认为这是从 ScriptableObject 获得与从公共抽象武器类中获得相同行为的好方法,并具有继承字段的额外好处..
-
脚本附加到游戏对象后,会创建一个新实例,并且您在编辑器中(或在运行时通过脚本)提供的值不会影响其他实例(除非你使用
static类变量)
标签: c# unity3d inheritance scripting