如何将rgb颜色传递到OnPhotonSerializeView(光子网络)



我需要将rgb变量传递到OnPhotonSerializeView。

我试着这样做:

public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
{
if (stream.IsWriting)
{
stream.SendNext(healthCircle.color.r);
stream.SendNext(healthCircle.color.g);
stream.SendNext(healthCircle.color.b);
}
else 
{
healthCircle.color.r = (float)stream.ReceiveNext();
healthCircle.color.g = (float)stream.ReceiveNext();
healthCircle.color.b = (float)stream.ReceiveNext();
}
}

在这之后,我得到了一个错误:

AssetsScriptsPlayer.cs(68,13): error CS1612: Cannot modify the return value of 'SpriteRenderer.color' because it is not a variable

我试着在谷歌上搜索,但什么也没找到。对不起我的问题。正在等待您的帮助:(

SpriteRenderer.color(就像Unity中的大多数东西一样(是一个属性,它返回或接受一个完整的Color作为赋值。

除此之外,Colorstruct,因此是按值复制,而不是引用类型,因此即使可以执行此操作,也会在此处发生的情况是

  • 返回当前healthCircle.color
  • 在此返回的Color值上分配,例如r = (float)stream.ReceiveNext();

=>所以这个

  • 始终返回一个新的Color实例,即当前实例的副本,并只分配其中的一个组件
  • 无效,因为healthCircle.color从未实际分配过新值

你想做的是要么

var color = healthCircle.color;
color.r = (float)stream.ReceiveNext();
color.g = (float)stream.ReceiveNext();
color.b = (float)stream.ReceiveNext();
healthCircle.color = color;

或者直接做

healthCircle.color = new Color((float)stream.ReceiveNext(), (float)stream.ReceiveNext(), (float)stream.ReceiveNext());

相关内容

  • 没有找到相关文章

最新更新