BasePhotoCustomObject.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using UnityEngine;
  5. using wf;
  6. public abstract class BasePhotoCustomObject : MonoBehaviour, IComparable<BasePhotoCustomObject>
  7. {
  8. public static BasePhotoCustomObject CreatePrefab(GameObject parent, BasePhotoCustomObject.Type createType)
  9. {
  10. if (createType == BasePhotoCustomObject.Type.Plane)
  11. {
  12. GameObject gameObject = Utility.CreatePrefab(parent, "ScenePhotoMode/CustomObject/Plane", false);
  13. return gameObject.GetComponent<BasePhotoCustomObject>();
  14. }
  15. if (createType == BasePhotoCustomObject.Type.Sphere)
  16. {
  17. GameObject gameObject2 = Utility.CreatePrefab(parent, "ScenePhotoMode/CustomObject/Sphere", false);
  18. return gameObject2.GetComponent<BasePhotoCustomObject>();
  19. }
  20. return null;
  21. }
  22. public static BasePhotoCustomObject InstantiateFromFile(GameObject parent, string fileFullPath)
  23. {
  24. KeyValuePair<bool, BasePhotoCustomObject.Type> typeFromFile = BasePhotoCustomObject.GetTypeFromFile(fileFullPath);
  25. if (!typeFromFile.Key)
  26. {
  27. return null;
  28. }
  29. BasePhotoCustomObject.Type value = typeFromFile.Value;
  30. BasePhotoCustomObject basePhotoCustomObject = BasePhotoCustomObject.CreatePrefab(parent, value);
  31. basePhotoCustomObject.Desilialize(File.ReadAllBytes(fileFullPath));
  32. return basePhotoCustomObject;
  33. }
  34. public static KeyValuePair<bool, BasePhotoCustomObject.Type> GetTypeFromFile(string fileFullPath)
  35. {
  36. bool key = false;
  37. if (string.IsNullOrEmpty(fileFullPath) || !File.Exists(fileFullPath))
  38. {
  39. return new KeyValuePair<bool, BasePhotoCustomObject.Type>(key, BasePhotoCustomObject.Type.Plane);
  40. }
  41. KeyValuePair<bool, BasePhotoCustomObject.Type> result;
  42. using (FileStream fileStream = new FileStream(fileFullPath, FileMode.Open, FileAccess.Read))
  43. {
  44. using (BinaryReader binaryReader = new BinaryReader(fileStream))
  45. {
  46. string a = binaryReader.ReadString();
  47. if (a != "COM3D2_PHOTO_CUSTOM_OBJECT")
  48. {
  49. result = new KeyValuePair<bool, BasePhotoCustomObject.Type>(key, BasePhotoCustomObject.Type.Plane);
  50. }
  51. else
  52. {
  53. int num = binaryReader.ReadInt32();
  54. BasePhotoCustomObject.Type value = (BasePhotoCustomObject.Type)Enum.Parse(typeof(BasePhotoCustomObject.Type), binaryReader.ReadString());
  55. key = true;
  56. result = new KeyValuePair<bool, BasePhotoCustomObject.Type>(key, value);
  57. }
  58. }
  59. }
  60. return result;
  61. }
  62. public abstract byte[] imageBinarys { get; }
  63. public abstract BasePhotoCustomObject.Type type { get; }
  64. public abstract Texture2D mainTexture { get; }
  65. public float scale
  66. {
  67. get
  68. {
  69. return this.scale_;
  70. }
  71. set
  72. {
  73. this.scale_ = value;
  74. this.SetScale(this.scale_);
  75. }
  76. }
  77. public bool enabledTextureColor
  78. {
  79. get
  80. {
  81. return this.enabledTextureColor_;
  82. }
  83. set
  84. {
  85. this.enabledTextureColor_ = value;
  86. this.SetTextureColor((!value) ? Color.white : this.textureColor);
  87. }
  88. }
  89. public Color textureColor
  90. {
  91. get
  92. {
  93. return this.textureColor_;
  94. }
  95. set
  96. {
  97. bool flag = this.textureColor_ != value && this.enabledTextureColor;
  98. this.textureColor_ = value;
  99. if (flag)
  100. {
  101. this.enabledTextureColor = true;
  102. }
  103. }
  104. }
  105. public bool visible
  106. {
  107. get
  108. {
  109. return base.gameObject.activeSelf;
  110. }
  111. set
  112. {
  113. base.gameObject.SetActive(value);
  114. }
  115. }
  116. public virtual void Awake()
  117. {
  118. this.Clear();
  119. }
  120. public virtual void OnDestroy()
  121. {
  122. }
  123. public bool SetTexture(string imageFilePath)
  124. {
  125. if (string.IsNullOrEmpty(imageFilePath))
  126. {
  127. return false;
  128. }
  129. if (!File.Exists(imageFilePath))
  130. {
  131. return false;
  132. }
  133. byte[] array = File.ReadAllBytes(imageFilePath);
  134. return array != null && array.Length != 0 && this.SetTexture(array);
  135. }
  136. public bool SetTexture(byte[] imageFileBinary)
  137. {
  138. return imageFileBinary != null && imageFileBinary.Length > 0 && this.CreateTexture2D(imageFileBinary);
  139. }
  140. public virtual void Clear()
  141. {
  142. this.scale = 1f;
  143. this.enabledTextureColor = true;
  144. this.textureColor = Color.white;
  145. }
  146. public virtual int CompareTo(BasePhotoCustomObject comp_obj)
  147. {
  148. return ((int)comp_obj.type).CompareTo((int)this.type);
  149. }
  150. public byte[] Serialize()
  151. {
  152. byte[] result = null;
  153. NDebug.Assert(this.mainTexture != null, "BasePhotoCustomObject::Serialize error.\nmainTextureがnullです");
  154. using (MemoryStream memoryStream = new MemoryStream())
  155. {
  156. using (BinaryWriter binaryWriter = new BinaryWriter(memoryStream))
  157. {
  158. binaryWriter.Write("COM3D2_PHOTO_CUSTOM_OBJECT");
  159. binaryWriter.Write(1130);
  160. binaryWriter.Write(this.type.ToString());
  161. binaryWriter.Write(this.scale_);
  162. binaryWriter.Write(this.enabledTextureColor_);
  163. binaryWriter.Write(this.textureColor_.a);
  164. binaryWriter.Write(this.textureColor_.r);
  165. binaryWriter.Write(this.textureColor_.g);
  166. binaryWriter.Write(this.textureColor_.b);
  167. this.OnSerialize(binaryWriter);
  168. result = memoryStream.ToArray();
  169. }
  170. }
  171. return result;
  172. }
  173. public bool Desilialize(byte[] binary)
  174. {
  175. this.Clear();
  176. using (MemoryStream memoryStream = new MemoryStream(binary))
  177. {
  178. using (BinaryReader binaryReader = new BinaryReader(memoryStream))
  179. {
  180. string a = binaryReader.ReadString();
  181. NDebug.Assert(a == "COM3D2_PHOTO_CUSTOM_OBJECT", "BasePhotoCustomObject::Desilialize error.\nヘッダーが不正です");
  182. int gameVersion = binaryReader.ReadInt32();
  183. BasePhotoCustomObject.Type type = (BasePhotoCustomObject.Type)Enum.Parse(typeof(BasePhotoCustomObject.Type), binaryReader.ReadString());
  184. this.scale = binaryReader.ReadSingle();
  185. this.enabledTextureColor = binaryReader.ReadBoolean();
  186. this.textureColor = new Color
  187. {
  188. a = binaryReader.ReadSingle(),
  189. r = binaryReader.ReadSingle(),
  190. g = binaryReader.ReadSingle(),
  191. b = binaryReader.ReadSingle()
  192. };
  193. this.OnDesilialize(gameVersion, type, binaryReader);
  194. }
  195. }
  196. return true;
  197. }
  198. protected abstract void OnSerialize(BinaryWriter writer);
  199. protected abstract bool OnDesilialize(int gameVersion, BasePhotoCustomObject.Type type, BinaryReader reader);
  200. protected abstract bool CreateTexture2D(byte[] imageFileBinary);
  201. protected abstract void SetTextureColor(Color setColor);
  202. protected abstract void SetScale(float scale);
  203. protected float scale_;
  204. protected bool enabledTextureColor_;
  205. protected Color textureColor_;
  206. public enum Type
  207. {
  208. Plane,
  209. Sphere
  210. }
  211. }