Skip to content

Instantly share code, notes, and snippets.

@AkhmadMax
Created April 14, 2017 06:47
Show Gist options
  • Save AkhmadMax/975b1b1ea8cc73ec356dda2e3ecf1fb6 to your computer and use it in GitHub Desktop.
Save AkhmadMax/975b1b1ea8cc73ec356dda2e3ecf1fb6 to your computer and use it in GitHub Desktop.
Zooms Photo In and Out
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using DG.Tweening;
using System.Collections;
public class Photo : MonoBehaviour, IPointerUpHandler
{
Texture rawImage;
Image photo;
RectTransform rect;
Vector2 initRectSize;
Vector2 initRectPos;
private bool opened;
private CanvasGroup bg;
private Image zoom;
void Start ()
{
photo = transform.FindChild("Image").GetComponent<Image>();
rect = GetComponent<RectTransform>();
initRectSize = rect.sizeDelta;
initRectPos = rect.anchoredPosition;
bg = transform.parent.FindChild("PhotoBg").GetComponent<CanvasGroup>();
zoom = transform.FindChild("Zoom").GetComponent<Image>();
}
public void OnPointerUp(PointerEventData ped)
{
if (!opened)
{
Open();
}
else
{
Close();
}
}
public void Open()
{
transform.DOKill();
rect.DOSizeDelta(new Vector2(photo.mainTexture.width, photo.mainTexture.height), 0.5f);
rect.DOAnchorPos(Vector2.zero, 0.5f);
opened = true;
Debug.Log(bg.name);
bg.DOFade(0.75f, 0.25f);
bg.blocksRaycasts = true;
if (zoom)
zoom.DOFade(0, 0.25f);
bg.transform.SetAsLastSibling();
transform.SetAsLastSibling();
}
public void Close()
{
transform.DOKill();
rect.DOSizeDelta(initRectSize, 0.25f);
rect.DOAnchorPos(initRectPos, 0.25f);
opened = false;
if (zoom)
zoom.DOFade(1, 0.25f).SetDelay(0.25f);
bg.DOFade(0.0f, 0.25f);
bg.blocksRaycasts = false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment