当前位置:   article > 正文

Unity 常用工具方法拓展_dg.tweening.ease

dg.tweening.ease
  1. using DG.Tweening;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using UnityEngine.Events;
  6. using UnityEngine.PostProcessing;
  7. using UnityEngine.UI;
  8. public enum RenderingMode
  9. {
  10. Opaque,
  11. Cutout,
  12. Fade,
  13. Transparent,
  14. }
  15. public static class MyUtilities
  16. {
  17. #region Animation
  18. private static Dictionary<Animation, bool> bool_Animation = new Dictionary<Animation, bool>();
  19. public static void DetectionAnimation(this Animation ani, string animationName, UnityAction OnStart, UnityAction OnUpdate, UnityAction OnEnd)
  20. {
  21. if (!bool_Animation.ContainsKey(ani))
  22. {
  23. bool_Animation.Add(ani, false);
  24. }
  25. if (!bool_Animation[ani])
  26. {
  27. if (ani.IsPlaying(animationName))
  28. {
  29. bool_Animation[ani] = true;
  30. OnStart();
  31. }
  32. }
  33. else
  34. {
  35. if (ani.IsPlaying(animationName))
  36. {
  37. OnUpdate();
  38. }
  39. else
  40. {
  41. OnEnd();
  42. bool_Animation.Remove(ani);
  43. }
  44. }
  45. }
  46. /// <summary>
  47. /// 根据进度播放动画
  48. /// </summary>
  49. /// <param name="ani"></param>
  50. /// <param name="aniName">动画名称</param>
  51. /// <param name="progress">进度</param>
  52. public static void PlayAnimationFollowProgress(this Animation ani,string aniName,float progress) {
  53. AnimationState state = ani[aniName];
  54. state.normalizedTime = progress;
  55. state.normalizedSpeed = 0;
  56. ani.Play(aniName);
  57. }
  58. public static IEnumerator IEDetectionAnimation(this Animation ani, string animationName, UnityAction OnStart, UnityAction OnUpdate, UnityAction OnEnd)
  59. {
  60. while (!ani.IsPlaying(animationName))
  61. {
  62. yield return null;
  63. }
  64. OnStart();
  65. while (ani.IsPlaying(animationName))
  66. {
  67. OnUpdate();
  68. yield return null;
  69. }
  70. OnEnd();
  71. }
  72. /// <summary>
  73. /// 倒播动画
  74. /// </summary>
  75. /// <param name="ani"></param>
  76. /// <param name="aniName"></param>
  77. /// <param name="OnStart"></param>
  78. /// <param name="OnUpdate"></param>
  79. /// <param name="OnEnd"></param>
  80. /// <returns></returns>
  81. public static IEnumerator IEBackPlayAnimation(this Animation ani, string aniName, UnityAction OnStart = null, UnityAction OnUpdate = null, UnityAction OnEnd = null)
  82. {
  83. if (ani == null) yield break;
  84. ani[aniName].speed = -1;
  85. ani[aniName].time = ani[aniName].length;
  86. ani.Play(aniName);
  87. if (OnStart != null)
  88. {
  89. OnStart();
  90. }
  91. while (ani.IsPlaying(aniName))
  92. {
  93. if (OnUpdate != null)
  94. {
  95. OnUpdate();
  96. }
  97. yield return null;
  98. }
  99. ani[aniName].speed = 1;
  100. if (OnEnd != null)
  101. {
  102. OnEnd();
  103. }
  104. }
  105. /// <summary>
  106. /// 正播动画
  107. /// </summary>
  108. /// <param name="ani"></param>
  109. /// <param name="aniName"></param>
  110. /// <param name="OnStart"></param>
  111. /// <param name="OnUpdate"></param>
  112. /// <param name="OnEnd"></param>
  113. /// <returns></returns>
  114. public static IEnumerator IEForwardPlayAnimation(this Animation ani, string aniName, UnityAction OnStart = null, UnityAction OnUpdate = null, UnityAction OnEnd = null)
  115. {
  116. if (ani == null) yield break;
  117. if (ani.IsPlaying(aniName))
  118. yield break;
  119. ani[aniName].speed = 1;
  120. ani.Play(aniName);
  121. if (OnStart != null)
  122. {
  123. OnStart();
  124. }
  125. while (ani.IsPlaying(aniName))
  126. {
  127. if (OnUpdate != null)
  128. {
  129. OnUpdate();
  130. }
  131. yield return null;
  132. }
  133. if (OnEnd != null)
  134. {
  135. OnEnd();
  136. }
  137. }
  138. #endregion
  139. #region Animator
  140. private static Dictionary<Animator, bool> bool_Animator = new Dictionary<Animator, bool>();
  141. public static void DetectionAnimator(this Animator ani, int layerIndex, string stateName, UnityAction OnStart, UnityAction OnUpdate, UnityAction OnEnd)
  142. {
  143. if (!bool_Animator.ContainsKey(ani))
  144. {
  145. bool_Animator.Add(ani, false);
  146. }
  147. if (!bool_Animator[ani])
  148. {
  149. if (ani.GetCurrentAnimatorStateInfo(layerIndex).IsName(stateName))
  150. {
  151. bool_Animator[ani] = true;
  152. OnStart();
  153. }
  154. }
  155. else
  156. {
  157. if (ani.GetCurrentAnimatorStateInfo(layerIndex).IsName(stateName))
  158. {
  159. OnUpdate();
  160. }
  161. else
  162. {
  163. bool_Animator[ani] = false;
  164. OnEnd();
  165. }
  166. }
  167. }
  168. public static void ResetBool_Animator()
  169. {
  170. if (bool_Animator != null)
  171. bool_Animator.Clear();
  172. }
  173. public static IEnumerator IEDetectionAnimator(this Animator ani, int layerIndex, string stateName, UnityAction OnStart, UnityAction OnUpdate, UnityAction OnEnd)
  174. {
  175. while (!ani.GetCurrentAnimatorStateInfo(layerIndex).IsName(stateName))
  176. {
  177. yield return null;
  178. }
  179. OnStart();
  180. while (ani.GetCurrentAnimatorStateInfo(layerIndex).IsName(stateName))
  181. {
  182. OnUpdate();
  183. yield return null;
  184. }
  185. OnEnd();
  186. }
  187. #endregion
  188. #region GameObject/Transform
  189. public static string GetParentPath(this Transform target)
  190. {
  191. string path = "";
  192. while (target.parent != null)
  193. {
  194. if (path != "")
  195. {
  196. path = "/" + path;
  197. }
  198. path = target.parent.name + path;
  199. target = target.parent;
  200. }
  201. return path;
  202. }
  203. // public static Sequence mySequence;
  204. // public static bool isMoving = false;
  205. public static void DoMoveAndRotation(this GameObject target, Vector3 position, Vector3 rotation, float duration, Ease ease, UnityAction OnUpdate, UnityAction OnEnd, Space spaceWorld = Space.Self)
  206. {
  207. if (target.GetComponent<WowMainCamera>())
  208. {
  209. if (target.GetComponent<WowMainCamera>().enabled)
  210. {
  211. // print("存在WowMainCamera.cs脚本");
  212. Debug.LogError("存在WowMainCamera.cs脚本");
  213. return;
  214. }
  215. }
  216. // isMoving = true;
  217. // mySequence.Kill();
  218. Sequence mySequence = DOTween.Sequence();
  219. mySequence.SetId("DoMoveAndRotation");
  220. Tweener t1;
  221. Tweener t2;
  222. switch (spaceWorld)
  223. {
  224. case Space.Self:
  225. t1 = target.transform.DOLocalMove(position, duration).SetEase(ease);
  226. t2 = target.transform.DOLocalRotate(rotation, duration).SetEase(ease);
  227. break;
  228. default:
  229. t1 = target.transform.DOMove(position, duration).SetEase(ease);
  230. t2 = target.transform.DORotate(rotation, duration).SetEase(ease);
  231. break;
  232. }
  233. // mySequence = DOTween.Sequence();
  234. mySequence.Append(t1)
  235. .Insert(0, t2)
  236. .AppendCallback(() =>
  237. {
  238. // isMoving = false;
  239. OnEnd();
  240. }).OnUpdate(() =>
  241. {
  242. OnUpdate();
  243. });
  244. }
  245. public static Sequence SharkSequence;
  246. public static void Shake(this GameObject target, float time, Vector3 strengthV3, bool isCamera, UnityAction OnUpdate)
  247. {
  248. SharkSequence.Kill();
  249. Tweener t1;
  250. if (isCamera)
  251. t1 = target.transform.DOShakeRotation(time, strengthV3, 0, 0);
  252. else
  253. t1 = target.transform.DOShakePosition(time, strengthV3, 0, 0);
  254. SharkSequence = DOTween.Sequence();
  255. SharkSequence.SetId("Shake");
  256. SharkSequence.Append(t1);
  257. }
  258. public static void Swing(this GameObject target, float time, Vector3 strengthV3, UnityAction OnUpdate)
  259. {
  260. if (SharkSequence == null || !SharkSequence.IsActive())
  261. {
  262. SharkSequence.Kill();
  263. Tweener t1 = target.transform.DOShakeRotation(time, strengthV3, 0, 0);
  264. SharkSequence = DOTween.Sequence();
  265. SharkSequence.SetId("Swing");
  266. SharkSequence.Append(t1);
  267. }
  268. }
  269. public static void WowSetControl(this GameObject target, bool isControl, bool isDrag, bool isZoom) //改变WowMainCamera的CanBeControl和canBeZoom变量
  270. {
  271. WowMainCamera wow = target.GetComponent<WowMainCamera>();
  272. if (wow == null)
  273. {
  274. Debug.LogError("wow为null");
  275. return;
  276. }
  277. wow.allowDrag = isDrag;
  278. wow.allowControl = isControl;
  279. wow.allowZoom = isZoom;
  280. }
  281. public static void WowDoMove_X_Y_Distance(this GameObject target, Vector3 position, float x, float y, float distance, float duration, Ease ease, UnityAction OnUpdate, UnityAction OnEnd, Space spaceWorld = Space.Self)
  282. {
  283. // isMoving = true;
  284. // mySequence.Kill();
  285. WowMainCamera wow = target.GetComponent<WowMainCamera>();
  286. if (wow == null)
  287. {
  288. Debug.LogError("wow为null");
  289. return;
  290. }
  291. Tweener t1, t2, t3, t4;
  292. wow.x %= 360;
  293. wow.y %= 360;
  294. float deviation = Mathf.Abs(wow.x - x);
  295. if (deviation > 180)
  296. {
  297. if (x > 0)
  298. {
  299. if (wow.x > 180)
  300. {
  301. x += 360;
  302. }
  303. else
  304. {
  305. x -= 360;
  306. }
  307. }
  308. else
  309. {
  310. if (wow.x < -180)
  311. {
  312. wow.x += 360;
  313. }
  314. else
  315. {
  316. x += 360;
  317. }
  318. }
  319. }
  320. switch (spaceWorld)
  321. {
  322. case Space.Self:
  323. t1 = wow.target.transform.DOLocalMove(position, duration).SetEase(ease);
  324. break;
  325. default:
  326. t1 = wow.target.transform.DOMove(position, duration).SetEase(ease);
  327. break;
  328. }
  329. t2 = DOTween.To(() => wow.x, a => wow.x = a, x, duration).SetEase(ease);
  330. t4 = DOTween.To(() => wow.y, b => wow.y = b, y, duration).SetEase(ease);
  331. t3 = DOTween.To(() => wow.desiredDistance, c => wow.desiredDistance = c, distance, duration).SetEase(Ease.Linear);
  332. Sequence mySequence = DOTween.Sequence();
  333. mySequence.SetId("WowDoMove_X_Y_Distance");
  334. mySequence.Append(t1)
  335. .Insert(0, t2)
  336. .Insert(0, t3)
  337. .Insert(0, t4)
  338. .AppendCallback(() =>
  339. {
  340. // isMoving = false;
  341. OnEnd();
  342. }).OnUpdate(() =>
  343. {
  344. OnUpdate();
  345. });
  346. }
  347. public static void WowDoMove(this GameObject target, Vector3 position, float duration, Ease ease, UnityAction OnUpdate, UnityAction OnEnd, Space spaceWorld = Space.Self)
  348. {
  349. // isMoving = true;
  350. // mySequence.Kill();
  351. WowMainCamera wow = target.GetComponent<WowMainCamera>();
  352. if (wow == null)
  353. {
  354. Debug.LogError("wow为null");
  355. return;
  356. }
  357. Tweener t1;
  358. switch (spaceWorld)
  359. {
  360. case Space.Self:
  361. t1 = wow.target.transform.DOLocalMove(position, duration).SetEase(ease);
  362. break;
  363. default:
  364. t1 = wow.target.transform.DOMove(position, duration).SetEase(ease);
  365. break;
  366. }
  367. Sequence mySequence = DOTween.Sequence();
  368. mySequence.SetId("WowDoMove");
  369. mySequence.Append(t1)
  370. .AppendCallback(() =>
  371. {
  372. // isMoving = false;
  373. OnEnd();
  374. }).OnUpdate(() =>
  375. {
  376. OnUpdate();
  377. });
  378. }
  379. public static void WowDoXY(this GameObject target, float x, float y, float duration, Ease ease, UnityAction OnUpdate, UnityAction OnEnd)
  380. {
  381. // isMoving = true;
  382. // mySequence.Kill();
  383. WowMainCamera wow = target.GetComponent<WowMainCamera>();
  384. if (wow == null)
  385. {
  386. Debug.LogError("wow为null");
  387. return;
  388. }
  389. wow.x %= 360;
  390. wow.y %= 360;
  391. float deviation = Mathf.Abs(wow.x - x);
  392. if (deviation > 180)
  393. {
  394. if (x > 0)
  395. {
  396. if (wow.x > 180)
  397. {
  398. x += 360;
  399. }
  400. else
  401. {
  402. x -= 360;
  403. }
  404. }
  405. else
  406. {
  407. if (wow.x < -180)
  408. {
  409. wow.x += 360;
  410. }
  411. else
  412. {
  413. x += 360;
  414. }
  415. }
  416. }
  417. Tweener t2 = DOTween.To(() => wow.x, a => wow.x = a, x, duration).SetEase(ease);
  418. Tweener t4 = DOTween.To(() => wow.y, b => wow.y = b, y, duration).SetEase(ease);
  419. Sequence mySequence = DOTween.Sequence();
  420. mySequence.SetId("WowDoXY");
  421. mySequence.Append(t2)
  422. .Insert(0, t4)
  423. .AppendCallback(() =>
  424. {
  425. // isMoving = false;
  426. OnEnd();
  427. }).OnUpdate(() =>
  428. {
  429. OnUpdate();
  430. });
  431. }
  432. public static void WowDoMove_X_Y(this GameObject target, Vector3 position, float x, float y, float duration, Ease ease, UnityAction OnUpdate, UnityAction OnEnd, Space worldSpace = Space.Self)
  433. {
  434. // isMoving = true;
  435. // mySequence.Kill();
  436. WowMainCamera wow = target.GetComponent<WowMainCamera>();
  437. if (wow == null)
  438. {
  439. Debug.LogError("wow为null");
  440. return;
  441. }
  442. Tweener t1;
  443. switch (worldSpace)
  444. {
  445. case Space.Self:
  446. t1 = wow.target.transform.DOLocalMove(position, duration).SetEase(ease);
  447. break;
  448. default:
  449. t1 = wow.target.transform.DOMove(position, duration).SetEase(ease);
  450. break;
  451. }
  452. wow.x %= 360;
  453. wow.y %= 360;
  454. float deviation = Mathf.Abs(wow.x - x);
  455. if (deviation > 180)
  456. {
  457. if (x > 0)
  458. {
  459. if (wow.x > 180)
  460. {
  461. x += 360;
  462. }
  463. else
  464. {
  465. x -= 360;
  466. }
  467. }
  468. else
  469. {
  470. if (wow.x < -180)
  471. {
  472. wow.x += 360;
  473. }
  474. else
  475. {
  476. x += 360;
  477. }
  478. }
  479. }
  480. Tweener t2 = DOTween.To(() => wow.x, a => wow.x = a, x, duration).SetEase(ease);
  481. Tweener t4 = DOTween.To(() => wow.y, b => wow.y = b, y, duration).SetEase(ease);
  482. Sequence mySequence = DOTween.Sequence();
  483. mySequence.SetId("WowDoMove_X_Y");
  484. mySequence.Append(t1)
  485. .Insert(0, t2)
  486. .Insert(0, t4)
  487. .AppendCallback(() =>
  488. {
  489. // isMoving = false;
  490. OnEnd();
  491. }).OnUpdate(() =>
  492. {
  493. OnUpdate();
  494. });
  495. }
  496. public static void WowDoDistance(this GameObject target, float distance, float duration, Ease ease, UnityAction OnUpdate, UnityAction OnEnd)
  497. {
  498. // isMoving = true;
  499. // mySequence.Kill();
  500. WowMainCamera wow = target.GetComponent<WowMainCamera>();
  501. if (wow == null)
  502. {
  503. Debug.LogError("wow为null");
  504. return;
  505. }
  506. Tweener t3 = DOTween.To(() => wow.desiredDistance, c => wow.desiredDistance = c, distance, duration).SetEase(Ease.Linear);
  507. Sequence mySequence = DOTween.Sequence();
  508. mySequence.SetId("WowDoDistance");
  509. mySequence.Append(t3)
  510. .AppendCallback(() =>
  511. {
  512. // isMoving = false;
  513. OnEnd();
  514. }).OnUpdate(() =>
  515. {
  516. OnUpdate();
  517. });
  518. }
  519. #endregion
  520. #region Graphic
  521. #region Text
  522. // public static Sequence sequence_text;
  523. public static Dictionary<Text, Sequence> text_Sequence = new Dictionary<Text, Sequence>();
  524. public static void FadeShowText(this Text text, float showtime, string text_str, Color text_color)
  525. {
  526. Sequence sequence_text = null;
  527. Debug.Log(text_Sequence.ContainsKey(text) + " " + text_Sequence.Count + " " + text.name);
  528. if (text_Sequence.ContainsKey(text))
  529. {
  530. sequence_text = text_Sequence[text];
  531. sequence_text.Kill(true);
  532. sequence_text = DOTween.Sequence();
  533. text_Sequence.Remove(text);
  534. text_Sequence.Add(text, sequence_text);
  535. }
  536. else
  537. {
  538. sequence_text = DOTween.Sequence();
  539. text_Sequence.Add(text, sequence_text);
  540. }
  541. text.text = text_str;
  542. text.color = new Color(text_color.r, text_color.g, text_color.b, 0);
  543. Tweener t1 = text.DOFade(1, 1);
  544. Tweener t2 = text.DOFade(0, 1);
  545. sequence_text.SetId("FadeShowText");
  546. sequence_text.Append(t1)
  547. .AppendInterval(showtime)
  548. .Append(t2)
  549. .OnComplete(() =>
  550. {
  551. text_Sequence.Remove(text);
  552. Debug.Log("remove");
  553. });
  554. }
  555. #endregion
  556. public static Dictionary<Graphic, Sequence> graphic_Sequence = new Dictionary<Graphic, Sequence>();
  557. public static void FadeInOut(this Graphic graphic, float time_stay, UnityAction OnFadeIn, UnityAction OnEnd)
  558. {
  559. Tweener t1 = graphic.DOFade(1, 0.8f).OnComplete(() =>
  560. {
  561. OnFadeIn();
  562. });
  563. Tweener t2 = graphic.DOFade(0, 0.8f).SetDelay(time_stay).OnComplete(() =>
  564. {
  565. graphic_Sequence.Remove(graphic);
  566. OnEnd();
  567. });
  568. Sequence sequence = null;
  569. if (graphic_Sequence.ContainsKey(graphic))
  570. {
  571. sequence = graphic_Sequence[graphic];
  572. sequence.Kill(true);
  573. }
  574. //else
  575. //{
  576. sequence = DOTween.Sequence();
  577. sequence.SetId("FadeInOut");
  578. graphic_Sequence.Add(graphic, sequence);
  579. //}
  580. sequence.Append(t1)
  581. .AppendInterval(time_stay)
  582. .Append(t2)
  583. .AppendCallback(() =>
  584. {
  585. graphic_Sequence.Remove(graphic);
  586. Debug.Log("remove");
  587. });
  588. }
  589. #endregion
  590. #region Camera
  591. public static void DoCameraMoveRotationAndView(this Camera ca, Vector3 position, Vector3 rotation, float view, float duration, Ease ease, UnityAction OnUpdate, UnityAction OnEnd)
  592. {
  593. if (ca.GetComponent<WowMainCamera>())
  594. {
  595. if (ca.GetComponent<WowMainCamera>().enabled)
  596. {
  597. // print("存在WowMainCamera.cs脚本");
  598. Debug.LogError("存在WowMainCamera.cs脚本");
  599. return;
  600. }
  601. }
  602. Sequence mySequence = DOTween.Sequence();
  603. mySequence.SetId("DoCameraMoveRotationAndView");
  604. Tweener t1 = ca.transform.DOMove(position, duration).SetEase(ease);
  605. Tweener t2 = ca.transform.DORotate(rotation, duration).SetEase(ease);
  606. Tweener t3 = ca.DOFieldOfView(view, duration).SetEase(ease);
  607. mySequence.Append(t1)
  608. .Insert(0, t2)
  609. .Insert(0, t3)
  610. .AppendCallback(() =>
  611. {
  612. //isMoving = false;
  613. OnEnd();
  614. }).OnUpdate(() =>
  615. {
  616. OnUpdate();
  617. });
  618. }
  619. #endregion
  620. #region MonoBehaviour
  621. public static void DelayToDo(this MonoBehaviour m, float time, UnityAction method)
  622. {
  623. float a = 0;
  624. Tweener t = DOTween.To(() => { return a; }, (v) => { a = v; }, 10, time).OnComplete(() =>
  625. {
  626. method();
  627. });
  628. t.SetId("DelayToDo");
  629. }
  630. public static bool IsMasterOrIsUnline(this MonoBehaviour m)
  631. {
  632. if ((PhotonNetwork.connected && PhotonNetwork.isMasterClient) || RPCUtilities.Instance.isUnLineTest)
  633. {
  634. return true;
  635. }
  636. else
  637. {
  638. return false;
  639. }
  640. }
  641. #endregion
  642. #region GameObject
  643. public static GameObject GetGameObject(this ScriptableObject m, string path)
  644. {
  645. return GameObjectCachePool.Instance.getGameObject(path);
  646. }
  647. public static GameObject GetGameObject(this GameObject m, string path)
  648. {
  649. return GameObjectCachePool.Instance.getGameObject(path);
  650. }
  651. public static GameObject GetGameObject(this MonoBehaviour m, string path)
  652. {
  653. return GameObjectCachePool.Instance.getGameObject(path);
  654. }
  655. public static void ChangeChildrenTag(this Transform m, string tagName)
  656. {
  657. if (m == null) return;
  658. m.tag = tagName;
  659. for (int i = 0; i < m.childCount; i++)
  660. {
  661. m.GetChild(i).tag = tagName;
  662. ChangeChildrenTag(m.GetChild(i), tagName);
  663. }
  664. }
  665. #endregion
  666. #region Transform
  667. public enum FrontPostionEnum
  668. {
  669. Top,//默认向上转25度
  670. Center,//默认0度
  671. Bottom//默认向下转25度
  672. }
  673. /// <summary>
  674. /// 得到物体前方N米的位置 不跟随transform.forward
  675. /// </summary>
  676. /// <param name="t"></param>
  677. /// <param name="frontDis">前方距离</param>
  678. /// <param name="offset">偏移量,360°,可调节</param>
  679. /// <returns>返回物体的世界坐标</returns>
  680. public static Vector3 FrontPosition(this Transform t, float frontDis, float offset = 0)
  681. {
  682. Vector3 resultV = Vector3.zero;
  683. resultV = t.position + t.forward * frontDis;
  684. resultV = new Vector3(resultV.x, t.position.y, resultV.z);
  685. //前方向量
  686. Vector3 v = (resultV - t.position).normalized;
  687. //v的垂直向量
  688. Vector3 v2 = Quaternion.AngleAxis(90, Vector3.up) * v;
  689. //旋转offset后的向量
  690. Vector3 v3 = Quaternion.AngleAxis(offset, v2) * v;
  691. //位置+向量*距离
  692. resultV = t.position + v3 * frontDis;
  693. return resultV;
  694. }
  695. /// <summary>
  696. /// 得到物体前方N米的位置 跟随transform.forward
  697. /// </summary>
  698. /// <param name="t"></param>
  699. /// <param name="frontDis">前方距离</param>
  700. /// <param name="frontPositionEnum">位置枚举,上方,中间,下方</param>
  701. /// <param name="offset">偏移量,360°,可调节</param>
  702. /// <returns>返回物体的世界坐标</returns>
  703. public static Vector3 FrontPosition(this Transform t, float frontDis, FrontPostionEnum frontPositionEnum, float offset = 0)
  704. {
  705. Vector3 resultV = Vector3.zero;
  706. switch (frontPositionEnum)
  707. {
  708. case FrontPostionEnum.Top:
  709. resultV = t.position + Quaternion.AngleAxis(offset - 25.0f, t.right) * t.forward * frontDis;
  710. break;
  711. case FrontPostionEnum.Center:
  712. resultV = t.position + Quaternion.AngleAxis(offset, t.right) * t.forward * frontDis;
  713. break;
  714. case FrontPostionEnum.Bottom:
  715. resultV = t.position + Quaternion.AngleAxis(offset + 25.0f, t.right) * t.forward * frontDis;
  716. break;
  717. }
  718. return resultV;
  719. }
  720. #endregion
  721. #region Material
  722. public static void SetMaterialRenderingMode(Material material, RenderingMode renderingMode)
  723. {
  724. switch (renderingMode)
  725. {
  726. case RenderingMode.Opaque:
  727. material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One);
  728. material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.Zero);
  729. material.SetInt("_ZWrite", 1);
  730. material.DisableKeyword("_ALPHATEST_ON");
  731. material.DisableKeyword("_ALPHABLEND_ON");
  732. material.DisableKeyword("_ALPHAPREMULTIPLY_ON");
  733. material.renderQueue = -1;
  734. break;
  735. case RenderingMode.Cutout:
  736. material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One);
  737. material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.Zero);
  738. material.SetInt("_ZWrite", 1);
  739. material.EnableKeyword("_ALPHATEST_ON");
  740. material.DisableKeyword("_ALPHABLEND_ON");
  741. material.DisableKeyword("_ALPHAPREMULTIPLY_ON");
  742. material.renderQueue = 2450;
  743. break;
  744. case RenderingMode.Fade:
  745. material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.SrcAlpha);
  746. material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
  747. material.SetInt("_ZWrite", 0);
  748. material.DisableKeyword("_ALPHATEST_ON");
  749. material.EnableKeyword("_ALPHABLEND_ON");
  750. material.DisableKeyword("_ALPHAPREMULTIPLY_ON");
  751. material.renderQueue = 3000;
  752. break;
  753. case RenderingMode.Transparent:
  754. material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One);
  755. material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
  756. material.SetInt("_ZWrite", 0);
  757. material.DisableKeyword("_ALPHATEST_ON");
  758. material.DisableKeyword("_ALPHABLEND_ON");
  759. material.EnableKeyword("_ALPHAPREMULTIPLY_ON");
  760. material.renderQueue = 3000;
  761. break;
  762. }
  763. }
  764. public static void SetTransplant(this Transform target, RenderingMode renderingMode, float apha, bool changeChild)
  765. {
  766. if (target.GetComponent<Renderer>())
  767. {
  768. for (int i = 0; i < target.GetComponent<Renderer>().materials.Length; i++)
  769. {
  770. target.GetComponent<Renderer>().materials[i].color = new Color(target.GetComponent<Renderer>().materials[i].color.r, target.GetComponent<Renderer>().materials[i].color.g, target.GetComponent<Renderer>().materials[i].color.b, apha);
  771. SetMaterialRenderingMode(target.GetComponent<Renderer>().materials[i], renderingMode);
  772. }
  773. }
  774. if (changeChild)
  775. {
  776. for (int i = 0; i < target.childCount; i++)
  777. {
  778. SetTransplant(target.GetChild(i), renderingMode, apha, true);
  779. }
  780. }
  781. }
  782. public static void SetTransplant(Transform target, RenderingMode renderingMode, float apha, float time, bool changeChild)
  783. {
  784. if (target.GetComponent<Renderer>())
  785. {
  786. for (int i = 0; i < target.GetComponent<Renderer>().materials.Length; i++)
  787. {
  788. SetMaterialRenderingMode(target.GetComponent<Renderer>().materials[i], renderingMode);
  789. target.GetComponent<Renderer>().materials[i].DOColor(new Color(target.GetComponent<Renderer>().materials[i].color.r, target.GetComponent<Renderer>().materials[i].color.g, target.GetComponent<Renderer>().materials[i].color.b, apha), time);
  790. }
  791. }
  792. if (changeChild)
  793. {
  794. for (int i = 0; i < target.childCount; i++)
  795. {
  796. SetTransplant(target.GetChild(i), renderingMode, apha, time, true);
  797. }
  798. }
  799. }
  800. public static void SetTransplant(this Transform target, RenderingMode renderingMode, float apha, float time, bool changeChild, UnityAction method)
  801. {
  802. if (target.GetComponent<Renderer>())
  803. {
  804. for (int i = 0; i < target.GetComponent<Renderer>().materials.Length; i++)
  805. {
  806. target.GetComponent<Renderer>().materials[i].DOColor(new Color(target.GetComponent<Renderer>().materials[i].color.r, target.GetComponent<Renderer>().materials[i].color.g, target.GetComponent<Renderer>().materials[i].color.b, apha), time);
  807. SetMaterialRenderingMode(target.GetComponent<Renderer>().materials[i], renderingMode);
  808. }
  809. }
  810. if (changeChild)
  811. {
  812. for (int i = 0; i < target.childCount; i++)
  813. {
  814. SetTransplant(target.GetChild(i), renderingMode, apha, time, true);
  815. }
  816. }
  817. DelayToDo(time, method);
  818. }
  819. #endregion
  820. public static void DelayToDo(float time, UnityAction method)
  821. {
  822. //Time.timeScale =
  823. float a = 0;
  824. Tweener t = DOTween.To(() => { return a; }, (v) => { a = v; }, 10, time).OnComplete(() =>
  825. {
  826. method();
  827. });
  828. t.SetId("DelayToDo");
  829. }
  830. public static void DelayToDo(float time, string Id, UnityAction method)
  831. {
  832. //Time.timeScale =
  833. float a = 0;
  834. Tweener t = DOTween.To(() => { return a; }, (v) => { a = v; }, 10, time).OnComplete(() =>
  835. {
  836. method();
  837. });
  838. t.SetId(Id);
  839. }
  840. }
  841. public static class Mathf_Frame
  842. {
  843. /// <summary>
  844. /// 计算直线和面的交点
  845. /// </summary>
  846. /// <param name="planeVector">平面的法向量</param>
  847. /// <param name="planePoint">平面上一点</param>
  848. /// <param name="lineVector">直线的方向向量</param>
  849. /// <param name="linePoint">直线上一点</param>
  850. /// <returns></returns>
  851. public static Vector3 CalPlaneLineIntersectPoint(Vector3 planeVector, Vector3 planePoint, Vector3 lineVector, Vector3 linePoint)
  852. {
  853. Vector3 returnResult = Vector3.zero;
  854. float vp1, vp2, vp3, n1, n2, n3, v1, v2, v3, m1, m2, m3, t, vpt;
  855. vp1 = planeVector.x;
  856. vp2 = planeVector.y;
  857. vp3 = planeVector.z;
  858. n1 = planePoint.x;
  859. n2 = planePoint.y;
  860. n3 = planePoint.z;
  861. v1 = lineVector.x;
  862. v2 = lineVector.y;
  863. v3 = lineVector.z;
  864. m1 = linePoint.x;
  865. m2 = linePoint.y;
  866. m3 = linePoint.z;
  867. vpt = v1 * vp1 + v2 * vp2 + v3 * vp3;
  868. //首先判断直线是否与平面平行
  869. if (vpt == 0)
  870. {
  871. // returnResult = null;
  872. Debug.Log("无交点");
  873. }
  874. else
  875. {
  876. t = ((n1 - m1) * vp1 + (n2 - m2) * vp2 + (n3 - m3) * vp3) / vpt;
  877. returnResult.x = m1 + v1 * t;
  878. returnResult.y = m2 + v2 * t;
  879. returnResult.z = m3 + v3 * t;
  880. }
  881. return returnResult;
  882. }
  883. }

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/125933
推荐阅读
相关标签
  

闽ICP备14008679号