【问题标题】:Setting Scale through RPC on Unity在 Unity 上通过 RPC 设置比例
【发布时间】:2014-04-29 04:08:51
【问题描述】:

我很难让我的健康条使用 RPC 通过 Unity 上的服务器同步。在我的游戏中,角色头上的生命值条应该会为整个服务器更新。这样你就可以只看另一个玩家并看到他们的健康栏。问题是,即使我通过网络发送信息并接收到信息,实际的物理条的比例也不会改变。不过,发出呼叫的玩家的栏已更改。

这是问题的截图:http://i.imgur.com/g2GozZv.png

当我发送 RPC 时,它确实会改变其他玩家的生命值,但不会影响规模。

我做了以下代码,但它不起作用:

void Start()
{
    if(!networkView.isMine)
    {
        enabled = false;
    }
}

void Update ()
{
    if(Input.GetKey(KeyCode.Alpha2))
    {
         Minus_Health();
    }
}

public void Minus_Health()
{
    health -= 10;
    healthBarLength = (float)health / (float)maxHealth / 5.1f;
    healthBar.scale = new Vector2(healthBarLength, healthBar.scale.y);
    Update_HP(health, maxHealth, healthBar.scale);
}

public void Update_HP(int hp, int maxHP, Vector3 bar)
{
    networkView.RPC("Update_Health",RPCMode.All, hp, maxHP, bar);
}


[RPC]
public void Update_Health(int value, int value2, Vector3 bar)
{
    health = value;
    maxHealth = value2;
    healthBar.scale = new Vector2(bar.x, bar.y);
}

我也试过这个,也没有运气:

void OnSerializeNetworkView(BitStream stream, NetworkMessageInfo info)
{
    if (stream.isWriting)
    {
        Vector3 networkHealth = healthBar.scale;
        stream.Serialize(ref networkHealth);
    }
    else
    {
        Vector3 networkHealth = Vector3.zero;
        stream.Serialize(ref networkHealth);
        healthBar.scale = networkHealth;
    }
}

【问题讨论】:

    标签: networking unity3d client rpc


    【解决方案1】:

    我发现了问题。代码本身运行良好(你说得对,那个 bar 参数很乏味)。

    问题实际上出在我用于健康栏的 ex2D 插件上。在每个 exSprite 上都有一个摄像头视图,设置为用户的主摄像头。因为它在播放器实例化时设置为我的相机,所以它只能通过我的相机看到我的栏,因此不会通过客户端/服务器端更新另一个栏。通过单击纹理并将 ex2D exSprite 的 Camera 设置为 None,我现在可以看到两个条都被正确更新/缩放。

    希望这可以帮助任何寻找如何通过网络制作健康栏的人,这是我正在使用的最终代码:

    using UnityEngine;
    using System.Collections;
    
    public class PlayerStats : MonoBehaviour
    {
        public int health;
        public int maxHealth;
        public float healthBarLength = 0;
        public exSprite healthBar;
    
        void Start()
        {
            if(!networkView.isMine)
            {
                enabled = false;
            }
        }
    
        void Update ()
        {
            if(Input.GetKey(KeyCode.Alpha2))
            {
                Minus_Health();
                Update_HP(health, maxHealth);
            }
        }
    
        public void Minus_Health()
        {
            health -= 10;
        }
    
        public void Update_HP(int hp, int maxHP)
        {
            networkView.RPC("Update_Health", RPCMode.AllBuffered, hp, maxHP);
        }
    
    
        [RPC]
        public void Update_Health(int value, int value2)
        {
            health = value;
            maxHealth = value2;
    
            healthBarLength = (float)value / (float)value2 / 5.1f;
            healthBar.scale = new Vector2(healthBarLength, healthBar.scale.y);
        }
    }
    

    另外,给那些在将 exSprite Camera 设置为 None 时出错的小提示;你需要更新你的 ex2D 插件。

    感谢 pek 的帮助,我希望这可以帮助别人! :)

    【讨论】:

    • 很高兴能提供帮助,即使我提供的帮助很少;) exSprite 听起来很难找到。顺便说一句,你应该接受你的答案是正确的。
    • 谢谢,我刚开始使用堆栈溢出。大多数时候我只是把头撞到墙上,直到它发出咔嗒声,但这一次我决定伸出援手,否则我永远不会好起来,如果我这样做了,那将需要 10 倍的时间。我真的很感谢您的帮助,并感谢您让我在这个网站上感到受欢迎。我希望你有一个美好的一天,保重^^
    • @Rayden 嘿,Rayden 你必须序列化健康和健康栏值吗?因为我的脚本我可以更新所有玩家的健康,但对于后期玩家,他们不会从游戏中的后期玩家那里收到更新的健康和健康条,我正在使用 RPC.AllBuffered :/
    【解决方案2】:

    如果 Update Health 中的所有参数都正确,那么可能有其他因素影响了规模。

    另外,如果valuevalue2发送正确,那么就不需要bar参数了:

    [RPC]
    public void Update_Health(int value, int value2)
    {
        health = value;
        maxHealth = value2;
    
        healthBarLength = (float)health / (float)maxHealth / 5.1f;
        healthBar.scale = new Vector2(healthBarLength, healthBar.scale.y);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-12-03
      • 2021-05-24
      • 1970-01-01
      • 1970-01-01
      • 2021-12-28
      • 2019-04-04
      • 2012-09-03
      • 2013-12-16
      相关资源
      最近更新 更多