Skip to content

Instantly share code, notes, and snippets.

@gamebox777 gamebox777/DragWindow.cs

Last active Nov 27, 2019
Embed
What would you like to do?
unity ドラッグできるウィンドウ
//public domain(著作権破棄) ご自由にお使いください
// 説明ページはこちら↓
// http://www.unitygamebox.com/entry/2019/11/27/%E3%80%90unity%E3%80%91%E3%83%89%E3%83%A9%E3%83%83%E3%82%B0/%E7%B8%AE%E5%B0%8F/%E6%9C%80%E5%A4%A7%E5%8C%96%E3%81%A7%E3%81%8D%E3%82%8B%E3%82%A6%E3%82%A3%E3%83%B3%E3%83%89%E3%82%A6%E3%80%90%E3%82%B9
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using System;
using System.Collections;
/// <summary>
///
/// </summary>
namespace DragWindow
{
[Serializable]
public partial class DragWindow : MonoBehaviour,IPointerDownHandler
{
[NonSerialized] public RectTransform rect;
[SerializeField] public Vector2 minSize = new Vector2(300, 30);
[SerializeField] public Vector2 maxSize = new Vector2(400, 300);
[SerializeField] public Vector2 MinimizeMaximizePivot = new Vector2(1, 1);
void Awake()
{
rect = GetComponent<RectTransform>();
}
/// <summary>
/// ウィンドウを最小化
/// </summary>
public void OnButtonMinimize()
{
var diff = rect.sizeDelta - minSize;
Vector3 pos = rect.anchoredPosition;
pos.x += diff.x * (MinimizeMaximizePivot.x - rect.pivot.x);
pos.y += diff.y * (MinimizeMaximizePivot.y - rect.pivot.y);
rect.anchoredPosition = pos;
rect.sizeDelta = minSize;
}
/// <summary>
/// ウィンドウ最大化
/// </summary>
public void OnButtonMaximize()
{
var diff = rect.sizeDelta - maxSize;
Vector3 pos = rect.anchoredPosition;
pos.x += diff.x * (MinimizeMaximizePivot.x - rect.pivot.x);
pos.y += diff.y * (MinimizeMaximizePivot.y - rect.pivot.y);
rect.anchoredPosition = pos;
rect.sizeDelta = maxSize;
}
/// <summary>
/// ウィンドウをクリックすると最前面にUIを表示する
/// </summary>
public void OnPointerDown(PointerEventData e)
{
rect.SetAsLastSibling();
}
}
} //namespace:DragWindow
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.