1.眨眼效果
- ///
- /// 眨眼效果
- ///
- /// 眼遮罩
- /// 眨眼时间
- /// 眨眼后回调
- ///
- public static IEnumerator IEBlinkEye(SpriteRenderer eyeMaskRenserer, float blinkTime, Action callback)
- {
- SetTrs(eyeMaskRenserer.transform, true);
- var startColor = Color.clear;
- var targetColor = Color.black;
- var startTime = Time.time;
- var needTime = 1f;
- eyeMaskRenserer.color = startColor;
- while (eyeMaskRenserer.color.a < 0.99f)
- {
- eyeMaskRenserer.color = Color.Lerp(startColor, targetColor, (Time.time - startTime) * needTime);
- yield return null;
- }
- eyeMaskRenserer.color = targetColor;
-
- if (blinkTime > 2f)
- {
- yield return new WaitForSeconds(blinkTime - 2f);
- callback?.Invoke();
- }
-
-
-
- startColor = Color.black;
- targetColor = Color.clear;
- startTime = Time.time;
- eyeMaskRenserer.color = startColor;
- while (eyeMaskRenserer.color.a > 0.01f)
- {
- eyeMaskRenserer.color = Color.Lerp(startColor, targetColor, (Time.time - startTime) * needTime);
- yield return null;
- }
- eyeMaskRenserer.color = targetColor;
- SetTrs(eyeMaskRenserer.transform, false);
- }
- ///
- /// 变化眼睛颜色
- ///
- public void ChangEyeColor2(Color startColor, Color endColor, float changeTime, Action ChangeCall = null)
- {
- eyeImg.color = startColor;
- eyeImg.DOColor(endColor, changeTime).OnComplete(() =>
- {
- if (ChangeCall != null)
- {
- ChangeCall.Invoke();
- }
- });
- }
2.计算两点的距离
- ///
- /// 计算两点的距离
- ///
- ///
- ///
- ///
- public static float CalculateTwoPointDistance(Vector3 point1, Vector3 point2)
- {
- //根据勾股定理(a²+b²=c²)求出支撑杆长度,开c的平方根得到弦的长度
- float c = Vector3.Distance(point2, point1);
- float a = Mathf.Abs(point2.y - point1.y);
- return Mathf.Sqrt(Mathf.Pow(c, 2) - Mathf.Pow(a, 2));
- }
3.Texture 相关函数
-
- #region Texture2D
- ///
- /// 将Base64String转换成Texture
- ///
- ///
- ///
- public static Texture2D Base64StringToTexture2D(string base64Str)
- {
- try
- {
- //将base64头部信息替换
- base64Str = base64Str.Replace("data:image/png;base64,", "").Replace("data:image/jgp;base64,", "")
- .Replace("data:image/jpg;base64,", "").Replace("data:image/jpeg;base64,", "");
- byte[] bytes = Convert.FromBase64String(base64Str);
- Texture2D texture = new Texture2D(784, 800);
- texture.LoadImage(bytes);
- return texture;
- }
- catch (Exception ex)
- {
- Debug.LogError("转换异常" + ex);
- return null;
- }
- }
-
- ///
- /// 编辑器模式下Texture转换成Texture2D
- ///
- ///
- ///
- public static Texture2D TextureToTexture2D_Editor(Texture texture)
- {
- Texture2D texture2d = texture as Texture2D;
- #if UNITY_EDITOR
- UnityEditor.TextureImporter ti = (UnityEditor.TextureImporter)UnityEditor.TextureImporter.GetAtPath(UnityEditor.AssetDatabase.GetAssetPath(texture2d));
- //图片Read/Write Enable的开关
- ti.isReadable = true;
-
- UnityEditor.AssetDatabase.ImportAsset(UnityEditor.AssetDatabase.GetAssetPath(texture2d));
- #endif
- return texture2d;
- }
-
- ///
- /// 运行模式下Texture转换成Texture2D
- ///
- ///
- ///
- public static Texture2D TextureToTexture2D(Texture texture, int width = 0, int height = 0)
- {
- if (width.Equals(0)) width = texture.width;
- if (height.Equals(0)) height = texture.height;
-
- Texture2D texture2D = new Texture2D(width, height, TextureFormat.RGBA32, false);
- RenderTexture currentRT = RenderTexture.active;
-
- RenderTexture renderTexture = RenderTexture.GetTemporary(width, height, 32);
- Graphics.Blit(texture, renderTexture);
- RenderTexture.active = renderTexture;
- texture2D.ReadPixels(new Rect(0, 0, renderTexture.width, renderTexture.height), 0, 0);
- texture2D.Apply();
-
- RenderTexture.active = currentRT;
- RenderTexture.ReleaseTemporary(renderTexture);
- return texture2D;
- }
- #endregion
4.设置物体的材质球
- ///
- /// 设置物体上所有材质球[简单粗暴版]
- ///
- ///
- ///
- public static void SetMaterials(Transform trs, Material material)
- {
- Material[] materials = trs.GetComponent
().materials; - for (int i = 0; i < materials.Length; i++)
- {
- materials[i] = material;
- }
- trs.GetComponent
().materials = materials; - }
-
- ///
- /// 设置物体上所有材质球[升级版]
- ///
- ///
- ///
- public void SetMaterials(Transform trs, params Material[] p_mat)
- {
- Material[] materials = trs.GetComponent
().materials; - int length = p_mat.Length > materials.Length ? p_mat.Length : materials.Length;
- Material[] mats = new Material[length];
- for (int i = 0; i < length; i++)
- {
- if (i >= p_mat.Length)
- {
- mats[i] = materials[i];
- continue;
- }
- mats[i] = p_mat[i];
- }
- //materials = mats;
- trs.GetComponent
().materials = mats; - }
5.使用HighlightingSystem 设置物体高亮
- ///
- /// 物体高亮
- ///
- ///
- ///
- ///
- ///
- public static void HightLightController(GameObject obj, bool flag, Color endColor = new Color(), Color startColor = new Color())
- {
- if (obj != null)
- {
- FlashingController flashing = obj.gameObject.GetComponent
(); - if (flag)
- {
- if (!flashing)
- flashing = obj.gameObject.AddComponent
(); - flashing.flashingStartColor = startColor;
- flashing.flashingEndColor = endColor;
- flashing.flashingDelay = 0f;
- flashing.flashingFrequency = 1f;
- flashing.enabled = true;
- }
- else
- {
- if (flashing)
- {
- UnityEngine.Object.Destroy(flashing);
- Highlighter high = obj.GetComponent
(); - if (high)
- UnityEngine.Object.Destroy(high);
- }
- }
- }
- }
6.延时方法
- ///
- /// 协程延时
- ///
- ///
- ///
- ///
- public static IEnumerator _WaitTime(float wait,Action _func)
- {
- yield return new WaitForSeconds(wait);
- if (null != _func)
- _func();
- }
- ///
- /// 延迟一段时间执行回调
- ///
- ///
- ///
- ///
- public static void DelayByDOTween(float delay, Action action)
- {
- var timer = 0f;
- DOTween.To(() => timer, x => timer = x, 1f, delay).OnComplete(() => { action.Invoke(); });
- }
7.通过名称查找子物体
- ///
- /// 通过名称查找未知层级子物体
- ///
- ///
- ///
- ///
- public static Transform GetChild(Transform parentTF, string childName)
- {
- //在子物体中查找名为childName 的子物体,如果有就返回,如果没有就开始递归
- Transform childTF = parentTF.Find(childName);
- if (childTF != null) return childTF;
- //将问题交由子物体
- int count = parentTF.childCount;
- for (int i = 0; i < count; i++)
- {
- childTF = GetChild(parentTF.GetChild(i), childName);
- if (childTF != null)
- {
- return childTF;
- }
- }
- return null;
- }
8.动态创建Tag
- ///
- /// 检查tag列表中是否有tag,没有该tag添加此tag
- ///
- /// 所要设设置的tag
- public static void SetGameObjectTag(GameObject gameObject, string tag)
- {
- #if UNITY_EDITOR
- if (!UnityEditorInternal.InternalEditorUtility.tags.Equals(tag)) //如果tag列表中没有这个tag
- {
- UnityEditorInternal.InternalEditorUtility.AddTag(tag); //在tag列表中添加这个tag
- }
- #endif
- gameObject.tag = tag;
- }