/// <summary> /// 非主控端接收数据 /// </summary> public void S2C_SetData(byte[] data) { #if UNITY_EDITOR //_syncCount ++; //_syncData = data; #endif int offset = 0; UnionValue union = new UnionValue(); // needSync union.ReadByteFrom(data, ref offset); byte needSync = union.byteValue; // position byte mask = 1; if ((needSync & mask) != 0) { _netPosition.SetData(Kit.ReadVector3FromBuffer(data, ref offset)); _isFreezed = (byte)(_isFreezed & (~mask)); } // rotation mask <<= 1; if ((needSync & mask) != 0) { _netRotation.SetData(Kit.ReadQuaternionFromBuffer(data, ref offset)); _isFreezed = (byte)(_isFreezed & (~mask)); } // velocity mask <<= 1; if ((needSync & mask) != 0) { _netVelocity.SetData(Kit.ReadVector3FromBuffer(data, ref offset)); _isFreezed = (byte)(_isFreezed & (~mask)); } // angularVelocity mask <<= 1; if ((needSync & mask) != 0) { _netAngularVelocity.SetData(Kit.ReadVector3FromBuffer(data, ref offset)); _isFreezed = (byte)(_isFreezed & (~mask)); } // aimPoint mask <<= 1; if ((needSync & mask) != 0) { _netAimPoint.SetData(Kit.ReadVector3FromBuffer(data, ref offset)); _isFreezed = (byte)(_isFreezed & (~mask)); } // input mask <<= 1; if ((needSync & mask) != 0) { union.ReadUShortFrom(data, ref offset); _netInput.SetData(union.ushortValue); _isFreezed = (byte)(_isFreezed & (~mask)); } // freezeCmd mask <<= 1; if ((needSync & mask) != 0) { union.ReadByteFrom(data, ref offset); byte freezeCmd = union.byteValue; mask = 1; for (int i = 0; i < 8; i++) { if ((freezeCmd & mask) != 0) { _isFreezed |= mask; } mask <<= 1; } } OnNetworkSync(); }
/// <summary> /// 主控端发送数据 /// </summary> /// <returns> 如果不需要发送,返回 null </returns> public byte[] C2S_GetData() { byte needSync = 0; byte freezeCmd = 0; int byteCount = 1; // position byte mask = 1; SyncAction action = _netPosition.GetSyncAction(); if (action != SyncAction.none) { needSync |= mask; if (action == SyncAction.freeze) { freezeCmd |= mask; } byteCount += Kit.sizeOfVector3; } // rotation mask <<= 1; action = _netRotation.GetSyncAction(); if (action != SyncAction.none) { needSync |= mask; if (action == SyncAction.freeze) { freezeCmd |= mask; } byteCount += Kit.sizeOfQuaternion; } // velocity mask <<= 1; action = _netVelocity.GetSyncAction(); if (action != SyncAction.none) { needSync |= mask; if (action == SyncAction.freeze) { freezeCmd |= mask; } byteCount += Kit.sizeOfVector3; } // angularVelocity mask <<= 1; action = _netAngularVelocity.GetSyncAction(); if (action != SyncAction.none) { needSync |= mask; if (action == SyncAction.freeze) { freezeCmd |= mask; } byteCount += Kit.sizeOfVector3; } // aimPoint mask <<= 1; action = _netAimPoint.GetSyncAction(); if (action != SyncAction.none) { needSync |= mask; if (action == SyncAction.freeze) { freezeCmd |= mask; } byteCount += Kit.sizeOfVector3; } // input mask <<= 1; action = _netInput.GetSyncAction(); if (action != SyncAction.none) { needSync |= mask; if (action == SyncAction.freeze) { freezeCmd |= mask; } byteCount += Kit.sizeOfUshort; } // freezeCmd mask <<= 1; if (freezeCmd != 0) { needSync |= mask; byteCount += 1; } byte[] buffer = null; if (needSync != 0) { buffer = GetShortestBuffer(byteCount); int offset = 0; UnionValue union = new UnionValue(needSync); union.WriteByteTo(buffer, ref offset); // position mask = 1; if ((needSync & mask) != 0) { Kit.WriteToBuffer(buffer, ref offset, _netPosition.GetData()); } // rotation mask <<= 1; if ((needSync & mask) != 0) { Kit.WriteToBuffer(buffer, ref offset, _netRotation.GetData()); } // velocity mask <<= 1; if ((needSync & mask) != 0) { Kit.WriteToBuffer(buffer, ref offset, _netVelocity.GetData()); } // angularVelocity mask <<= 1; if ((needSync & mask) != 0) { Kit.WriteToBuffer(buffer, ref offset, _netAngularVelocity.GetData()); } // aimPoint mask <<= 1; if ((needSync & mask) != 0) { Kit.WriteToBuffer(buffer, ref offset, _netAimPoint.GetData()); } // input mask <<= 1; if ((needSync & mask) != 0) { union.ushortValue = _netInput.GetData(); union.WriteUShortTo(buffer, ref offset); } // freezeCmd mask <<= 1; if ((needSync & mask) != 0) { union.byteValue = freezeCmd; union.WriteByteTo(buffer, ref offset); } } return(buffer); }
/// <summary> /// 确保最小最大值关系正确 /// </summary> public void OrderMinMax() { if (min > max) Kit.Swap(ref min, ref max); }
/// <summary> /// 返回一个指定范围内的随机整数 /// </summary> /// <param name="minValue"> 返回的随机数的下界(包含) </param> /// <param name="maxValue"> 返回的随机数的上界(不包含) </param> /// <returns> [minValue, maxValue) 范围的均匀分布随机数 </returns> public int Range(int minValue, int maxValue) { if (minValue > maxValue) Kit.Swap(ref minValue, ref maxValue); return minValue + (int)((maxValue - minValue) * Range01()); }
/// <summary> /// 返回一个指定范围内的随机浮点数 /// </summary> /// <param name="minValue"> 返回的随机数的下界(包含) </param> /// <param name="maxValue"> 返回的随机数的上界(不包含) </param> /// <returns> [minValue, maxValue) 范围的均匀分布随机数 </returns> public float Range(float minValue, float maxValue) { if (minValue > maxValue) Kit.Swap(ref minValue, ref maxValue); return minValue + (maxValue - minValue) * (float)Range01(); }
protected override void Editor_OnGUI(Rect rect, SerializedProperty property, GUIContent label) { if (_target == null) { _target = property.serializedObject.targetObject; _undoString = _target.ToString(); _propertyInfo = Kit.GetPropertyInfo(_target, _propertyName); if (_propertyInfo == null) { EditorGUI.LabelField(rect, label.text, "Can not find property."); } else if (drawer.fieldInfo.FieldType != _propertyInfo.PropertyType) { _propertyInfo = null; EditorGUI.LabelField(rect, label.text, "Property type is not same as field."); } else if (!_propertyInfo.CanRead || !_propertyInfo.CanWrite) { _propertyInfo = null; EditorGUI.LabelField(rect, label.text, "Property can not read or write."); } } if (_propertyInfo == null) return; EditorGUI.BeginChangeCheck(); object value = _propertyInfo.GetValue(_target, null); switch (property.propertyType) { case SerializedPropertyType.AnimationCurve: { value = EditorGUI.CurveField(rect, label, (AnimationCurve)value); break; } case SerializedPropertyType.Boolean: { value = EditorGUI.Toggle(rect, label, (bool)value); break; } case SerializedPropertyType.Bounds: { value = EditorGUI.BoundsField(rect, label, (Bounds)value); break; } case SerializedPropertyType.Color: { value = EditorGUI.ColorField(rect, label, (Color)value); break; } case SerializedPropertyType.Enum: { value = EditorGUI.EnumPopup(rect, label, (Enum)value); break; } case SerializedPropertyType.Float: { value = EditorGUI.FloatField(rect, label, (float)value); break; } case SerializedPropertyType.Integer: { value = EditorGUI.IntField(rect, label, (int)value); break; } case SerializedPropertyType.ObjectReference: { value = EditorGUI.ObjectField( rect, label, value as UnityEngine.Object, _propertyInfo.PropertyType, !EditorUtility.IsPersistent(_target)); break; } case SerializedPropertyType.Rect: { value = EditorGUI.RectField(rect, label, (Rect)value); break; } case SerializedPropertyType.String: { value = EditorGUI.TextField(rect, label, (string)value); break; } case SerializedPropertyType.Vector2: { value = EditorGUI.Vector2Field(rect, label, (Vector2)value); break; } case SerializedPropertyType.Vector3: { value = EditorGUI.Vector3Field(rect, label, (Vector3)value); break; } case SerializedPropertyType.Vector4: { value = EditorGUI.Vector4Field(rect, label.text, (Vector4)value); break; } default: { EditorGUI.LabelField(rect, label.text, "Type is not supported."); break; } } if (EditorGUI.EndChangeCheck()) { Undo.RecordObject(_target, _undoString); _propertyInfo.SetValue(_target, value, null); EditorUtility.SetDirty(_target); } }