PhotoCustomObjectSphere.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. using System;
  2. using System.IO;
  3. using UnityEngine;
  4. public class PhotoCustomObjectSphere : BasePhotoCustomObject
  5. {
  6. public override byte[] imageBinarys
  7. {
  8. get
  9. {
  10. return this.imageBinary;
  11. }
  12. }
  13. public override BasePhotoCustomObject.Type type
  14. {
  15. get
  16. {
  17. return BasePhotoCustomObject.Type.Sphere;
  18. }
  19. }
  20. public override Texture2D mainTexture
  21. {
  22. get
  23. {
  24. return this.texture;
  25. }
  26. }
  27. public bool isAlphaTexture
  28. {
  29. get
  30. {
  31. return this.isAlphaTexture_;
  32. }
  33. }
  34. public PhotoCustomObjectSphere.RenderingType renderingType
  35. {
  36. get
  37. {
  38. return this.renderingType_;
  39. }
  40. set
  41. {
  42. this.renderingType_ = value;
  43. if (this.material == null)
  44. {
  45. return;
  46. }
  47. if (this.renderingType_ == PhotoCustomObjectSphere.RenderingType.TwoSides)
  48. {
  49. this.material.SetFloat("_Cull", 0f);
  50. }
  51. else if (this.renderingType_ == PhotoCustomObjectSphere.RenderingType.Front)
  52. {
  53. this.material.SetFloat("_Cull", 2f);
  54. }
  55. else if (this.renderingType_ == PhotoCustomObjectSphere.RenderingType.Back)
  56. {
  57. this.material.SetFloat("_Cull", 1f);
  58. }
  59. }
  60. }
  61. public override void Awake()
  62. {
  63. base.Awake();
  64. this.material = new Material(Shader.Find("CM3D2/Unlit_Texture_Photo_MyObject"));
  65. foreach (Renderer renderer in this.texRenderers)
  66. {
  67. if (renderer != null)
  68. {
  69. renderer.material = this.material;
  70. }
  71. }
  72. this.renderingType = PhotoCustomObjectSphere.RenderingType.TwoSides;
  73. this.material.SetFloat("_ZWrite", 1f);
  74. this.material.renderQueue = 2980;
  75. this.scaleTransform.gameObject.SetActive(false);
  76. }
  77. public override void OnDestroy()
  78. {
  79. base.OnDestroy();
  80. this.ReleaseTexture();
  81. }
  82. public override void Clear()
  83. {
  84. base.Clear();
  85. if (this.material != null && this.material.mainTexture != null)
  86. {
  87. this.material.mainTexture = null;
  88. }
  89. if (this.scaleTransform != null)
  90. {
  91. this.scaleTransform.gameObject.SetActive(false);
  92. }
  93. this.ReleaseTexture();
  94. }
  95. protected override bool CreateTexture2D(byte[] imageBinary)
  96. {
  97. this.ReleaseTexture();
  98. this.texture = new Texture2D(16, 16);
  99. if (!this.texture.LoadImage(imageBinary))
  100. {
  101. this.ReleaseTexture();
  102. this.scaleTransform.gameObject.SetActive(false);
  103. return false;
  104. }
  105. if (this.alphaCheck)
  106. {
  107. this.isAlphaTexture_ = false;
  108. Color[] pixels = this.texture.GetPixels();
  109. for (int i = 0; i < pixels.Length; i++)
  110. {
  111. if (pixels[i].a != 1f)
  112. {
  113. this.isAlphaTexture_ = true;
  114. break;
  115. }
  116. }
  117. }
  118. this.material.SetFloat("_ZWrite", (float)((!this.isAlphaTexture_) ? 1 : 0));
  119. this.imageBinary = new byte[imageBinary.Length];
  120. Array.Copy(imageBinary, this.imageBinary, imageBinary.Length);
  121. this.material.mainTexture = this.texture;
  122. this.scaleTransform.gameObject.SetActive(true);
  123. return true;
  124. }
  125. protected override void SetTextureColor(Color setColor)
  126. {
  127. if (this.material != null)
  128. {
  129. this.material.SetColor("_Color", setColor);
  130. }
  131. }
  132. protected override void SetScale(float scale)
  133. {
  134. if (this.scaleTransform != null)
  135. {
  136. this.scaleTransform.localScale = new Vector3(scale, scale, scale);
  137. }
  138. }
  139. protected override void OnSerialize(BinaryWriter writer)
  140. {
  141. writer.Write(1002);
  142. writer.Write(this.renderingType.ToString());
  143. writer.Write(this.isAlphaTexture_);
  144. writer.Write(this.imageBinary.Length);
  145. writer.Write(this.imageBinary);
  146. }
  147. protected override bool OnDesilialize(int gameVersion, BasePhotoCustomObject.Type type, BinaryReader reader)
  148. {
  149. if (type != BasePhotoCustomObject.Type.Sphere)
  150. {
  151. return false;
  152. }
  153. int num = 1000;
  154. if (1120 < gameVersion)
  155. {
  156. num = reader.ReadInt32();
  157. }
  158. if (1002 <= num)
  159. {
  160. this.renderingType = (PhotoCustomObjectSphere.RenderingType)Enum.Parse(typeof(PhotoCustomObjectSphere.RenderingType), reader.ReadString());
  161. }
  162. else
  163. {
  164. this.renderingType = PhotoCustomObjectSphere.RenderingType.TwoSides;
  165. }
  166. if (1001 <= num)
  167. {
  168. this.isAlphaTexture_ = reader.ReadBoolean();
  169. this.alphaCheck = false;
  170. }
  171. int count = reader.ReadInt32();
  172. base.SetTexture(reader.ReadBytes(count));
  173. this.alphaCheck = true;
  174. return true;
  175. }
  176. private void ReleaseTexture()
  177. {
  178. if (this.texture != null)
  179. {
  180. UnityEngine.Object.DestroyImmediate(this.texture);
  181. this.texture = null;
  182. this.imageBinary = null;
  183. }
  184. }
  185. [SerializeField]
  186. protected Transform scaleTransform;
  187. [SerializeField]
  188. protected Renderer[] texRenderers;
  189. protected Texture2D texture;
  190. protected Material material;
  191. protected byte[] imageBinary = new byte[0];
  192. protected bool isAlphaTexture_;
  193. private bool alphaCheck = true;
  194. private PhotoCustomObjectSphere.RenderingType renderingType_;
  195. public enum RenderingType
  196. {
  197. TwoSides,
  198. Front,
  199. Back
  200. }
  201. }