using System.Collections; using System.Collections.Generic; using UnityEngine; public class Curtain : MonoBehaviour { public Vector3 startPos;//移动目标 public bool curState; public float Speed; Vector3 birthPlace; public float scale;//缩放尺寸 float birthScalex; int flag;//窗帘缩放方向 bool isEnd;//行为是否结束 private void Awake() { birthPlace = transform.position; birthScalex = transform.localScale.x; isEnd = true; } public void open() { StartCoroutine(Run(birthPlace)); StartCoroutine(Scale(birthScalex)); } public void close() { StartCoroutine(Run(startPos)); StartCoroutine(Scale(scale)); } public void onClick() { if (!isEnd) return; TaskCenter.GetInstance().FinishTask(2); if (curState) { curState = false; close(); } else { curState = true; open(); } isEnd = false; } IEnumerator Run(Vector3 tragetpos) { Vector3 dir = tragetpos - transform.position; while (true) { yield return new WaitForEndOfFrame(); transform.position += dir * Speed * Time.deltaTime; if (Vector3.Distance(tragetpos, transform.position)<=0.05f) { isEnd = true; break; } } } IEnumerator Scale(float targetscale) { while (true) { yield return new WaitForEndOfFrame(); float x = transform.localScale.x; float power = targetscale - x ; if (power > 0) { flag = 1; } else { flag = -1; } x += flag * Speed * Time.deltaTime; transform.localScale = new Vector3(x,transform.localScale.y,transform.localScale.z); if (Mathf.Abs(power)<=0.05) { break; } } } }