【问题标题】:How to reference instantiated object in [Command] function?如何在 [Command] 函数中引用实例化对象?
【发布时间】:2017-12-11 11:50:47
【问题描述】:

我目前正在学习统一制作多人 fps,但我遇到了问题。我有一个PlayerShoot 脚本,可以处理不同武器和类型的射击。这是代码。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
using Weapons;

public class PlayerShoot : NetworkBehaviour {
public WeaponManager weaponManager;

void Awake()
{
    weaponManager = GetComponent<WeaponManager> ();

    if (weaponManager == null)
        return;
}

void Update()
{
    if (!isLocalPlayer)
        return;

    // Fire
    if (Input.GetButtonDown ("Fire1")) 
    {
        HandleFire ();
    }
}

void HandleFire()
{
    Weapon currentWeapon = weaponManager.equippedWeapon;

    if (!currentWeapon.CanFire())
        return;

    switch (currentWeapon.weaponType) 
    {
        case WeaponType.THROWING:
            ThrowWeapon ((WeaponThrowing)currentWeapon);
            break;

        case WeaponType.FIREARM:
            RaycastShoot(currentWeapon);
            break;

        case WeaponType.MELEE:
            AttackMelee(currentWeapon);
            break;
    }
}

// Throwing weapon
void ThrowWeapon(WeaponThrowing weapon)
{
    GameObject throwedObject = (GameObject)Instantiate (weapon.throwObjectPrefab, weaponManager.throwWeaponPlace.position,  weaponManager.throwWeaponPlace.rotation);

    Debug.Log(throwedObject);

    Rigidbody throwedObjectRB = throwedObject.GetComponent<Rigidbody> ();

    if (throwedObjectRB != null) 
    {
        throwedObjectRB.AddForce(throwedObject.transform.forward * weapon.throwForce, ForceMode.Impulse);
    }

    CmdOnWeaponThrowed();
}

[Command]
void CmdOnWeaponThrowed()
{
    // How to access my throwed object here. 

    NetworkServer.Spawn(obj, obj.GetComponent<NetworkIdentity>().assetId);
}

// Raycast shooting
void RaycastShoot(Weapon weapon)
{

}

// Melle attack
void AttackMelee(Weapon weapon)
{

}
}

在这里,在手柄射击中,我得到装备的武器并检查其类型,并根据类型调用射击该类型的方法。就我而言,它正在投掷武器。在投掷武器函数中,我实例化武器预制件,然后调用CmdOnWeaponThrowed 在所有客户端中生成对象。所以我的问题是我无法访问 CmdOnWeaponThrowed 函数中的 throwedObject 变量,因为命令不会将对象作为参数访问。

【问题讨论】:

  • 你在方法CmdOnWeaponThrowed中有obj。该游戏对象的声明在哪里??
  • obj 只是占位符 我想了解如何访问它,我想访问我的 throwedObject

标签: c# unity3d unity3d-unet unet


【解决方案1】:

将其传递给方法:

[Command]
void CmdOnWeaponThrowed(GameObject obj)
{
    var netId = obj.GetComponent<NetworkIdentity>();
    if (netId == null)
    {
        // component is missing.
        return;
    }
    NetworkServer.Spawn(obj, netId.assetId);
}

然后

void ThrowWeapon(WeaponThrowing weapon)
{
    GameObject throwedObject = (GameObject)Instantiate (weapon.throwObjectPrefab, weaponManager.throwWeaponPlace.position,  weaponManager.throwWeaponPlace.rotation);

    Debug.Log(throwedObject);

    Rigidbody throwedObjectRB = throwedObject.GetComponent<Rigidbody> ();

    if (throwedObjectRB != null) 
    {
        throwedObjectRB.AddForce(throwedObject.transform.forward * weapon.throwForce, ForceMode.Impulse);
    }

    CmdOnWeaponThrowed(throwedObject);
}

希望这会有所帮助:)

【讨论】:

  • 我已经尝试过了,但命令函数只接受基本数据类型,如 int、float 等。参数变为 null 并且函数踢出错误对象引用未设置为对象的实例。
  • 你能否在生成时将对象的引用存储在函数之外。以后在命令中你可以使用它吗?
  • 我试过了,但没有帮助仍然会出现错误。
  • docs 说你可以传递带有网络身份的游戏对象。你不应该拥有武器的网络身份吗?
  • 我在帖子底部添加了错误图片链接
猜你喜欢
  • 1970-01-01
  • 2015-08-28
  • 2022-01-17
  • 1970-01-01
  • 2020-01-04
  • 1970-01-01
  • 1970-01-01
  • 2019-10-19
  • 2014-10-31
相关资源
最近更新 更多