• Unity 公用函数整理【一】


    1.眨眼效果

    1. ///
    2. /// 眨眼效果
    3. ///
    4. /// 眼遮罩
    5. /// 眨眼时间
    6. /// 眨眼后回调
    7. ///
    8. public static IEnumerator IEBlinkEye(SpriteRenderer eyeMaskRenserer, float blinkTime, Action callback)
    9. {
    10. SetTrs(eyeMaskRenserer.transform, true);
    11. var startColor = Color.clear;
    12. var targetColor = Color.black;
    13. var startTime = Time.time;
    14. var needTime = 1f;
    15. eyeMaskRenserer.color = startColor;
    16. while (eyeMaskRenserer.color.a < 0.99f)
    17. {
    18. eyeMaskRenserer.color = Color.Lerp(startColor, targetColor, (Time.time - startTime) * needTime);
    19. yield return null;
    20. }
    21. eyeMaskRenserer.color = targetColor;
    22. if (blinkTime > 2f)
    23. {
    24. yield return new WaitForSeconds(blinkTime - 2f);
    25. callback?.Invoke();
    26. }
    27. startColor = Color.black;
    28. targetColor = Color.clear;
    29. startTime = Time.time;
    30. eyeMaskRenserer.color = startColor;
    31. while (eyeMaskRenserer.color.a > 0.01f)
    32. {
    33. eyeMaskRenserer.color = Color.Lerp(startColor, targetColor, (Time.time - startTime) * needTime);
    34. yield return null;
    35. }
    36. eyeMaskRenserer.color = targetColor;
    37. SetTrs(eyeMaskRenserer.transform, false);
    38. }
    1. ///
    2. /// 变化眼睛颜色
    3. ///
    4. public void ChangEyeColor2(Color startColor, Color endColor, float changeTime, Action ChangeCall = null)
    5. {
    6. eyeImg.color = startColor;
    7. eyeImg.DOColor(endColor, changeTime).OnComplete(() =>
    8. {
    9. if (ChangeCall != null)
    10. {
    11. ChangeCall.Invoke();
    12. }
    13. });
    14. }

    2.计算两点的距离

    1. ///
    2. /// 计算两点的距离
    3. ///
    4. ///
    5. ///
    6. ///
    7. public static float CalculateTwoPointDistance(Vector3 point1, Vector3 point2)
    8. {
    9. //根据勾股定理(a²+b²=c²)求出支撑杆长度,开c的平方根得到弦的长度
    10. float c = Vector3.Distance(point2, point1);
    11. float a = Mathf.Abs(point2.y - point1.y);
    12. return Mathf.Sqrt(Mathf.Pow(c, 2) - Mathf.Pow(a, 2));
    13. }

    3.Texture 相关函数

    1. #region Texture2D
    2. ///
    3. /// 将Base64String转换成Texture
    4. ///
    5. ///
    6. ///
    7. public static Texture2D Base64StringToTexture2D(string base64Str)
    8. {
    9. try
    10. {
    11. //将base64头部信息替换
    12. base64Str = base64Str.Replace("data:image/png;base64,", "").Replace("data:image/jgp;base64,", "")
    13. .Replace("data:image/jpg;base64,", "").Replace("data:image/jpeg;base64,", "");
    14. byte[] bytes = Convert.FromBase64String(base64Str);
    15. Texture2D texture = new Texture2D(784, 800);
    16. texture.LoadImage(bytes);
    17. return texture;
    18. }
    19. catch (Exception ex)
    20. {
    21. Debug.LogError("转换异常" + ex);
    22. return null;
    23. }
    24. }
    25. ///
    26. /// 编辑器模式下Texture转换成Texture2D
    27. ///
    28. ///
    29. ///
    30. public static Texture2D TextureToTexture2D_Editor(Texture texture)
    31. {
    32. Texture2D texture2d = texture as Texture2D;
    33. #if UNITY_EDITOR
    34. UnityEditor.TextureImporter ti = (UnityEditor.TextureImporter)UnityEditor.TextureImporter.GetAtPath(UnityEditor.AssetDatabase.GetAssetPath(texture2d));
    35. //图片Read/Write Enable的开关
    36. ti.isReadable = true;
    37. UnityEditor.AssetDatabase.ImportAsset(UnityEditor.AssetDatabase.GetAssetPath(texture2d));
    38. #endif
    39. return texture2d;
    40. }
    41. ///
    42. /// 运行模式下Texture转换成Texture2D
    43. ///
    44. ///
    45. ///
    46. public static Texture2D TextureToTexture2D(Texture texture, int width = 0, int height = 0)
    47. {
    48. if (width.Equals(0)) width = texture.width;
    49. if (height.Equals(0)) height = texture.height;
    50. Texture2D texture2D = new Texture2D(width, height, TextureFormat.RGBA32, false);
    51. RenderTexture currentRT = RenderTexture.active;
    52. RenderTexture renderTexture = RenderTexture.GetTemporary(width, height, 32);
    53. Graphics.Blit(texture, renderTexture);
    54. RenderTexture.active = renderTexture;
    55. texture2D.ReadPixels(new Rect(0, 0, renderTexture.width, renderTexture.height), 0, 0);
    56. texture2D.Apply();
    57. RenderTexture.active = currentRT;
    58. RenderTexture.ReleaseTemporary(renderTexture);
    59. return texture2D;
    60. }
    61. #endregion

    4.设置物体的材质球

    1. ///
    2. /// 设置物体上所有材质球[简单粗暴版]
    3. ///
    4. ///
    5. ///
    6. public static void SetMaterials(Transform trs, Material material)
    7. {
    8. Material[] materials = trs.GetComponent().materials;
    9. for (int i = 0; i < materials.Length; i++)
    10. {
    11. materials[i] = material;
    12. }
    13. trs.GetComponent().materials = materials;
    14. }
    15. ///
    16. /// 设置物体上所有材质球[升级版]
    17. ///
    18. ///
    19. ///
    20. public void SetMaterials(Transform trs, params Material[] p_mat)
    21. {
    22. Material[] materials = trs.GetComponent().materials;
    23. int length = p_mat.Length > materials.Length ? p_mat.Length : materials.Length;
    24. Material[] mats = new Material[length];
    25. for (int i = 0; i < length; i++)
    26. {
    27. if (i >= p_mat.Length)
    28. {
    29. mats[i] = materials[i];
    30. continue;
    31. }
    32. mats[i] = p_mat[i];
    33. }
    34. //materials = mats;
    35. trs.GetComponent().materials = mats;
    36. }

    5.使用HighlightingSystem 设置物体高亮

    1. ///
    2. /// 物体高亮
    3. ///
    4. ///
    5. ///
    6. ///
    7. ///
    8. public static void HightLightController(GameObject obj, bool flag, Color endColor = new Color(), Color startColor = new Color())
    9. {
    10. if (obj != null)
    11. {
    12. FlashingController flashing = obj.gameObject.GetComponent();
    13. if (flag)
    14. {
    15. if (!flashing)
    16. flashing = obj.gameObject.AddComponent();
    17. flashing.flashingStartColor = startColor;
    18. flashing.flashingEndColor = endColor;
    19. flashing.flashingDelay = 0f;
    20. flashing.flashingFrequency = 1f;
    21. flashing.enabled = true;
    22. }
    23. else
    24. {
    25. if (flashing)
    26. {
    27. UnityEngine.Object.Destroy(flashing);
    28. Highlighter high = obj.GetComponent();
    29. if (high)
    30. UnityEngine.Object.Destroy(high);
    31. }
    32. }
    33. }
    34. }

    6.延时方法

    1. ///
    2. /// 协程延时
    3. ///
    4. ///
    5. ///
    6. ///
    7. public static IEnumerator _WaitTime(float wait,Action _func)
    8. {
    9. yield return new WaitForSeconds(wait);
    10. if (null != _func)
    11. _func();
    12. }
    1. ///
    2. /// 延迟一段时间执行回调
    3. ///
    4. ///
    5. ///
    6. ///
    7. public static void DelayByDOTween(float delay, Action action)
    8. {
    9. var timer = 0f;
    10. DOTween.To(() => timer, x => timer = x, 1f, delay).OnComplete(() => { action.Invoke(); });
    11. }

    7.通过名称查找子物体

    1. ///
    2. /// 通过名称查找未知层级子物体
    3. ///
    4. ///
    5. ///
    6. ///
    7. public static Transform GetChild(Transform parentTF, string childName)
    8. {
    9.         //在子物体中查找名为childName 的子物体,如果有就返回,如果没有就开始递归
    10.         Transform childTF = parentTF.Find(childName);
    11. if (childTF != null) return childTF;
    12.         //将问题交由子物体
    13.         int count = parentTF.childCount;
    14. for (int i = 0; i < count; i++)
    15. {
    16. childTF = GetChild(parentTF.GetChild(i), childName);
    17. if (childTF != null)
    18. {
    19. return childTF;
    20. }
    21. }
    22. return null;
    23. }

    8.动态创建Tag

    1. ///
    2. /// 检查tag列表中是否有tag,没有该tag添加此tag
    3. ///
    4. /// 所要设设置的tag
    5. public static void SetGameObjectTag(GameObject gameObject, string tag)
    6. {
    7. #if UNITY_EDITOR
    8. if (!UnityEditorInternal.InternalEditorUtility.tags.Equals(tag)) //如果tag列表中没有这个tag
    9. {
    10. UnityEditorInternal.InternalEditorUtility.AddTag(tag); //在tag列表中添加这个tag
    11. }
    12. #endif
    13. gameObject.tag = tag;
    14. }

  • 相关阅读:
    windows系统删除网络适配器
    Vue中动态绑定class和style
    JVM学习三
    Leetcode 16.07 最大数值
    Pytorch中的torch.index_select对应MindSpore哪个方法
    PAT 1042 Shuffling Machine
    SpringBoot-拦截器
    四旋翼无人机学习第12节--跨页连接符的标号设置、DRC、PDF导出
    Cordova 自定义插件(Android版本)
    JAVA线程和线程池
  • 原文地址:https://blog.csdn.net/U3DCoder/article/details/126510629