自己跟着编写(目前为半成品)

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEditor;
  5. public class ModifyParticleSystemBasicSetting : MonoBehaviour
  6. {
  7. /// <summary>
  8. /// 属性部分
  9. /// </summary>
  10. [Header("设置ScalingMode为Hieratchy以应用父级缩放")]
  11. public bool setScalingMode = false;
  12. [Header("未使用拖尾功能的,清除Trail材质引用")]
  13. public bool isTrailmat = false;
  14. [Header("统一Order in Layer为指定值")]
  15. public bool isSetOrderInLayer = false;
  16. public int settingValueOfOIL = 0;
  17. [Header("是否去除接收和投射阴影")]
  18. public bool isShadow;
  19. /// <summary>
  20. /// 临时判断部分
  21. /// </summary>
  22. /// <summary>
  23. /// 存储部分
  24. /// </summary>
  25. GameObject[] addGameObjects = new GameObject[0];
  26. List<ParticleSystem> particles = new List<ParticleSystem>();
  27. List<ParticleSystemRenderer> particleRenders = new List<ParticleSystemRenderer>();
  28. /// <summary>
  29. /// 将所有选中物体的
  30. /// </summary>
  31. [ContextMenu("添加选中的物体")]
  32. void AddSelectedObject()
  33. {
  34. addGameObjects = Selection.gameObjects;
  35. for (int i = 0; i < addGameObjects.Length; i++)
  36. {
  37. ParticleSystem[] temParticles = new ParticleSystem[0];
  38. ParticleSystemRenderer[] temParticlesRenders = new ParticleSystemRenderer[0];
  39. temParticles = addGameObjects[i].GetComponentsInChildren<ParticleSystem>(true);
  40. temParticlesRenders = addGameObjects[i].GetComponentsInChildren<ParticleSystemRenderer>(true);
  41. for (int j = 0; j <temParticles.Length; j++)
  42. {
  43. particles.Add(temParticles[j]);
  44. particleRenders.Add(temParticlesRenders[j]);
  45. }
  46. }
  47. Debug.LogFormat("选择物体:{0}个,粒子系统有:{1}个", addGameObjects.Length, particles.Count);
  48. }
  49. [ContextMenu("开始修改")]
  50. void StartModify()
  51. {
  52. for (int i = 0; i < particles.Count; i++)
  53. {
  54. //获取粒子模块
  55. ParticleSystem.MainModule mainModule = particles[i].main;
  56. ParticleSystem.EmissionModule emissionModule = particles[i].emission;
  57. ParticleSystem.NoiseModule noiseModule = particles[i].noise;
  58. ParticleSystem.LightsModule lightsModule = particles[i].lights;
  59. ParticleSystem.TrailModule trailModule = particles[i].trails;
  60. ParticleSystem.CustomDataModule customDataModule = particles[i].customData;
  61. //实际开始修改↓
  62. if(setScalingMode == true)//scalingMode设置为Hierarchy应用父级缩放
  63. {
  64. mainModule.scalingMode = ParticleSystemScalingMode.Hierarchy;
  65. }
  66. if(isTrailmat == true)//拖尾材质:勾选Trails则不修改,不勾选Trails则修改
  67. {
  68. if(trailModule.enabled == false)
  69. {
  70. particleRenders[i].trailMaterial = null;
  71. }
  72. }
  73. if(isSetOrderInLayer == true)//设置Order in Layer的值为settingValueOfOIL
  74. {
  75. particleRenders[i].sortingOrder = settingValueOfOIL;
  76. }
  77. if(isShadow == true)//设置Cast Shadows和Receive Shadows为Off
  78. {
  79. particleRenders[i].shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.Off;
  80. particleRenders[i].receiveShadows = false;
  81. }
  82. }
  83. }
  84. }

大佬编写

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEditor;
  5. using UnityEngine.UI;
  6. public class Effect_Optimize : MonoBehaviour
  7. {
  8. // 使用方法
  9. // 1:把此脚本拖拽进场景任意物体中。
  10. // 2:选好要调整得到项后,在监视面板(inspector)的右上角点击小锁 锁定监视面板。
  11. // 3:在Project面板多选你要改变的预制体。
  12. // 4:在监视面板右键脚本 选择【添加选中的物体】。
  13. // 5:在监视面板右键脚本 选择【开始优化】。
  14. // 6:优化完毕,Remove挂载的脚本,解除监视面板小锁。
  15. public string Ady521 = "请inspector面板右键脚本查看如何操作";
  16. [Header("是否限制粒子最大数量↓")]
  17. public bool isParSize = false;
  18. [Header("暴力设置:循环特效最大粒子数&非循环特效最大粒子数")]
  19. public int loopParricleMaxSize = 500;
  20. public int onceParricleMaxSize = 100;
  21. [Header("智能计算粒子峰值数(考虑原本的最大粒子,结果小于原本 则设置为峰值,大于则不)")]
  22. public bool isAuto = false;
  23. [Header("是否把粒子的ScalingMode改为Hierarchy以适应父级缩放")]
  24. public bool setScalingMode = false;
  25. [Header("勾选√ 管理粒子发射完毕后状态:无动作、隐藏物体、销毁物体、回调")]
  26. public bool isDes = false;
  27. public ParticleSystemStopAction des;
  28. [Header("对所有粒子,NoiSe模块,统一设置其精度为2DNoise")]
  29. public bool isNoise = false;
  30. [Header("检查并关闭灯光功能")]
  31. public bool isLight = false;
  32. [Header("未使用拖尾功能的,清除trail材质引用")]
  33. public bool isTrailmat = false;
  34. [Header("是否统一设置粒子在屏幕上最大大小限制")]
  35. public bool isMaxParticleSize = false;
  36. public float maxParsize = 5;
  37. [Header("是否统一把所有Order in Layer还原到以下值")]
  38. public bool isOIL = false;
  39. public int oil = 0;
  40. [Header("是否检查并关闭所有粒子的投影和光照接收")]
  41. public bool isShadow = false;
  42. [Header("下列清单仅展示已添加的物体和获取到的粒子,无需操作")]
  43. GameObject[] addgo = new GameObject[0];
  44. List<ParticleSystemRenderer> parRenders = new List<ParticleSystemRenderer>();
  45. List<ParticleSystem> pars = new List<ParticleSystem>();
  46. [ContextMenu("》添加选中的物体《")]
  47. void Add_TXGO()
  48. {
  49. addgo = Selection.gameObjects;
  50. for (int i = 0; i < addgo.Length; i++)
  51. {
  52. ParticleSystem[] linshipar = new ParticleSystem[0];
  53. ParticleSystemRenderer[] linshiparren = new ParticleSystemRenderer[0];
  54. linshipar = addgo[i].GetComponentsInChildren<ParticleSystem>(true);
  55. linshiparren = addgo[i].GetComponentsInChildren<ParticleSystemRenderer>(true);
  56. for (int a = 0; a < linshipar.Length; a++)
  57. {
  58. pars.Add(linshipar[a]);
  59. parRenders.Add(linshiparren[a]);
  60. }
  61. }
  62. Debug.Log("本次选择了" + addgo.Length + "个物体" + ",已加入清单的粒子系统的有"+ parRenders.Count+"个,请【开始优化】");
  63. }
  64. [ContextMenu("__》开始优化《__")]
  65. void YouhuaTX()
  66. {
  67. for (int i = 0; i < pars.Count; i++)
  68. {
  69. ParticleSystem.MainModule mainmodule = pars[i].main;
  70. ParticleSystem.EmissionModule emimodule = pars[i].emission;
  71. ParticleSystem.NoiseModule noiseModule = pars[i].noise;
  72. ParticleSystem.LightsModule lightsModule = pars[i].lights;
  73. ParticleSystem.TrailModule trailModule = pars[i].trails;
  74. ParticleSystem.CustomDataModule customDataModule = pars[i].customData;
  75. if(isParSize == true)
  76. {
  77. if(isAuto == true)
  78. {
  79. OnAuto(mainmodule, emimodule);
  80. }
  81. else
  82. {
  83. if (pars[i].main.loop == true)
  84. {
  85. mainmodule.maxParticles = loopParricleMaxSize;
  86. }
  87. else
  88. {
  89. mainmodule.maxParticles = onceParricleMaxSize;
  90. }
  91. if (mainmodule.maxParticles <= 1)
  92. {
  93. mainmodule.maxParticles = 1;
  94. }
  95. }
  96. }
  97. if(setScalingMode == true)
  98. {
  99. mainmodule.scalingMode = ParticleSystemScalingMode.Hierarchy;
  100. }
  101. if (isDes == true)
  102. {
  103. mainmodule.stopAction = des;
  104. }
  105. if(isNoise == true)
  106. {
  107. noiseModule.quality = ParticleSystemNoiseQuality.Medium;
  108. }
  109. if (isLight == true)
  110. {
  111. lightsModule.enabled = false;
  112. }
  113. if(isTrailmat == true)
  114. {
  115. if (trailModule.enabled == false)
  116. {
  117. parRenders[i].trailMaterial = null;
  118. }
  119. }
  120. if (isMaxParticleSize)
  121. {
  122. parRenders[i].maxParticleSize = maxParsize;
  123. }
  124. if (isOIL == true)
  125. {
  126. parRenders[i].sortingOrder = oil;
  127. }
  128. if(isShadow == true)
  129. {
  130. parRenders[i].shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.Off;
  131. parRenders[i].receiveShadows = false;
  132. }
  133. }
  134. Debug.Log("所选物体全部优化完毕,共修改了 "+ parRenders.Count+" 个,请 CTRL+S 保存修改,"+ "有疑问请右键脚本 跳转我的首页 联系我");
  135. }
  136. [ContextMenu("控制台显示文字教程")]
  137. void LogJiaoCheng()
  138. {
  139. Debug.Log("1:把此脚本拖拽进场景任意物体中。2:选好要调整得到项后,在监视面板(inspector)的右上角点击小锁 锁定监视面板。3:在Project面板多选你要改变的预制体。4:在监视面板右键脚本 选择【添加选中的物体】。5:在监视面板右键脚本 选择【开始优化】。6:优化完毕,Remove挂载的脚本,解除监视面板小锁。");
  140. }
  141. [ContextMenu("跳转到网页看图文教程")]
  142. void Bilibilitext()
  143. {
  144. Application.OpenURL("https://www.bilibili.com/video/BV1oi4y1u72z");
  145. }
  146. [ContextMenu("有疑问 点此联系我")]
  147. void Bilibili()
  148. {
  149. Application.OpenURL("https://space.bilibili.com/7234711");
  150. }
  151. public void OnAuto(ParticleSystem.MainModule mainModule, ParticleSystem.EmissionModule emissionModule)
  152. {
  153. float b = mainModule.maxParticles;
  154. float particleCount = 0;
  155. if (emissionModule.rateOverTime.constant > 0)
  156. {
  157. float size = Mathf.Max(emissionModule.rateOverTime.constantMax, emissionModule.rateOverTime.constantMin);
  158. particleCount += size;
  159. }
  160. if (emissionModule.rateOverTime.curveMultiplier != 0)
  161. {
  162. float[] dd = new float[emissionModule.rateOverTime.curve.keys.Length];
  163. for (int z = 0; z < dd.Length; z++)
  164. {
  165. dd[z] = emissionModule.rateOverTime.curve.keys[z].value;
  166. }
  167. float maxValue = Mathf.Max(dd) * emissionModule.rateOverTime.curveMultiplier;
  168. particleCount += maxValue;
  169. }
  170. if (emissionModule.burstCount >= 1)
  171. {
  172. for (int i = 0; i < emissionModule.burstCount; i++)
  173. {
  174. if (emissionModule.GetBurst(i).count.constant != 0)
  175. {
  176. particleCount += Mathf.Max(emissionModule.GetBurst(i).count.constantMax, emissionModule.GetBurst(i).count.constantMin);
  177. if (emissionModule.GetBurst(i).repeatInterval > 0.01f)
  178. {
  179. float size = Mathf.Max(emissionModule.GetBurst(i).count.constantMax, emissionModule.GetBurst(i).count.constantMin);
  180. size = size / emissionModule.GetBurst(i).repeatInterval;
  181. particleCount += size;
  182. }
  183. }
  184. else
  185. {
  186. if(emissionModule.GetBurst(i).count.curve == null)
  187. {
  188. Debug.Log("没有发射任何粒子");
  189. }
  190. else
  191. {
  192. float[] dd = new float[(int)emissionModule.GetBurst(i).count.curve.length];
  193. for (int z = 0; z < dd.Length; z++)
  194. {
  195. dd[z] = emissionModule.GetBurst(i).count.curve.keys[z].value;
  196. }
  197. float maxValue = Mathf.Max(dd) * emissionModule.GetBurst(i).count.curveMultiplier;
  198. particleCount += maxValue;
  199. if (emissionModule.GetBurst(i).repeatInterval > 0.01f)
  200. {
  201. float size = Mathf.Max(emissionModule.GetBurst(i).count.constantMax, emissionModule.GetBurst(i).count.constantMin);
  202. size = size / emissionModule.GetBurst(i).repeatInterval;
  203. particleCount += size;
  204. }
  205. }
  206. }
  207. }
  208. }
  209. float sizeclamp = 0;
  210. sizeclamp = mainModule.startLifetime.constantMax;
  211. sizeclamp = Mathf.Clamp(sizeclamp, 1, 99999);
  212. particleCount = particleCount * sizeclamp;
  213. Debug.Log(sizeclamp);
  214. if (particleCount <= b && particleCount != 0)
  215. {
  216. float ad = Mathf.Max(emissionModule.rateOverDistance.constantMax, emissionModule.rateOverDistance.constantMin);
  217. if (ad==0&& emissionModule.rateOverDistance.curve == null)
  218. {
  219. if (particleCount <= 2)
  220. {
  221. mainModule.maxParticles = 5;
  222. }
  223. else
  224. {
  225. mainModule.maxParticles = (int)particleCount;
  226. }
  227. }
  228. else
  229. {
  230. Debug.Log("当前粒子拥有距离发射量,安全起见不设置其max粒子限制,当前值:"+mainModule.maxParticles);
  231. }
  232. }
  233. }
  234. }