123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class CoursewareItem : MonoBehaviour {
- [SerializeField]
- private CoursewareDir ShowDir;
- [SerializeField]
- private float ShowScaleRate = 2;//放大倍数
- private Transform startPar;//初始的根节点
- private Vector3 startPos;//初始的位置
- private Vector3 startScale;//初始大小
- private bool isShow = false;//是否正在被焦点展示
-
- void Start () {
- startPar = this.transform.parent;
- startPos = this.transform.position;
- startScale = this.transform.localScale;
- }
- // Update is called once per frame
- private void LateUpdate() {
- if(Input.GetKeyDown(KeyCode.P))
- {
- Show();
- }
- if (Input.GetKeyUp(KeyCode.P))
- {
- //Hide();
- }
- RefreshTween();
- }
- private float showDis = 0.25f;
- private Vector3 GetShowPos()
- {
- var pos = Vector3.zero;
- switch(ShowDir)
- {
- case CoursewareDir.Up:
- pos = Vector3.up;
- break;
- case CoursewareDir.Down:
- pos = Vector3.down;
- break;
- case CoursewareDir.Left:
- pos = Vector3.left;
- break;
- case CoursewareDir.Right:
- pos = Vector3.right;
- break;
- case CoursewareDir.LeftUp:
- pos = Vector3.up + Vector3.left;
- break;
- case CoursewareDir.LeftDown:
- pos = Vector3.down + Vector3.left;
- break;
- case CoursewareDir.RightUp:
- pos = Vector3.up + Vector3.right;
- break;
- case CoursewareDir.RightDown:
- pos = Vector3.down + Vector3.right;
- break;
- default:
- pos = Vector3.zero;
- break;
- }
- return pos * showDis + startPos;
- }
- private float suofangNeedTime = 0.5f;//缩放需要的时间
- private void RefreshTween()
- {
- if(isTweening)
- {
- useTime += Time.deltaTime/ suofangNeedTime;
- this.transform.position = Vector3.Lerp(this.transform.position, targetPos, useTime);
- this.transform.localScale = Vector3.Lerp(this.transform.localScale, targetScale, useTime);
- if (useTime >= 1)
- {
- Arrived();
- }
- }
- }
- public bool IsShow
- {
- get { return isShow; }
- }
- public bool IsTweening
- {
- get { return isTweening; }
- }
- public void Show()
- {
- isShow = true;
- this.transform.parent = null;//先放出去
- AddTween(GetShowPos(), startScale * ShowScaleRate);
- }
- private bool isTweening = false;
- private Vector3 targetPos;
- private Vector3 targetScale;
- private Vector3 curPos;
- private Vector3 curScale;
- private float useTime = 0;
- private void AddTween(Vector3 pos, Vector3 scale)
- {
- CDebug.Log("AddTween");
- useTime = 0;
- curPos = this.transform.position;
- curScale = this.transform.localScale;
- isTweening = true;
- targetPos = pos;
- targetScale = scale;
- }
- //动画结束的位置
- private void Arrived()
- {
- CDebug.Log("欢动完成");
- isTweening = false;
- //关闭了
- if (!isShow)
- {
- this.transform.parent = startPar;//归位
- }
- }
- public void Hide()
- {
- isShow = false;
- AddTween(startPos, startScale);
- }
- public void QiangzhiHide()
- {
- isShow = false;
- this.transform.parent = startPar;//归位
- this.transform.position = startPos;
- this.transform.localScale = startScale;
- }
- }
|