PropManager.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using UnityEngine;
  5. using UnityEngine.Rendering;
  6. namespace COM3D2.MeidoPhotoStudio.Plugin
  7. {
  8. internal class PropManager
  9. {
  10. private List<DragDogu> doguList = new List<DragDogu>();
  11. private DragType dragTypeOld = DragType.None;
  12. private DragType currentDragType = DragType.None;
  13. private bool showGizmos = false;
  14. enum DragType
  15. {
  16. None, Move, Rotate, Scale, Delete, Other
  17. }
  18. public void Activate() { }
  19. public void Deactivate()
  20. {
  21. foreach (DragDogu dogu in doguList)
  22. {
  23. GameObject.Destroy(dogu.gameObject);
  24. }
  25. doguList.Clear();
  26. }
  27. public void Update()
  28. {
  29. if (Input.GetKeyDown(KeyCode.Space))
  30. {
  31. showGizmos = !showGizmos;
  32. currentDragType = dragTypeOld = DragType.None;
  33. UpdateDragType();
  34. }
  35. if (Input.GetKey(KeyCode.Z) || Input.GetKey(KeyCode.X) || Input.GetKey(KeyCode.C)
  36. || Input.GetKey(KeyCode.D)
  37. )
  38. {
  39. currentDragType = DragType.Other;
  40. }
  41. else
  42. {
  43. currentDragType = DragType.None;
  44. }
  45. if (currentDragType != dragTypeOld) UpdateDragType();
  46. dragTypeOld = currentDragType;
  47. }
  48. private void UpdateDragType()
  49. {
  50. bool dragPointActive = currentDragType == DragType.Other;
  51. foreach (DragDogu dogu in doguList)
  52. {
  53. dogu.SetDragProp(showGizmos, dragPointActive, dragPointActive);
  54. }
  55. }
  56. private GameObject GetDeploymentObject()
  57. {
  58. GameObject go = GameObject.Find("Deployment Object Parent");
  59. if (go == null) go = new GameObject("Deployment Object Parent");
  60. return go;
  61. }
  62. public void SpawnObject(string assetName)
  63. {
  64. // TODO: Add a couple more things to ignore list
  65. GameObject dogu = null;
  66. if (assetName.StartsWith("mirror"))
  67. {
  68. Material mirrorMaterial = new Material(Shader.Find("Mirror"));
  69. dogu = GameObject.CreatePrimitive(PrimitiveType.Plane);
  70. Renderer mirrorRenderer = dogu.GetComponent<Renderer>();
  71. mirrorRenderer.material = mirrorMaterial;
  72. mirrorRenderer.enabled = true;
  73. MirrorReflection2 mirrorReflection = dogu.AddComponent<MirrorReflection2>();
  74. mirrorReflection.m_TextureSize = 2048;
  75. Vector3 localPosition = new Vector3(0f, 0.96f, 0f);
  76. dogu.transform.Rotate(dogu.transform.right, 90f);
  77. switch (assetName)
  78. {
  79. case "mirror1":
  80. dogu.transform.localScale = new Vector3(0.2f, 0.4f, 0.2f);
  81. break;
  82. case "mirror2":
  83. dogu.transform.localScale = new Vector3(0.1f, 0.4f, 0.2f);
  84. break;
  85. case "mirror3":
  86. localPosition.y = 0.85f;
  87. dogu.transform.localScale = new Vector3(0.03f, 0.18f, 0.124f);
  88. break;
  89. }
  90. dogu.transform.localPosition = localPosition;
  91. }
  92. else if (assetName.IndexOf(':') >= 0)
  93. {
  94. string[] assetParts = assetName.Split(':');
  95. GameObject obj = GameMain.Instance.BgMgr.CreateAssetBundle(assetParts[0]);
  96. if (obj == null)
  97. {
  98. obj = Resources.Load("BG/" + assetParts[0]) as GameObject;
  99. }
  100. GameObject bg = GameObject.Instantiate(obj);
  101. int num = int.Parse(assetParts[1]);
  102. dogu = bg.transform.GetChild(num).gameObject;
  103. dogu.transform.SetParent(null);
  104. GameObject.Destroy(bg);
  105. bg.SetActive(false);
  106. }
  107. else
  108. {
  109. GameObject obj = GameMain.Instance.BgMgr.CreateAssetBundle(assetName);
  110. if (obj == null) obj = Resources.Load("Prefab/" + assetName) as GameObject;
  111. dogu = GameObject.Instantiate(obj) as GameObject;
  112. dogu.transform.localPosition = Vector3.zero;
  113. MeshRenderer[] meshRenderers = dogu.GetComponentsInChildren<MeshRenderer>();
  114. for (int i = 0; i < meshRenderers.Length; i++)
  115. {
  116. if (meshRenderers[i] != null)
  117. {
  118. meshRenderers[i].shadowCastingMode = ShadowCastingMode.Off;
  119. }
  120. }
  121. Collider collider = dogu.transform.GetComponent<Collider>();
  122. if (collider != null) collider.enabled = false;
  123. foreach (Transform transform in dogu.transform)
  124. {
  125. collider = transform.GetComponent<Collider>();
  126. if (collider != null)
  127. {
  128. collider.enabled = false;
  129. }
  130. }
  131. #region particle system experiment
  132. // if (asset.StartsWith("Particle/"))
  133. // {
  134. // ParticleSystem particleSystem = go.GetComponent<ParticleSystem>();
  135. // if (particleSystem != null)
  136. // {
  137. // ParticleSystem.MainModule main;
  138. // main = particleSystem.main;
  139. // main.loop = true;
  140. // main.duration = Mathf.Infinity;
  141. // ParticleSystem[] particleSystems = particleSystem.GetComponents<ParticleSystem>();
  142. // foreach (ParticleSystem part in particleSystems)
  143. // {
  144. // ParticleSystem.EmissionModule emissionModule = part.emission;
  145. // ParticleSystem.Burst[] bursts = new ParticleSystem.Burst[emissionModule.burstCount];
  146. // emissionModule.GetBursts(bursts);
  147. // for (int i = 0; i < bursts.Length; i++)
  148. // {
  149. // bursts[i].cycleCount = Int32.MaxValue;
  150. // }
  151. // emissionModule.SetBursts(bursts);
  152. // main = part.main;
  153. // main.loop = true;
  154. // main.duration = Mathf.Infinity;
  155. // }
  156. // }
  157. // }
  158. #endregion
  159. }
  160. if (dogu != null)
  161. {
  162. // TODO: Figure out why some props aren't centred properly
  163. // Doesn't happen in MM but even after copy pasting the code, it doesn't work :/
  164. GameObject deploymentObject = GetDeploymentObject();
  165. GameObject finalDogu = new GameObject();
  166. dogu.transform.SetParent(finalDogu.transform, true);
  167. finalDogu.transform.SetParent(deploymentObject.transform, false);
  168. finalDogu.transform.position = new Vector3(0f, 0f, 0.5f);
  169. GameObject dragPoint = BaseDrag.MakeDragPoint(
  170. PrimitiveType.Cube, Vector3.one * 0.12f, BaseDrag.LightBlue
  171. );
  172. DragDogu dragDogu = dragPoint.AddComponent<DragDogu>();
  173. dragDogu.Initialize(finalDogu);
  174. dragDogu.Delete += (s, a) => DeleteDogu();
  175. dragDogu.SetDragProp(showGizmos, false, false);
  176. doguList.Add(dragDogu);
  177. }
  178. }
  179. private void DeleteDogu()
  180. {
  181. doguList.RemoveAll(dragDogu =>
  182. {
  183. if (dragDogu.DeleteMe)
  184. {
  185. GameObject.Destroy(dragDogu.gameObject);
  186. return true;
  187. }
  188. return false;
  189. }
  190. );
  191. }
  192. }
  193. }