Help us understand the problem. What is going on with this article?

uGUIのオブジェクトを矢印キーで動かしたい

More than 3 years have passed since last update.

NGUIはUIWidget選択時にRectモードだと矢印キーでposition移動できるので
uGUIでも再現してみた。
Unity5用。

CanvasRendererMover.cs
using UnityEngine;
using UnityEditor;
using System.Collections;

[CustomEditor(typeof(CanvasRenderer))]
public class CanvasRendererMover : Editor
{
    static int hint = "WidgetHash".GetHashCode();
    void OnSceneGUI()
    {
        if(UnityEditor.Tools.current != Tool.Rect) return;

        var e = Event.current;
        int id = GUIUtility.GetControlID(hint, FocusType.Keyboard);
        var type = e.GetTypeForControl(id);
        if(type != EventType.keyDown) return;

        if(e.keyCode == KeyCode.UpArrow) Move(target, new Vector3(0.0f, 1.0f), e);
        else if(e.keyCode == KeyCode.RightArrow) Move(target, new Vector3(1.0f, 0.0f), e);
        else if(e.keyCode == KeyCode.DownArrow) Move(target, new Vector3(0.0f, -1.0f), e);
        else if(e.keyCode == KeyCode.LeftArrow) Move(target, new Vector3(-1.0f, 0.0f), e);
    }

    void Move(object obj, Vector3 v, Event e)
    {
        var g = obj as CanvasRenderer;
        g.transform.localPosition += v;
        e.Use();
    }
}
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした