Last active
August 8, 2020 19:11
-
-
Save robsonbramos/1bc7614ab39d544edf9239d8deb83984 to your computer and use it in GitHub Desktop.
Simple inspector to use along with DOTween for implementing sliding GUI elements in Unity 5.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections; | |
using DG.Tweening; | |
// BOA | |
public class SlideTween : MonoBehaviour | |
{ | |
public enum Position { OutsideTop, OutsideBottom, OutsideLeft, OutsideRight, Center, InsideTop, InsideBottom, InsideLeft, InsideRight }; | |
public Position enterPoint; | |
public Position targetPoint; | |
public Position exitPoint; | |
public DG.Tweening.Ease enterEase; | |
public DG.Tweening.Ease exitEase; | |
public float enterDuration = 1f; | |
public float exitDuration = 1f; | |
public float exitTimeout = 0f; | |
public bool disableAfterExit = true; | |
public bool bringToFront = true; | |
private Vector3 endPos; | |
public void PlayTween() | |
{ | |
// Initializes DOTween | |
DOTween.Init(false, true, LogBehaviour.ErrorsOnly); | |
// Activate gameObject if it's not already | |
gameObject.SetActive(true); | |
// Sets initial position | |
transform.position = GetPosition(enterPoint); | |
// Stores the target posigion | |
endPos = GetPosition(targetPoint); | |
// If "Bring to front" is enabled, set the gameObject as last sibling | |
if (bringToFront) | |
{ | |
// Brings the gameObject to the front of layer | |
gameObject.transform.SetAsLastSibling(); | |
} | |
// If the exitTimout is set, starts a coroutine | |
if (exitTimeout > 0f && gameObject.activeSelf) StartCoroutine(Show(exitTimeout)); | |
// Or simply displays the gameObject without the exitTimeout | |
else Show(enterEase); | |
} | |
IEnumerator Show(float timeout) | |
{ | |
MoveIn(); | |
yield return new WaitForSeconds(timeout); | |
MoveOut(); | |
} | |
void Show(DG.Tweening.Ease enterEase) | |
{ | |
MoveIn(); | |
} | |
void MoveIn() | |
{ | |
// Tweens the gameobject from the start point to the target point | |
transform.DOMove(endPos, enterDuration).SetEase(enterEase); | |
} | |
IEnumerator Exit() | |
{ | |
// Tweens the gameobject to the end point from it's current position | |
transform.DOMove(GetPosition(exitPoint), exitDuration).SetEase(exitEase); | |
// If "Disable after exit" is on, deactivate the gameobject | |
if (disableAfterExit) | |
{ | |
yield return new WaitForSeconds(exitDuration); | |
gameObject.SetActive(false); | |
} | |
} | |
public void MoveOut() | |
{ | |
if (gameObject.activeSelf) | |
{ | |
StartCoroutine(Exit()); | |
} | |
} | |
public Vector3 GetPosition(Position position) { | |
// Get gameobject's rect transform | |
RectTransform rt = gameObject.GetComponent<RectTransform>(); | |
// Get gameobject's current width and height | |
Vector2 size = Vector2.Scale(rt.sizeDelta, transform.lossyScale); | |
// Get current screen size | |
Vector3 screenSize = Camera.main.ViewportToWorldPoint(new Vector3(0, 1, 0)); | |
// Set screen positions | |
Vector3 screenOutTop = new Vector3(endPos.x, screenSize.y + size.y / 2, endPos.z); | |
Vector3 screenOutBottom = new Vector3(endPos.x, -screenSize.y - size.y / 2, endPos.z); | |
Vector3 screenOutLeft = new Vector3(screenSize.x - size.x / 2, endPos.y, endPos.z); | |
Vector3 screenOutRight = new Vector3(-screenSize.x + size.x / 2, endPos.y, endPos.z); | |
Vector3 screenInTop = new Vector3(endPos.x, screenSize.y - size.y / 2, endPos.z); | |
Vector3 screenInBottom = new Vector3(endPos.x, -screenSize.y + size.y / 2, endPos.z); | |
Vector3 screenInLeft = new Vector3(screenSize.x + size.x / 2, endPos.y, endPos.z); | |
Vector3 screenInRight = new Vector3(-screenSize.x - size.x / 2, endPos.y, endPos.z); | |
Vector3 pos; | |
// Find the start position | |
switch (position) { | |
// Outside positions | |
case Position.OutsideTop: pos = screenOutTop; break; | |
case Position.OutsideBottom: pos = screenOutBottom; break; | |
case Position.OutsideLeft: pos = screenOutLeft; break; | |
case Position.OutsideRight: pos = screenOutRight; break; | |
// Inside positions | |
case Position.InsideTop: pos = screenInTop; break; | |
case Position.InsideBottom: pos = screenInBottom; break; | |
case Position.InsideLeft: pos = screenInLeft; break; | |
case Position.InsideRight: pos = screenInRight; break; | |
// Default position | |
default: pos = Vector3.zero; break; | |
} | |
// Returns the world position | |
return pos; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment