KITASENJU DESIGN BLOG

memo, html, javascript, unity

Unity+Macでファイルのドラッグ&ドロップ

以下を使わせていただきました。 UniDragAndDropForMac

GitHub - baobao/UniDragAndDropForMac: A library that implements drag and drop in Unity's macOS build

でも、使い方に悪戦苦闘しました。以下サンプルコードです。

  • WWWの使い方
  • IEnumeratorの使い方
  • Macのローカルファイルパスには"file:///"をつける

と言うところに苦戦しました。

using System.Collections;
using System.Collections.Generic;
using Shibuya24.Utility;
using UnityEngine;
using UnityEngine.UI;

public class DragAndDropSample : MonoBehaviour
{
    //[SerializeField] private Text _text;
    private Texture2D _tex;
    private string _url;

    void Start()
    {
        _tex = new Texture2D(1024,1024);
        //UniDragAndDrop.onDragAndDropFilePath = x => _text.text = x;
        UniDragAndDrop.onDragAndDropFilePath = onLoad;//x => _text.text = x;
        UniDragAndDrop.Initialize();
    }


    private void OnGUI(){

        GUI.Label(new Rect(500, 500, 500, 500), _url);
        GUI.DrawTexture(new Rect(300, 300, 200, 200), _tex, ScaleMode.StretchToFill);
    
    }
    private void onLoad(string url){
        //_text.text=url;
        _url = "file:///"+url;

        StartCoroutine("onLoad2");
    }

    IEnumerator onLoad2(){

        Debug.Log("onLoad" + _url);
        
        WWW www = new WWW(_url); 
        yield return www; 
        www.LoadImageIntoTexture(_tex);
    }


}
"FOOTER"