https://assetstore.unity.com/packages/tools/animation/dotween-hotween-v2-27676

 

DOTween (HOTween v2) | 애니메이션 도구 | Unity Asset Store

Use the DOTween (HOTween v2) tool from Demigiant on your next project. Find this & more animation tools on the Unity Asset Store.

assetstore.unity.com

 

애니메이션 컨트롤러와 애니메이션을 만들지 않고 이 패키지를 설치하면 코드로 애니메이션을 구현할 수 있습니다.

 

https://dotween.demigiant.com/

 

DOTween (HOTween v2)

DOTween is a fast, efficient, fully type-safe object-oriented animation engine for Unity, optimized for C# users, free and open-source, with tons of advanced features It is also the evolution of HOTween, my previous Unity tween engine. Compared to it, DOTw

dotween.demigiant.com

 

설치 방법

 

 

설치를 하고 유니티로 들어가면 이런 창이 뜰텐데

x를 클릭해도 계속 창이 나오니 이 순서대로 해주시면 됩니다.

 

 

 

사용방법


우선 테스트를 하기 위해 셋팅은 이런 식으로 했습니다.

 

 

 

 코드 설명

 

 

using 에 DG.Tweening

 

 

 

 

여기서 주의깊게 봐야할 곳은 DOLocalMoveY입니다.

여기에 endValue, duration을 인자로 받는데

이것은 targetPosition.y 가 200으로 맞춰져 있고

y좌표로 10초동안 이동됩니다.

그리고 디버그가 출력된 후 삭제됩니다.

 

 

 

그 다음으로 봐야할 곳은 DOScale 입니다.

앞에 인자는 기존 사이즈를 1이라 보고 몇배로 크게 할것인가

그리고 뒤에 인자는 몇초동안 그 사이즈를 만들 것인가 입니다.

 

'한 줄로 풀어 해석하면 기존사이즈를 5초동안 2배로 만든다' 입니다.

 

그러면 아래는 5초동안 0.1의 사이즈로 축소됩니다.

 

 

위의 코드는 10초 이동 후 삭제

아래 코드는 5초간 커지고 5초간 작아지고

그러면 이동중에 커졌다 작아지며

사라지는 시간은 10초 후 크기 변화도 5+5=10 초니깐

사이즈가 0.1이 되자마자 사라질 것입니다.

 

 

 

알파(희미해지고 선명해지는)값을 조절해주는 코드입니다.

우선 TextMeshProUGUI 컴포넌트를 불러와야 합니다.

x를 y로 바꾸어보았는데 별 차이가 없는 것을 보아

흔히 변수를 선언할 때 int x = 1; int y = 1;

상관없듯이 그런 것 같지만 확실한 것은 아닙니다.

뒤에 0f, 7f는 첫번째는 알파값을 조절 뒤에 값은 똑같이 몇동안이므로

(알파 값이 0이면 보이지 않습니다, 반대로 1이면 있는 그대로 색을 띄웁니다.)

 

이러면 이 코드는 '7초동안 알파를 0으로 만든다' 입니다.

 

 

 

완성물

 

 

using DG.Tweening;
using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;

    public class test : MonoBehaviour
    {
        private TMP_Text damageText;
        void Start()
        {
            damageText = GetComponent<TMP_Text>();

            Debug.Log(this.transform.localPosition);

            Vector3 targetPosition = this.transform.localPosition;

            targetPosition.y += 200;

            this.transform.DOLocalMoveY(targetPosition.y, 10).OnComplete(() =>
            {
                Debug.Log("MoveY Complete!");
                Destroy(this.gameObject);
            });

            this.transform.DOScale(2, 5f).OnComplete(() =>
            {
                Debug.Log("DoScale Complete 1 -> 2");
                this.transform.DOScale(0.1f, 5f).OnComplete(() =>
                {
                    Debug.Log("DoScale Complete 2 -> 0.1");
                });

            });

            var tmpUGUI = this.damageText.GetComponent<TextMeshProUGUI>();

            DOTween.ToAlpha(() => tmpUGUI.color, x => tmpUGUI.color = x, 0f, 7f);
        }

    }

 

10초간 위로 이동하면서

5초간 커지고, 5초간 작아지고

7초동안 알파값이 0이 되므로

보이지 않더라도 실제로 10초 - 7초 = 3초 후에

이 오브젝트는 Destroy됩니다.

'라이브러리' 카테고리의 다른 글

[GitHub] Sprites-Outline  (0) 2024.05.20

Simple camera shake effect for Unity3d, written in C#. Attach to your camera GameObject. To shake the camera, set shakeDuration to the number of seconds it should shake for. It will start shaking if it is enabled.

 

카메라의 Transform을 Public으로 설정하여

인스펙터에서 메인카메라를 할당한다면

메인카메라로 보는 오브젝트들의 흔들림을 조정할 수 있습니다.

Shake Duration : 지속 시간

Shake Amount : 흔들림 강도

Decrease Factor : 흘러가는 시간을 조정(Shake Duration 수치 조정)

팀 프로젝트를 하며 적용을 한 장면입니다.

 

using UnityEngine;
using System.Collections;

public class CameraShake : MonoBehaviour
{
	// Transform of the camera to shake. Grabs the gameObject's transform
	// if null.
	public Transform camTransform;
	
	// How long the object should shake for.
	public float shakeDuration = 0f;
	
	// Amplitude of the shake. A larger value shakes the camera harder.
	public float shakeAmount = 0.7f;
	public float decreaseFactor = 1.0f;
	
	Vector3 originalPos;
	
	void Awake()
	{
		if (camTransform == null)
		{
			camTransform = GetComponent(typeof(Transform)) as Transform;
		}
	}
	
	void OnEnable()
	{
		originalPos = camTransform.localPosition;
	}

	void Update()
	{
		if (shakeDuration > 0)
		{
			camTransform.localPosition = originalPos + Random.insideUnitSphere * shakeAmount;
			
			shakeDuration -= Time.deltaTime * decreaseFactor;
		}
		else
		{
			shakeDuration = 0f;
			camTransform.localPosition = originalPos;
		}
	}
}

 

 

https://gist.github.com/ftvs/5822103

 

Simple camera shake effect for Unity3d, written in C#. Attach to your camera GameObject. To shake the camera, set shakeDuration

Simple camera shake effect for Unity3d, written in C#. Attach to your camera GameObject. To shake the camera, set shakeDuration to the number of seconds it should shake for. It will start shaking i...

gist.github.com

 

 

'Study > ' 카테고리의 다른 글

[Tip] Object.FindObjectOfType  (0) 2024.06.11
[Tip] RenderSettings  (0) 2024.06.05
[팁] Slider.onValueChanged  (0) 2024.06.05
[Tip] 3D 프로젝트에서 SpriteAtals 패킹하는 법  (0) 2024.05.23
[Tip] 패키지에서 프리팹찾기  (0) 2024.04.18

구조

이때 주의할것은 뭐가 위에 올라와야 하는지 구조를 잡는 것이다.

 

 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;

public class UISkillButton : MonoBehaviour
{
    [SerializeField] private Button btn;
    [SerializeField] private int maxCooltime = 10;
    [SerializeField] private TMP_Text cooltimeText;
    [SerializeField] private Image slier;
    private float cooltime;
    private bool isCoolTime;

    void Start()
    {
        btn.onClick.AddListener(() => {
            if (IsAvaliable())
            {
                this.UseSkill();
            }
            else
            {
                Debug.Log("아직은 사용할수 없습니다.");
            }
        });

        this.Init();
    }

    private void Init()
    {
        Debug.Log("스킬버튼을 초기화 합니다.");
        this.cooltime = this.maxCooltime;
        this.slier.fillAmount = 0;
        this.cooltimeText.text = this.cooltimeText.ToString();
        this.cooltimeText.gameObject.SetActive(false);
        this.isCoolTime = false;
    }

    private void UseSkill()
    {
        Debug.Log("스킬을 사용했습니다.");
        this.cooltimeText.gameObject.SetActive(true);
        this.cooltimeText.text = this.cooltime.ToString();
        this.slier.fillAmount = 1;
        this.StartCoroutine(this.CoWaitForCoolTime());
    }

    private IEnumerator CoWaitForCoolTime()
    {
        this.isCoolTime = true;

        while (true)
        {
            this.cooltime -= Time.deltaTime;
            this.cooltimeText.text = Mathf.RoundToInt(this.cooltime).ToString();
            this.slier.fillAmount = this.cooltime / this.maxCooltime;
            if (this.cooltime <= 0)
            {
                break;
            }
            yield return null;
        }

        this.isCoolTime = false;

        Debug.Log("스킬을 사용할수 있습니다.");

        this.Init();
    }

    private bool IsAvaliable()
    {
        return !isCoolTime;
    }
}

 

 

'낙서장 > UIUX' 카테고리의 다른 글

Slider  (0) 2024.03.18
Switch Button  (0) 2024.03.18
Button and Inputfield  (0) 2024.03.18

+ Recent posts