Unity UI 缩放动画:实现从小到大弹出效果
你可以使用Scale动画来实现UI从小到大弹出的效果。以下是一个示例代码:\n\ncsharp\nusing System.Collections;\nusing UnityEngine;\nusing UnityEngine.UI;\n\npublic class ScaleAnimation : MonoBehaviour\n{\n public float scaleSpeed = 1f; // 缩放速度\n public float maxScale = 2f; // 最大缩放比例\n\n private RectTransform rectTransform;\n private Vector3 originalScale;\n\n private void Start()\n {\n rectTransform = GetComponent<RectTransform>();\n originalScale = rectTransform.localScale;\n }\n\n public void StartScaleAnimation()\n {\n StartCoroutine(ScaleCoroutine());\n }\n\n private IEnumerator ScaleCoroutine()\n {\n float timer = 0f;\n while (timer < 1f)\n {\n timer += Time.deltaTime * scaleSpeed;\n float scale = Mathf.Lerp(0f, maxScale, timer);\n rectTransform.localScale = originalScale * scale;\n yield return null;\n }\n }\n}\n\n\n你可以将上述代码附加到你的UI对象上,然后通过调用StartScaleAnimation()方法来开始缩放动画。\n\n你也可以自定义缩放速度和最大缩放比例,通过修改scaleSpeed和maxScale变量的值来实现。
原文地址: https://www.cveoy.top/t/topic/pHxt 著作权归作者所有。请勿转载和采集!