LoginSignup
1
1

More than 1 year has passed since last update.

GraphViewでTextureNodeを作る方法

Posted at

GraphViewを勉強中です。

を一通り試した後で、では「テクスチャを持ったTextureNodeを作ろうとした時にどうしようかな?」と色々試行錯誤した結果、とりあえず実装出来たのでメモしておきます

スクリーンショット 2021-06-05 000109.png

TextureNode.cs
using UnityEditor.Experimental.GraphView;
using UnityEditor.UIElements;
using UnityEngine;
using UnityEngine.UIElements;

namespace ScreenPocket
{
    /// <summary>
    /// テクスチャノード
    /// </summary>
    public sealed class TextureNode : NodeBase
    {
        private Image _image;
        public Texture texture => _image.image;

        public TextureNode() : base()
        {
            title = "Texture";

            var outputPort = Port.Create<Edge>(Orientation.Horizontal, Direction.Output, Port.Capacity.Multi, typeof(Texture2D));
            outputContainer.Add(outputPort);

            var objectField = new ObjectField
            {
                objectType = typeof(Texture2D),
                allowSceneObjects = false
            };

            _image = new Image(); //表示用

            objectField.RegisterCallback<ChangeEvent<Object>>(e =>
            {
                _image.image = (Texture2D)e.newValue;
            });

            mainContainer.Add(objectField);
            mainContainer.Add(_image);
        }
    }
}

ポイントはRegisterCallbackでしょうか。
テクスチャを扱うObjectFieldが欲しかった事もあり

objectField.RegisterCallback<ChangeEvent<Texture2D>>(e =>
            {
                _image.image = e.newValue;
            });

としようとした所、コールバックが呼ばれず、

objectField.RegisterCallback<ChangeEvent<Object>>(e =>
            {
                _image.image = (Texture2D)e.newValue;
            });

こうしなければならなかった点で少し時間を食いました。

ちなみにこの状態だと、テクスチャが原寸大に表示されてしまい、1024x1024だと大きすぎるので

objectField.RegisterCallback<ChangeEvent<Object>>(e =>
            {
                _image.image = (Texture2D)e.newValue;
                _image.sourceRect = new Rect(0f,0f,100f,100f);
            });

として100x100で縮小表示しようとしましたが、どうにも上手く縮小できないので調査中です。
いっそプレビューはノードから外した方が良いかも??

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1