由于帧同步需要各客户端的浮点数不能出现误差,所以需要使用经过处理过的浮点数。
不只是浮点数本身,所有基于浮点数实现的数据类型都要经过处理,包括Vector2、Vector3、Quaternion等等
由csharp代码实现,直接拖入unity可直接使用
// 添加该标签可以保证该数据类型可以被json等工具正常序列化
[Serializable]
class mFloat{
// 存储中会使用的真实值
public int realValue = 0;
// 默认处理的位数
public int scale = 1000;
// 程序中正常调用需使用的值
public float value{
set{
// 整存
realValue = (int)(value * (float)scale);
}
get{
// 零取
return (float)realValue / (float)scale;
}
}
// 初始化赋值
public mFloat(float f){
this.value = f;
}
// 符号重载
public static mFloat operator+ (mFloat a, mFloat b){
return new mFloat(a.value + b.value);
}
public static mFloat operator- (mFloat a, mFloat b){
return new mFloat(a.value - b.value);
}
public static mFloat operator* (mFloat a, mFloat b){
return new mFloat( a.value * b.value);
}
public static mFloat operator/ (mFloat a, mFloat b){
return new mFloat(a.value / b.value);
}
}
基于自定义浮点数据mFloat
[Serializable]
class mVector2 {
public mFloat x;
public mFloat y;
public mVector2(){
this.x = new mFloat(0f);
this.y = new mFloat(0f);
}
public mVector2(float x, float y){
this.x = new mFloat(x);
this.y = new mFloat(y);
}
public void Set(float x, float y){
this.x = new mFloat(x);
this.y = new mFloat(y);
}
// vector2处理
public void Set(Vector2 a){
this.x = new mFloat(a.x);
this.y = new mFloat(a.y);
}
// vector2还原
public Vector2 Get(){
return new Vector2(this.x.value, this.y.value);
}
public static mVector2 operator+ (mVector2 a, mVector2 b){
return new mVector2(a.x.value + b.x.value, a.y.value + b.y.value);
}
public static mVector2 operator- (mVector2 a, mVector2 b){
return new mVector2(a.x.value - b.x.value, a.y.value - b.y.value);
}
public static mVector2 operator* (mVector2 a, mVector2 b){
return new mVector2( a.x.value * b.x.value, a.y.value * b.y.value);
}
public static mVector2 operator/ (mVector2 a, mVector2 b){
return new mVector2(a.x.value / b.x.value, a.y.value / b.y.value);
}
}
基于自定义浮点数据mFloat
[Serializable]
class mVector3 {
public mFloat x;
public mFloat y;
public mFloat z;
public mVector3(){
this.x = new mFloat(0f);
this.y = new mFloat(0f);
this.z = new mFloat(0f);
}
public mVector3(float x, float y, float z){
this.x = new mFloat(x);
this.y = new mFloat(y);
this.z = new mFloat(z);
}
public void Set(float x, float y, float z){
this.x = new mFloat(x);
this.y = new mFloat(y);
this.z = new mFloat(z);
}
public void Set(Vector3 a){
this.x = new mFloat(a.x);
this.y = new mFloat(a.y);
this.z = new mFloat(a.z);
}
public Vector3 Get(){
return new Vector3(this.x.value, this.y.value, this.z.value);
}
public static mVector3 operator+ (mVector3 a, mVector3 b){
return new mVector3(a.x.value + b.x.value, a.y.value + b.y.value, a.z.value + b.z.value);
}
public static mVector3 operator- (mVector3 a, mVector3 b){
return new mVector3(a.x.value - b.x.value, a.y.value - b.y.value, a.z.value - b.z.value);
}
public static mVector3 operator* (mVector3 a, mVector3 b){
return new mVector3(a.x.value * b.x.value, a.y.value * b.y.value, a.z.value * b.z.value);
}
public static mVector3 operator/ (mVector3 a, mVector3 b){
return new mVector3(a.x.value / b.x.value, a.y.value / b.y.value, a.z.value / b.z.value);
}
}
基于自定义浮点数据mFloat
[Serializable]
class mQuaternion {
public mFloat x;
public mFloat y;
public mFloat z;
public mFloat w;
public mQuaternion(){
this.x = new mFloat(0f);
this.y = new mFloat(0f);
this.z = new mFloat(0f);
this.w = new mFloat(0f);
}
public mQuaternion(float x, float y, float z, float w){
this.x = new mFloat(x);
this.y = new mFloat(y);
this.z = new mFloat(z);
this.z = new mFloat(w);
}
public void Set(float x, float y, float z, float w){
this.x = new mFloat(x);
this.y = new mFloat(y);
this.z = new mFloat(z);
this.z = new mFloat(w);
}
public void Set(Quaternion a){
this.x = new mFloat(a.x);
this.y = new mFloat(a.y);
this.z = new mFloat(a.z);
this.z = new mFloat(a.w);
}
public Quaternion Get(){
return new Quaternion(this.x.value, this.y.value, this.z.value, this.w.value);
}
public static mQuaternion operator+ (mQuaternion a, mQuaternion b){
return new mQuaternion(a.x.value + b.x.value, a.x.value + b.x.value, a.z.value + b.z.value, a.w.value + b.w.value);
}
public static mQuaternion operator- (mQuaternion a, mQuaternion b){
return new mQuaternion(a.x.value - b.x.value, a.y.value - b.y.value, a.z.value - b.z.value, a.w.value + b.w.value);
}
public static mQuaternion operator* (mQuaternion a, mQuaternion b){
return new mQuaternion(a.x.value * b.x.value, a.y.value * b.y.value, a.z.value * b.z.value, a.w.value + b.w.value);
}
public static mQuaternion operator/ (mQuaternion a, mQuaternion b){
return new mQuaternion(a.x.value / b.x.value, a.y.value / b.y.value, a.z.value / b.z.value, a.w.value + b.w.value);
}
}