PhotoCustomObjectSphere.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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 override void Awake()
  35. {
  36. base.Awake();
  37. this.material = new Material(Shader.Find("CM3D2/Unlit_Texture_Photo_MyObject"));
  38. foreach (Renderer renderer in this.texRenderers)
  39. {
  40. if (renderer != null)
  41. {
  42. renderer.material = this.material;
  43. }
  44. }
  45. this.material.SetFloat("_Cull", 0f);
  46. this.material.SetFloat("_ZWrite", 1f);
  47. this.material.renderQueue = 2980;
  48. this.scaleTransform.gameObject.SetActive(false);
  49. }
  50. public override void OnDestroy()
  51. {
  52. base.OnDestroy();
  53. this.ReleaseTexture();
  54. }
  55. public override void Clear()
  56. {
  57. base.Clear();
  58. if (this.material != null && this.material.mainTexture != null)
  59. {
  60. this.material.mainTexture = null;
  61. }
  62. if (this.scaleTransform != null)
  63. {
  64. this.scaleTransform.gameObject.SetActive(false);
  65. }
  66. this.ReleaseTexture();
  67. }
  68. protected override bool CreateTexture2D(byte[] imageBinary)
  69. {
  70. this.ReleaseTexture();
  71. this.texture = new Texture2D(16, 16);
  72. if (!this.texture.LoadImage(imageBinary))
  73. {
  74. this.ReleaseTexture();
  75. this.scaleTransform.gameObject.SetActive(false);
  76. return false;
  77. }
  78. if (this.alphaCheck)
  79. {
  80. this.isAlphaTexture_ = false;
  81. Color[] pixels = this.texture.GetPixels();
  82. for (int i = 0; i < pixels.Length; i++)
  83. {
  84. if (pixels[i].a != 1f)
  85. {
  86. this.isAlphaTexture_ = true;
  87. break;
  88. }
  89. }
  90. }
  91. this.material.SetFloat("_ZWrite", (float)((!this.isAlphaTexture_) ? 1 : 0));
  92. this.imageBinary = new byte[imageBinary.Length];
  93. Array.Copy(imageBinary, this.imageBinary, imageBinary.Length);
  94. this.material.mainTexture = this.texture;
  95. this.scaleTransform.gameObject.SetActive(true);
  96. return true;
  97. }
  98. protected override void SetTextureColor(Color setColor)
  99. {
  100. if (this.material != null)
  101. {
  102. this.material.SetColor("_Color", setColor);
  103. }
  104. }
  105. protected override void SetScale(float scale)
  106. {
  107. if (this.scaleTransform != null)
  108. {
  109. this.scaleTransform.localScale = new Vector3(scale, scale, scale);
  110. }
  111. }
  112. protected override void OnSerialize(BinaryWriter writer)
  113. {
  114. writer.Write(1001);
  115. writer.Write(this.isAlphaTexture_);
  116. writer.Write(this.imageBinary.Length);
  117. writer.Write(this.imageBinary);
  118. }
  119. protected override bool OnDesilialize(int gameVersion, BasePhotoCustomObject.Type type, BinaryReader reader)
  120. {
  121. if (type != BasePhotoCustomObject.Type.Sphere)
  122. {
  123. return false;
  124. }
  125. int num = 1000;
  126. if (1120 < gameVersion)
  127. {
  128. num = reader.ReadInt32();
  129. }
  130. if (1001 <= num)
  131. {
  132. this.isAlphaTexture_ = reader.ReadBoolean();
  133. this.alphaCheck = false;
  134. }
  135. int count = reader.ReadInt32();
  136. base.SetTexture(reader.ReadBytes(count));
  137. this.alphaCheck = true;
  138. return true;
  139. }
  140. private void ReleaseTexture()
  141. {
  142. if (this.texture != null)
  143. {
  144. UnityEngine.Object.DestroyImmediate(this.texture);
  145. this.texture = null;
  146. this.imageBinary = null;
  147. }
  148. }
  149. [SerializeField]
  150. protected Transform scaleTransform;
  151. [SerializeField]
  152. protected Renderer[] texRenderers;
  153. protected Texture2D texture;
  154. protected Material material;
  155. protected byte[] imageBinary = new byte[0];
  156. protected bool isAlphaTexture_;
  157. private bool alphaCheck = true;
  158. }