PhotoCustomObjectPlane.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. using System;
  2. using System.IO;
  3. using UnityEngine;
  4. public class PhotoCustomObjectPlane : 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.Plane;
  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 PhotoCustomObjectPlane.BackRenderingType backRenderingType
  35. {
  36. get
  37. {
  38. return this.backRenderingType_;
  39. }
  40. set
  41. {
  42. this.backRenderingType_ = value;
  43. if (this.material == null)
  44. {
  45. return;
  46. }
  47. if (this.backRenderingType_ == PhotoCustomObjectPlane.BackRenderingType.Mirror)
  48. {
  49. this.material.SetFloat("_Cull", 0f);
  50. this.subTextureObject.SetActive(false);
  51. }
  52. else if (this.backRenderingType_ == PhotoCustomObjectPlane.BackRenderingType.Flip)
  53. {
  54. this.material.SetFloat("_Cull", 2f);
  55. this.subTextureObject.SetActive(true);
  56. }
  57. else if (this.backRenderingType_ == PhotoCustomObjectPlane.BackRenderingType.None)
  58. {
  59. this.material.SetFloat("_Cull", 2f);
  60. this.subTextureObject.SetActive(false);
  61. }
  62. }
  63. }
  64. public override void Awake()
  65. {
  66. base.Awake();
  67. this.material = new Material(Shader.Find("CM3D2/Unlit_Texture_Photo_MyObject"));
  68. foreach (Renderer renderer in this.texRenderers)
  69. {
  70. if (renderer != null)
  71. {
  72. renderer.material = this.material;
  73. }
  74. }
  75. this.subTextureObject.SetActive(false);
  76. this.scaleTransform.gameObject.SetActive(false);
  77. }
  78. public override void OnDestroy()
  79. {
  80. base.OnDestroy();
  81. this.ReleaseTexture();
  82. }
  83. public override void Clear()
  84. {
  85. base.Clear();
  86. if (this.material != null && this.material.mainTexture != null)
  87. {
  88. this.material.mainTexture = null;
  89. }
  90. if (this.scaleTransform != null)
  91. {
  92. this.scaleTransform.gameObject.SetActive(false);
  93. }
  94. this.ReleaseTexture();
  95. }
  96. protected override bool CreateTexture2D(byte[] imageBinary)
  97. {
  98. this.ReleaseTexture();
  99. this.texture = new Texture2D(16, 16);
  100. if (!this.texture.LoadImage(imageBinary))
  101. {
  102. this.ReleaseTexture();
  103. this.scaleTransform.gameObject.SetActive(false);
  104. return false;
  105. }
  106. if (this.alphaCheck)
  107. {
  108. this.isAlphaTexture_ = false;
  109. Color[] pixels = this.texture.GetPixels();
  110. for (int i = 0; i < pixels.Length; i++)
  111. {
  112. if (pixels[i].a != 1f)
  113. {
  114. this.isAlphaTexture_ = true;
  115. break;
  116. }
  117. }
  118. }
  119. this.material.SetFloat("_ZWrite", (float)((!this.isAlphaTexture_) ? 1 : 0));
  120. this.imageBinary = new byte[imageBinary.Length];
  121. Array.Copy(imageBinary, this.imageBinary, imageBinary.Length);
  122. int width = this.texture.width;
  123. int height = this.texture.height;
  124. float num = (float)Math.Min(width, height) / (float)Math.Max(width, height);
  125. Transform transform = this.texRenderers[0].transform;
  126. Vector3 one = Vector3.one;
  127. if (height < width)
  128. {
  129. one.z = num;
  130. }
  131. else
  132. {
  133. one.x = num;
  134. }
  135. transform.localScale = one;
  136. this.material.mainTexture = this.texture;
  137. this.scaleTransform.gameObject.SetActive(true);
  138. return true;
  139. }
  140. protected override void SetTextureColor(Color setColor)
  141. {
  142. if (this.material != null)
  143. {
  144. this.material.SetColor("_Color", setColor);
  145. }
  146. }
  147. protected override void SetScale(float scale)
  148. {
  149. if (this.scaleTransform != null)
  150. {
  151. this.scaleTransform.localScale = new Vector3(scale, scale, scale);
  152. }
  153. }
  154. protected override void OnSerialize(BinaryWriter writer)
  155. {
  156. writer.Write(1000);
  157. writer.Write(this.backRenderingType_.ToString());
  158. writer.Write(this.isAlphaTexture_);
  159. writer.Write(this.imageBinary.Length);
  160. writer.Write(this.imageBinary);
  161. }
  162. protected override bool OnDesilialize(int gameVersion, BasePhotoCustomObject.Type type, BinaryReader reader)
  163. {
  164. if (type != BasePhotoCustomObject.Type.Plane)
  165. {
  166. return false;
  167. }
  168. if (1120 < gameVersion)
  169. {
  170. int num = reader.ReadInt32();
  171. }
  172. this.backRenderingType = (PhotoCustomObjectPlane.BackRenderingType)Enum.Parse(typeof(PhotoCustomObjectPlane.BackRenderingType), reader.ReadString());
  173. this.isAlphaTexture_ = reader.ReadBoolean();
  174. int count = reader.ReadInt32();
  175. this.alphaCheck = false;
  176. base.SetTexture(reader.ReadBytes(count));
  177. this.alphaCheck = true;
  178. return true;
  179. }
  180. private void ReleaseTexture()
  181. {
  182. if (this.texture != null)
  183. {
  184. UnityEngine.Object.DestroyImmediate(this.texture);
  185. this.texture = null;
  186. this.imageBinary = null;
  187. }
  188. }
  189. [SerializeField]
  190. protected Transform scaleTransform;
  191. [SerializeField]
  192. protected GameObject subTextureObject;
  193. [SerializeField]
  194. protected Renderer[] texRenderers;
  195. protected Texture2D texture;
  196. protected Material material;
  197. protected byte[] imageBinary = new byte[0];
  198. protected PhotoCustomObjectPlane.BackRenderingType backRenderingType_;
  199. protected bool isAlphaTexture_;
  200. private bool alphaCheck = true;
  201. public enum BackRenderingType
  202. {
  203. Mirror,
  204. Flip,
  205. None
  206. }
  207. }