Unity3DでuGUIのDataGridに挑戦してみる

とりあえず、データグリッドっぽいのは出来た

20150203_0325_02

Hierarchy のプロパティをひたすらメモしては GetComponent して値をセットするだけの作業でそれっぽいのができました。

サンプルコード

using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using System.Collections;
using System.Linq;
using Unity.Linq;
using Mera.UI;

public class TestScript : MonoBehaviour {

    // Use this for initialization
    void Start() {

        // Resources
        var dcUI = Resources.LoadAll<Sprite>("UI001").ToDictionary<Sprite,string>(sprite => sprite.name);

        var button = dcUI["Button001"];
        var grid_head = dcUI["GridHead001"];
        var grid_cell = dcUI["GridCell001"];

        // Vector2
        var top_left = new Vector2(0f, 1f);
        var middle_left = new Vector2(0f, 0.5f);
        var bottom_left = new Vector2(0f, 0f);
        var top_center = new Vector2(0.5f, 1f);
        var middle_center = new Vector2(0.5f, 0.5f);
        var bottom_center = new Vector2(0.5f, 0f);
        var top_right = new Vector2(1f, 1f);
        var middle_right = new Vector2(1f, 0.5f);
        var bottom_right = new Vector2(1f, 0f);

        // Event System
        var event_system = gameObject.UIGetEventSystem();

        // Canvas
        var canvas_object = gameObject.UIGetCanvas();
        // DataGrid
        var data_grid = canvas_object
            .UIAddNode(name: "DataGrid2", anchorMin: top_left, anchorMax: top_left, anchoredPosition: new Vector2(0, -Screen.height + 20), sizeDelta: new Vector2(Screen.width - 20, Screen.height - 20));
        // ScrollView
        var scroll_view = data_grid
            .UIAddNode(name: "ScrollView2", anchorMin: bottom_left, anchorMax: top_right, anchoredPosition: new Vector2(0, 0), sizeDelta: new Vector2(0, 0), pivot: top_left)
                .UISetScrollRect(movementType: ScrollRect.MovementType.Clamped)
                .UISetMask(showMaskGraphic: true)
                .UISetCanvasRender()
                .UISetImage(sprite: button, type: Image.Type.Sliced);
        // Content
        var content = scroll_view
            .UIAddNode(name: "Content2", anchorMin: middle_center, anchorMax: middle_center, anchoredPosition: new Vector2(0, 200), sizeDelta: new Vector2(Screen.width - 20, Screen.height - 20), pivot: top_center)
                .UISetVerticalLayoutGroup()
                .UISetContentSizeFitter(horizontalFit: ContentSizeFitter.FitMode.PreferredSize, verticalFit: ContentSizeFitter.FitMode.PreferredSize)
                .UIAttachContentToScrollRect();
        // Rows
        for (int ii = 0; ii < 60; ii++) {
            // Header
            var row = content
                .UIAddNode(name: "Row", anchorMin: top_left, anchorMax: top_left, sizeDelta: new Vector2(0, 0), pivot: middle_center)
                .UISetHorizontalLayoutGroup(childAlignment: TextAnchor.UpperLeft);
            // Cell
            for (int jj = 0; jj < 20; jj++) {
                row
                    .UIAddNode(name: "Cell", anchorMin: top_left, anchorMax: top_left, sizeDelta: new Vector2(100, 24), pivot: top_left)
                        .UISetCanvasRender()
                        .UISetImage(sprite: ii == 0 ? grid_head : grid_cell, type: Image.Type.Sliced)
                        .UISetLayoutElement(100, 24)
                    // Text
                    .UIAddNode(name: "Text", anchorMin: middle_center, anchorMax: middle_center, anchoredPosition: new Vector2(0, 0), sizeDelta: new Vector2(100, 24), pivot: middle_center)
                        .UISetCanvasRender()
                        .UISetText(Text: "Sample");
            }
        }
        // VScrollBar
        var vscroll_bar = data_grid
            .UIAddNode(name: "VScrollBar", anchorMin: bottom_right, anchorMax: top_right, anchoredPosition: new Vector2(0, 0), sizeDelta: new Vector2(20, 0), pivot: top_left)
                .UISetCanvasRender()
                .UISetImage(sprite: grid_cell, type: Image.Type.Sliced)
                .UISetScrollbar(scrollview: scroll_view, Vertical: true)
            .UIAddNode(name: "Sliding Area", anchorMin: bottom_left, anchorMax: top_right, anchoredPosition: new Vector2(0, 0), sizeDelta: new Vector2(-20, -20), pivot: middle_center)
                .UIAddNode(name: "Handle", anchorMin: Vector2.zero, anchorMax: Vector2.one, anchoredPosition: Vector2.zero, sizeDelta: new Vector2(20, 20), pivot: middle_center)
                    .UISetCanvasRender()
                    .UISetImage(sprite: grid_head, type: Image.Type.Sliced)
                    .UISetHandleToScrollBar();
        // HScrollBar
        var hscroll_bar = data_grid
            .UIAddNode(name: "HScrollBar", anchorMin: bottom_left, anchorMax: bottom_right, anchoredPosition: new Vector2(0, 0), sizeDelta: new Vector2(0, 20), pivot: top_left)
                .UISetCanvasRender()
                .UISetImage(sprite: grid_cell, type: Image.Type.Sliced)
                .UISetScrollbar(scrollview: scroll_view, Horizontal: true)
            .UIAddNode(name: "Sliding Area", anchorMin: bottom_left, anchorMax: top_right, anchoredPosition: new Vector2(0, 0), sizeDelta: new Vector2(-20, -20), pivot: middle_center)
                .UIAddNode(name: "Handle", anchorMin: Vector2.zero, anchorMax: Vector2.one, anchoredPosition: Vector2.zero, sizeDelta: new Vector2(20, 20), pivot: middle_center)
                    .UISetCanvasRender()
                    .UISetImage(sprite: grid_head, type: Image.Type.Sliced)
                    .UISetHandleToScrollBar();


    }

    // Update is called once per frame
    void Update() {

    }
}

次なる野望

何気にグ○ープ○ティっぽいのが作れるといいですね。
今のコードはコンポーネント単位で拡張メソッドをつけちゃってるので、普段はラッパ的なものでもう少しまとめてしまいたいところです。

まだボタンクリック処理とかのイベント設定ができてないのでそれができたら基本的な事ができるようになる気がします。

何するかっていうと、PHP で管理画面作るのしんどすぎるねん。
色々コードの自動生成と組み合わせておもろいのできそうな気がする。

ツイートツイート