DNPPreset.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using TMPro;
  5. using DamageNumbersPro;
  6. namespace DamageNumbersPro.Internal
  7. {
  8. [CreateAssetMenu(fileName = "Preset", menuName = "TextMeshPro/Preset for DNP", order = -1)]
  9. public class DNPPreset : ScriptableObject
  10. {
  11. // Font
  12. public bool changeFontAsset;
  13. public TMP_FontAsset fontAsset;
  14. // Color
  15. public bool changeColor;
  16. public Color color = Color.white;
  17. public bool enableGradient;
  18. public VertexGradient gradient = new VertexGradient(Color.white, Color.white, Color.white, Color.white);
  19. // Number
  20. public bool changeNumber;
  21. public bool enableNumber = true;
  22. public TextSettings numberSettings = new TextSettings(0);
  23. public DigitSettings digitSettings = new DigitSettings(0);
  24. // Left Text
  25. public bool changeLeftText;
  26. public bool enableLeftText = true;
  27. public string leftText;
  28. public TextSettings leftTextSettings = new TextSettings(0f);
  29. // Right Text
  30. public bool changeRightText;
  31. public bool enableRightText = true;
  32. public string rightText;
  33. public TextSettings rightTextSettings = new TextSettings(0f);
  34. // Vertical Text
  35. public bool hideVerticalTexts = false;
  36. // Fade In
  37. public bool changeFadeIn = false;
  38. public float durationFadeIn = 0.2f;
  39. public bool enableOffsetFadeIn = true;
  40. [Tooltip("TextA and TextB move together from this offset.")]
  41. public Vector2 offsetFadeIn = new Vector2(0.5f, 0);
  42. public bool enableScaleFadeIn = true;
  43. [Tooltip("Scales in from this scale.")]
  44. public Vector2 scaleFadeIn = new Vector2(2, 2);
  45. public bool enableCrossScaleFadeIn = false;
  46. [Tooltip("Scales TextA in from this scale and TextB from the inverse of this scale.")]
  47. public Vector2 crossScaleFadeIn = new Vector2(1, 1.5f);
  48. public bool enableShakeFadeIn = false;
  49. [Tooltip("Shakes in from this offset.")]
  50. public Vector2 shakeOffsetFadeIn = new Vector2(0, 1.5f);
  51. [Tooltip("Shakes in at this frequency.")]
  52. public float shakeFrequencyFadeIn = 4f;
  53. // Fade Out
  54. public bool changeFadeOut = false;
  55. public float durationFadeOut = 0.2f;
  56. public bool enableOffsetFadeOut = true;
  57. [Tooltip("TextA and TextB move apart to this offset.")]
  58. public Vector2 offsetFadeOut = new Vector2(0.5f, 0);
  59. public bool enableScaleFadeOut = false;
  60. [Tooltip("Scales out to this scale.")]
  61. public Vector2 scaleFadeOut = new Vector2(2, 2);
  62. public bool enableCrossScaleFadeOut = false;
  63. [Tooltip("Scales TextA out to this scale and TextB to the inverse of this scale.")]
  64. public Vector2 crossScaleFadeOut = new Vector2(1, 1.5f);
  65. public bool enableShakeFadeOut = false;
  66. [Tooltip("Shakes out to this offset.")]
  67. public Vector2 shakeOffsetFadeOut = new Vector2(0, 1.5f);
  68. [Tooltip("Shakes out at this frequency.")]
  69. public float shakeFrequencyFadeOut = 4f;
  70. // Movement
  71. public bool changeMovement = false;
  72. public bool enableLerp = true;
  73. public LerpSettings lerpSettings = new LerpSettings(0);
  74. public bool enableVelocity = false;
  75. public VelocitySettings velocitySettings = new VelocitySettings(0);
  76. public bool enableShaking = false;
  77. [Tooltip("Shake settings during idle.")]
  78. public ShakeSettings shakeSettings = new ShakeSettings(new Vector2(0.005f, 0.005f));
  79. public bool enableFollowing = false;
  80. public FollowSettings followSettings = new FollowSettings(0);
  81. // Rotation
  82. public bool changeRotation = false;
  83. public bool enableStartRotation = false;
  84. [Tooltip("The minimum z-angle for the random spawn rotation.")]
  85. public float minRotation = -4f;
  86. [Tooltip("The maximum z-angle for the random spawn rotation.")]
  87. public float maxRotation = 4f;
  88. public bool enableRotateOverTime = false;
  89. [Tooltip("The minimum rotation speed for the z-angle.")]
  90. public float minRotationSpeed = -15f;
  91. [Tooltip("The maximum rotation speed for the z-angle.")]
  92. public float maxRotationSpeed = 15;
  93. [Tooltip("Defines rotation speed over lifetime.")]
  94. public AnimationCurve rotateOverTime = new AnimationCurve(new Keyframe[] { new Keyframe(0, 1), new Keyframe(0.4f, 1), new Keyframe(0.8f, 0), new Keyframe(1, 0) });
  95. // Scaling
  96. public bool changeScaling = false;
  97. public bool enableScaleByNumber = false;
  98. public ScaleByNumberSettings scaleByNumberSettings = new ScaleByNumberSettings(0);
  99. public bool enableScaleOverTime = false;
  100. [Tooltip("Will scale over it's lifetime using this curve.")]
  101. public AnimationCurve scaleOverTime = new AnimationCurve(new Keyframe(0, 1), new Keyframe(1, 0.7f));
  102. // Spam Control
  103. public bool changeSpamControl = false;
  104. public string spamGroup = "";
  105. public bool enableCombination = false;
  106. public CombinationSettings combinationSettings = new CombinationSettings(0);
  107. public bool enableDestruction = false;
  108. public DestructionSettings destructionSettings = new DestructionSettings(0);
  109. public bool enableCollision = false;
  110. public CollisionSettings collisionSettings = new CollisionSettings(0);
  111. public bool enablePush = false;
  112. public PushSettings pushSettings = new PushSettings(0);
  113. public bool IsApplied(DamageNumber dn)
  114. {
  115. TMP_Text[] textMeshs = dn.GetTextMeshs();
  116. if (textMeshs[0] == null)
  117. {
  118. dn.GetReferencesIfNecessary();
  119. textMeshs = dn.GetTextMeshs();
  120. }
  121. bool isApplied = true;
  122. // Font
  123. if (changeFontAsset)
  124. {
  125. foreach(TMP_Text tmp in textMeshs)
  126. {
  127. if (fontAsset != tmp.font)
  128. {
  129. isApplied = false;
  130. }
  131. }
  132. }
  133. // Color
  134. if (changeColor)
  135. {
  136. foreach (TMP_Text tmp in textMeshs)
  137. {
  138. if (color != tmp.color || enableGradient != tmp.enableVertexGradient || !gradient.Equals(tmp.colorGradient))
  139. {
  140. isApplied = false;
  141. }
  142. }
  143. }
  144. // Number
  145. if(changeNumber)
  146. {
  147. if(enableNumber != dn.enableNumber || !numberSettings.Equals(dn.numberSettings) || !digitSettings.Equals(dn.digitSettings))
  148. {
  149. isApplied = false;
  150. }
  151. }
  152. // Left Text
  153. if(changeLeftText)
  154. {
  155. if(enableLeftText != dn.enableLeftText || !leftTextSettings.Equals(dn.leftTextSettings) || leftText != dn.leftText)
  156. {
  157. isApplied = false;
  158. }
  159. }
  160. // Right Text
  161. if (changeRightText)
  162. {
  163. if (enableRightText != dn.enableRightText || !rightTextSettings.Equals(dn.rightTextSettings) || rightText != dn.rightText)
  164. {
  165. isApplied = false;
  166. }
  167. }
  168. // Vertical Texts
  169. if (hideVerticalTexts)
  170. {
  171. if(dn.enableTopText || dn.enableBottomText)
  172. {
  173. isApplied = false;
  174. }
  175. }
  176. // Fade In
  177. if(changeFadeIn)
  178. {
  179. if(durationFadeIn != dn.durationFadeIn || enableOffsetFadeIn != dn.enableOffsetFadeIn || offsetFadeIn != dn.offsetFadeIn ||
  180. enableScaleFadeIn != dn.enableScaleFadeIn || scaleFadeIn != dn.scaleFadeIn || enableCrossScaleFadeIn != dn.enableCrossScaleFadeIn ||
  181. crossScaleFadeIn != dn.crossScaleFadeIn || enableShakeFadeIn != dn.enableShakeFadeIn || shakeOffsetFadeIn != dn.shakeOffsetFadeIn ||
  182. shakeFrequencyFadeIn != dn.shakeFrequencyFadeIn)
  183. {
  184. isApplied = false;
  185. }
  186. }
  187. // Fade Out
  188. if (changeFadeOut)
  189. {
  190. if (durationFadeOut != dn.durationFadeOut || enableOffsetFadeOut != dn.enableOffsetFadeOut || offsetFadeOut != dn.offsetFadeOut ||
  191. enableScaleFadeOut != dn.enableScaleFadeOut || scaleFadeOut != dn.scaleFadeOut || enableCrossScaleFadeOut != dn.enableCrossScaleFadeOut ||
  192. crossScaleFadeOut != dn.crossScaleFadeOut || enableShakeFadeOut != dn.enableShakeFadeOut || shakeOffsetFadeOut != dn.shakeOffsetFadeOut ||
  193. shakeFrequencyFadeOut != dn.shakeFrequencyFadeOut)
  194. {
  195. isApplied = false;
  196. }
  197. }
  198. // Movement
  199. if(changeMovement)
  200. {
  201. if(enableLerp != dn.enableLerp || !lerpSettings.Equals(dn.lerpSettings) ||
  202. enableVelocity != dn.enableVelocity || !velocitySettings.Equals(dn.velocitySettings) ||
  203. enableShaking != dn.enableShaking || !shakeSettings.Equals(dn.shakeSettings) ||
  204. enableFollowing != dn.enableFollowing || !followSettings.Equals(dn.followSettings))
  205. {
  206. isApplied = false;
  207. }
  208. }
  209. // Rotation
  210. if(changeRotation)
  211. {
  212. if(enableStartRotation != dn.enableStartRotation || minRotation != dn.minRotation || maxRotation != dn.maxRotation ||
  213. enableRotateOverTime != dn.enableRotateOverTime || minRotationSpeed != dn.minRotationSpeed || maxRotationSpeed != dn.maxRotationSpeed || !rotateOverTime.Equals(dn.rotateOverTime))
  214. {
  215. isApplied = false;
  216. }
  217. }
  218. // Scale
  219. if(changeScaling)
  220. {
  221. if(enableScaleByNumber != dn.enableScaleByNumber || !scaleByNumberSettings.Equals(dn.scaleByNumberSettings) ||
  222. enableScaleOverTime != dn.enableScaleOverTime || !scaleOverTime.Equals(dn.scaleOverTime))
  223. {
  224. isApplied = false;
  225. }
  226. }
  227. // Spam Group
  228. if(changeSpamControl)
  229. {
  230. if(enableCombination != dn.enableCombination || !combinationSettings.Equals(dn.combinationSettings) ||
  231. enableDestruction != dn.enableDestruction || !destructionSettings.Equals(dn.destructionSettings) ||
  232. enableCollision != dn.enableCollision || !collisionSettings.Equals(dn.collisionSettings) ||
  233. enablePush != dn.enablePush || !pushSettings.Equals(dn.pushSettings))
  234. {
  235. isApplied = false;
  236. }
  237. }
  238. return isApplied;
  239. }
  240. public void Apply(DamageNumber dn)
  241. {
  242. TMP_Text[] textMeshs = dn.GetTextMeshs();
  243. // Font
  244. if (changeFontAsset)
  245. {
  246. foreach(TMP_Text tmp in textMeshs)
  247. {
  248. tmp.font = fontAsset;
  249. }
  250. }
  251. // Color
  252. if (changeColor)
  253. {
  254. foreach (TMP_Text tmp in textMeshs)
  255. {
  256. tmp.color = color;
  257. tmp.enableVertexGradient = enableGradient;
  258. tmp.colorGradient = gradient;
  259. }
  260. }
  261. // Number
  262. if (changeNumber)
  263. {
  264. dn.enableNumber = enableNumber;
  265. dn.numberSettings = numberSettings;
  266. dn.digitSettings = digitSettings;
  267. }
  268. // Left Text
  269. if (changeLeftText)
  270. {
  271. dn.enableLeftText = enableLeftText;
  272. dn.leftText = leftText;
  273. dn.leftTextSettings = leftTextSettings;
  274. }
  275. // Right Text
  276. if (changeRightText)
  277. {
  278. dn.enableRightText = enableRightText;
  279. dn.rightText = rightText;
  280. dn.rightTextSettings = rightTextSettings;
  281. }
  282. // Hide Vertical Texts
  283. if(hideVerticalTexts)
  284. {
  285. dn.enableTopText = dn.enableBottomText = false;
  286. }
  287. // Fade In
  288. if(changeFadeIn)
  289. {
  290. dn.durationFadeIn = durationFadeIn;
  291. dn.enableOffsetFadeIn = enableOffsetFadeIn;
  292. dn.offsetFadeIn = offsetFadeIn;
  293. dn.enableScaleFadeIn = enableScaleFadeIn;
  294. dn.scaleFadeIn = scaleFadeIn;
  295. dn.enableCrossScaleFadeIn = enableCrossScaleFadeIn;
  296. dn.crossScaleFadeIn = crossScaleFadeIn;
  297. dn.enableShakeFadeIn = enableShakeFadeIn;
  298. dn.shakeOffsetFadeIn = shakeOffsetFadeIn;
  299. dn.shakeFrequencyFadeIn = shakeFrequencyFadeIn;
  300. }
  301. // Fade Out
  302. if (changeFadeOut)
  303. {
  304. dn.durationFadeOut = durationFadeOut;
  305. dn.enableOffsetFadeOut = enableOffsetFadeOut;
  306. dn.offsetFadeOut = offsetFadeOut;
  307. dn.enableScaleFadeOut = enableScaleFadeOut;
  308. dn.scaleFadeOut = scaleFadeOut;
  309. dn.enableCrossScaleFadeOut = enableCrossScaleFadeOut;
  310. dn.crossScaleFadeOut = crossScaleFadeOut;
  311. dn.enableShakeFadeOut = enableShakeFadeOut;
  312. dn.shakeOffsetFadeOut = shakeOffsetFadeOut;
  313. dn.shakeFrequencyFadeOut = shakeFrequencyFadeOut;
  314. }
  315. // Movement
  316. if(changeMovement)
  317. {
  318. dn.enableLerp = enableLerp;
  319. dn.lerpSettings = lerpSettings;
  320. dn.enableVelocity = enableVelocity;
  321. dn.velocitySettings = velocitySettings;
  322. dn.enableShaking = enableShaking;
  323. dn.shakeSettings = shakeSettings;
  324. dn.enableFollowing = enableFollowing;
  325. dn.followSettings = followSettings;
  326. }
  327. // Rotation
  328. if(changeRotation)
  329. {
  330. dn.enableStartRotation = enableStartRotation;
  331. dn.minRotation = minRotation;
  332. dn.maxRotation = maxRotation;
  333. dn.enableRotateOverTime = enableRotateOverTime;
  334. dn.minRotationSpeed = minRotationSpeed;
  335. dn.maxRotationSpeed = maxRotationSpeed;
  336. dn.rotateOverTime = rotateOverTime;
  337. }
  338. // Scale
  339. if(changeScaling)
  340. {
  341. dn.enableScaleByNumber = enableScaleByNumber;
  342. dn.scaleByNumberSettings = scaleByNumberSettings;
  343. dn.enableScaleOverTime = enableScaleOverTime;
  344. dn.scaleOverTime = scaleOverTime;
  345. }
  346. // Spam Control
  347. if(changeSpamControl)
  348. {
  349. if(dn.spamGroup == null || dn.spamGroup == "")
  350. {
  351. dn.spamGroup = spamGroup;
  352. }
  353. dn.enableCombination = enableCombination;
  354. dn.combinationSettings = combinationSettings;
  355. dn.enableDestruction = enableDestruction;
  356. dn.destructionSettings = destructionSettings;
  357. dn.enableCollision = enableCollision;
  358. dn.collisionSettings = collisionSettings;
  359. dn.enablePush = enablePush;
  360. dn.pushSettings = pushSettings;
  361. }
  362. }
  363. public void Get(DamageNumber dn)
  364. {
  365. TMP_Text[] textMeshs = dn.GetTextMeshs();
  366. // Font
  367. changeFontAsset = true;
  368. foreach (TMP_Text tmp in textMeshs)
  369. {
  370. if(tmp != null)
  371. {
  372. fontAsset = tmp.font;
  373. }
  374. }
  375. // Color
  376. changeColor = true;
  377. foreach (TMP_Text tmp in textMeshs)
  378. {
  379. if(tmp != null)
  380. {
  381. color = tmp.color;
  382. enableGradient = tmp.enableVertexGradient;
  383. gradient = tmp.colorGradient;
  384. }
  385. }
  386. // Fade In
  387. changeFadeIn = true;
  388. durationFadeIn = dn.durationFadeIn;
  389. enableOffsetFadeIn = dn.enableOffsetFadeIn;
  390. offsetFadeIn = dn.offsetFadeIn;
  391. enableScaleFadeIn = dn.enableScaleFadeIn;
  392. scaleFadeIn = dn.scaleFadeIn;
  393. enableCrossScaleFadeIn = dn.enableCrossScaleFadeIn;
  394. crossScaleFadeIn = dn.crossScaleFadeIn;
  395. enableShakeFadeIn = dn.enableShakeFadeIn;
  396. shakeOffsetFadeIn = dn.shakeOffsetFadeIn;
  397. shakeFrequencyFadeIn = dn.shakeFrequencyFadeIn;
  398. // Fade Out
  399. changeFadeOut = true;
  400. durationFadeOut = dn.durationFadeOut;
  401. enableOffsetFadeOut = dn.enableOffsetFadeOut;
  402. offsetFadeOut = dn.offsetFadeOut;
  403. enableScaleFadeOut = dn.enableScaleFadeOut;
  404. scaleFadeOut = dn.scaleFadeOut;
  405. enableCrossScaleFadeOut = dn.enableCrossScaleFadeOut;
  406. crossScaleFadeOut = dn.crossScaleFadeOut;
  407. enableShakeFadeOut = dn.enableShakeFadeOut;
  408. shakeOffsetFadeOut = dn.shakeOffsetFadeOut;
  409. shakeFrequencyFadeOut = dn.shakeFrequencyFadeOut;
  410. // Movement
  411. changeMovement = true;
  412. enableLerp = dn.enableLerp;
  413. lerpSettings = dn.lerpSettings;
  414. enableVelocity = dn.enableVelocity;
  415. velocitySettings = dn.velocitySettings;
  416. enableShaking = dn.enableShaking;
  417. shakeSettings = dn.shakeSettings;
  418. enableFollowing = dn.enableFollowing;
  419. followSettings = dn.followSettings;
  420. // Rotation
  421. changeRotation = true;
  422. enableStartRotation = dn.enableStartRotation;
  423. minRotation = dn.minRotation;
  424. maxRotation = dn.maxRotation;
  425. enableRotateOverTime = dn.enableRotateOverTime;
  426. minRotationSpeed = dn.minRotationSpeed;
  427. maxRotationSpeed = dn.maxRotationSpeed;
  428. rotateOverTime = dn.rotateOverTime;
  429. // Scale
  430. changeScaling = true;
  431. enableScaleByNumber = dn.enableScaleByNumber;
  432. scaleByNumberSettings = dn.scaleByNumberSettings;
  433. enableScaleOverTime = dn.enableScaleOverTime;
  434. scaleOverTime = dn.scaleOverTime;
  435. // Spam Group
  436. changeSpamControl = true;
  437. spamGroup = dn.spamGroup != "" ? "Default" : "";
  438. enableCombination = dn.enableCombination;
  439. combinationSettings = dn.combinationSettings;
  440. enableDestruction = dn.enableDestruction;
  441. destructionSettings = dn.destructionSettings;
  442. enableCollision = dn.enableCollision;
  443. collisionSettings = dn.collisionSettings;
  444. enablePush = dn.enablePush;
  445. pushSettings = dn.pushSettings;
  446. }
  447. }
  448. }