PropManager.cs 8.1 KB

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