AMWriteBinaryData.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using UnityEngine;
  5. public class AMWriteBinaryData
  6. {
  7. public AMWriteBinaryData(AnimatorData anime_data)
  8. {
  9. this.anime_data_ = anime_data;
  10. NDebug.AssertNull(this.anime_data_);
  11. NDebug.Assert(this.anime_data_.takes.Count == 1, "take over error.");
  12. this.total_frame_ = this.anime_data_.getCurrentTake().numFrames;
  13. this.frame_rate_ = this.anime_data_.getCurrentTake().frameRate;
  14. }
  15. public static bool PossibleBinaryConvert(AMTrack track)
  16. {
  17. return AMBinaryTranslation.CheckType(track) || AMBinaryRotation.CheckType(track) || AMBinaryProperty.CheckType(track) || AMBinaryMethod.CheckType(track);
  18. }
  19. public void CreateData(BinaryWriter binary)
  20. {
  21. binary.Write("BaseData");
  22. binary.Write(this.total_frame_);
  23. binary.Write(this.frame_rate_);
  24. AMTake currentTake = this.anime_data_.getCurrentTake();
  25. List<AMTrack> trackValues = currentTake.trackValues;
  26. for (int i = 0; i < trackValues.Count; i++)
  27. {
  28. trackValues[i].updateCache();
  29. if (AMBinaryTranslation.CheckType(trackValues[i]))
  30. {
  31. this.CreateBinaryTranslation(trackValues[i]).Write(binary);
  32. }
  33. else if (AMBinaryRotation.CheckType(trackValues[i]))
  34. {
  35. this.CreateBinaryRotation(trackValues[i]).Write(binary);
  36. }
  37. else if (AMBinaryProperty.CheckType(trackValues[i]))
  38. {
  39. this.CreateBinaryProperty(trackValues[i]).Write(binary);
  40. }
  41. else if (AMBinaryMethod.CheckType(trackValues[i]))
  42. {
  43. this.CreateBinaryMethod(trackValues[i]).Write(binary);
  44. }
  45. }
  46. binary.Write("Finish");
  47. }
  48. public AMBinaryTranslation CreateBinaryTranslation(AMTrack track_data)
  49. {
  50. if (!AMBinaryTranslation.CheckType(track_data))
  51. {
  52. return null;
  53. }
  54. AMTranslationTrack amtranslationTrack = track_data as AMTranslationTrack;
  55. AMBinaryTranslation ambinaryTranslation = new AMBinaryTranslation();
  56. this.CreateBaseData(track_data, amtranslationTrack.obj.gameObject, ambinaryTranslation);
  57. ambinaryTranslation.pos_array = new Vector3[this.total_frame_];
  58. for (int i = 1; i <= this.total_frame_; i++)
  59. {
  60. int num = i - 1;
  61. ambinaryTranslation.pos_array[num] = amtranslationTrack.getPositionAtFrame((float)i);
  62. }
  63. return ambinaryTranslation;
  64. }
  65. public AMBinaryRotation CreateBinaryRotation(AMTrack track_data)
  66. {
  67. if (!AMBinaryRotation.CheckType(track_data))
  68. {
  69. return null;
  70. }
  71. AMRotationTrack amrotationTrack = track_data as AMRotationTrack;
  72. AMBinaryRotation ambinaryRotation = new AMBinaryRotation();
  73. this.CreateBaseData(track_data, amrotationTrack.obj.gameObject, ambinaryRotation);
  74. ambinaryRotation.quaternion_array = new Quaternion[this.total_frame_];
  75. for (int i = 1; i <= this.total_frame_; i++)
  76. {
  77. int num = i - 1;
  78. ambinaryRotation.quaternion_array[num] = amrotationTrack.getRotationAtFrame((float)i);
  79. }
  80. return ambinaryRotation;
  81. }
  82. public AMBinaryProperty CreateBinaryProperty(AMTrack track_data)
  83. {
  84. if (!AMBinaryProperty.CheckType(track_data))
  85. {
  86. return null;
  87. }
  88. AMPropertyTrack prop_track = track_data as AMPropertyTrack;
  89. AMBinaryProperty ambinaryProperty = new AMBinaryProperty();
  90. ambinaryProperty.component_name = prop_track.getComponentName();
  91. ambinaryProperty.property_name = prop_track.propertyName;
  92. ambinaryProperty.value_type = (AMPropertyTrack.ValueType)prop_track.valueType;
  93. this.CreateBaseData(track_data, prop_track.obj, ambinaryProperty);
  94. if (prop_track.valueType == 2)
  95. {
  96. this.CreateBinaryPropertyGeneric<float>(ref ambinaryProperty.float_val_array, prop_track, () => (float)prop_track.getPropertyValueNumeric());
  97. }
  98. else if (prop_track.valueType == 0)
  99. {
  100. this.CreateBinaryPropertyGeneric<int>(ref ambinaryProperty.int_val_array, prop_track, () => (int)prop_track.getPropertyValueNumeric());
  101. }
  102. else if (prop_track.valueType == 5)
  103. {
  104. this.CreateBinaryPropertyGeneric<Vector3>(ref ambinaryProperty.vec3_val_array, prop_track, new Func<Vector3>(prop_track.getPropertyValueVector3));
  105. }
  106. else if (prop_track.valueType == 6)
  107. {
  108. this.CreateBinaryPropertyGeneric<Color>(ref ambinaryProperty.color_val_array, prop_track, new Func<Color>(prop_track.getPropertyValueColor));
  109. }
  110. return ambinaryProperty;
  111. }
  112. private void CreateBinaryPropertyGeneric<T>(ref T[] ref_array, AMPropertyTrack prop_track, Func<T> get_prop_func) where T : struct
  113. {
  114. ref_array = new T[this.total_frame_];
  115. for (int i = 1; i <= this.total_frame_; i++)
  116. {
  117. prop_track.previewFrame((float)i, false);
  118. int num = i - 1;
  119. ref_array[num] = get_prop_func();
  120. }
  121. }
  122. public AMBinaryMethod CreateBinaryMethod(AMTrack track_data)
  123. {
  124. if (!AMBinaryMethod.CheckType(track_data))
  125. {
  126. return null;
  127. }
  128. AMBinaryMethod ambinaryMethod = new AMBinaryMethod();
  129. List<AMBinaryMethod.MethodData> list = new List<AMBinaryMethod.MethodData>();
  130. AMEventTrack ameventTrack = track_data as AMEventTrack;
  131. List<AMAction> cache = ameventTrack.cache;
  132. this.CreateBaseData(track_data, ameventTrack.obj, ambinaryMethod);
  133. for (int i = 0; i < cache.Count; i++)
  134. {
  135. AMEventAction ameventAction = cache[i] as AMEventAction;
  136. list.Add(new AMBinaryMethod.MethodData
  137. {
  138. start_flame = ameventAction.startFrame - 1,
  139. component_name = ameventAction.methodInfo.DeclaringType.Name,
  140. method_name = ameventAction.methodName,
  141. param_list = ameventAction.parameters
  142. });
  143. }
  144. ambinaryMethod.method_data_array = list.ToArray();
  145. return ambinaryMethod;
  146. }
  147. private void CreateBaseData(AMTrack track_data, GameObject target_obj, AMBinaryDataBaseObject write_data)
  148. {
  149. write_data.obj = target_obj;
  150. write_data.object_tree_path = UTY.GetObjectTreePath(target_obj);
  151. write_data.total_frame = this.total_frame_;
  152. write_data.track_id = track_data.id;
  153. }
  154. private AnimatorData anime_data_;
  155. private int total_frame_;
  156. private int frame_rate_;
  157. }