Skip to content

Instantly share code, notes, and snippets.

@masa795
Created December 7, 2013 14:51
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save masa795/7843511 to your computer and use it in GitHub Desktop.
GUIの横幅をマウスのドラッグで変更する
using UnityEngine;
using UnityEditor;
using System.Collections;
using System.Collections.Generic;
public class DragReSizeFiled : EditorWindow {
// Add menu item to the Window menu
[MenuItem("Window/DragReSizeFiled")]
static void Init () {
// Get existing open window or if none, make a new one:
EditorWindow.GetWindow<DragReSizeFiled>(false, "DragReSizeFiled");
}
static Vector2 scrollPosition;
private int m_focusedControlCounter = 0;
private Dictionary<Rect, int> cellNumRect = new Dictionary<Rect, int>();
private List<Rect> controlRects = new List<Rect>();
private int focusedControl = -1;
int resizeGroup1Width = 300;
int resizeGroup2Width = 100;
// Implement your own editor GUI here.
void OnGUI () {
controlRects.Clear();
cellNumRect.Clear();
m_focusedControlCounter = 0;
EditorGUILayout.LabelField("Resize sample" );
Rect offset = GUILayoutUtility.GetLastRect();
scrollPosition = GUILayout.BeginScrollView(scrollPosition, false, false);
bool isDragArea = false;
bool isDragArea2 = false;
Rect r ;
for (int count = 0; count < 5; ++count)
{
GUILayout.BeginHorizontal();
EditorGUILayout.TextField(("unity3d text field resize gourp1 " + count), GUILayout.Width(resizeGroup1Width));
AddControlRect(offset);
// TextFieldの間を選択したか +++++++++++++++++++++++++++++++++++++
if (focusedControl == m_focusedControlCounter)
isDragArea = true;
EditorGUILayout.LabelField(" ", GUILayout.Width(3));
r = AddControlRect(offset);
EditorGUIUtility.AddCursorRect(r, MouseCursor.ResizeHorizontal);
// TextFieldの間を選択したか +++++++++++++++++++++++++++++++++++++
EditorGUILayout.TextField("abcdefg");
AddControlRect(offset);
GUILayout.EndHorizontal();
}
for (int count = 0; count < 5; ++count)
{
GUILayout.BeginHorizontal();
EditorGUILayout.LabelField(("unity3d text field resize gourp2 " + count), GUILayout.Width(resizeGroup2Width));
AddControlRect(offset);
// TextFieldの間を選択したか +++++++++++++++++++++++++++++++++++++
if (focusedControl == m_focusedControlCounter)
isDragArea2 = true;
EditorGUILayout.LabelField(" ", GUILayout.Width(3));
r = AddControlRect(offset);
EditorGUIUtility.AddCursorRect(r, MouseCursor.ResizeHorizontal);
// TextFieldの間を選択したか +++++++++++++++++++++++++++++++++++++
EditorGUILayout.TextField("abcdefg");
AddControlRect(offset);
GUILayout.EndHorizontal();
}
GUILayout.EndScrollView();
Event ev = Event.current;
if (ev.type == EventType.mouseUp)
{
m_focusedControlCounter = -1;
focusedControl = -1;
Event.current.Use();
Repaint();
}
else if (ev.type == EventType.mouseDown)
{
m_mousDownPosition = ev.mousePosition;
focusedControl = GetControlNum(ev.mousePosition);
Event.current.Use();
Repaint();
}
else if (ev.type == EventType.mouseDrag)
{
if (isDragArea)
{
int diff = (int)(ev.mousePosition.x - m_mousDownPosition.x);
m_mousDownPosition = ev.mousePosition;
resizeGroup1Width = (resizeGroup1Width + diff);
Repaint();
}
else if (isDragArea2)
{
int diff = (int)(ev.mousePosition.x - m_mousDownPosition.x);
m_mousDownPosition = ev.mousePosition;
resizeGroup2Width = (resizeGroup2Width + diff);
Repaint();
}
}
}
Vector2 m_mousDownPosition = Vector2.zero;
private Rect AddControlRect(Rect offset)
{
Rect r = GUILayoutUtility.GetLastRect();
Rect res = r;
r.y += offset.y + offset.height;
controlRects.Add(r);
cellNumRect[r] = m_focusedControlCounter++;
return res;
}
private int GetControlNum(Vector2 pos)
{
pos += scrollPosition;
int i = 0;
foreach (Rect r in controlRects)
{
if (r.Contains(pos)) return i;
++i;
}
return -1;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment