Home > OS >  Unity3d UI Toolkit how to trigger event on clicking project file?
Unity3d UI Toolkit how to trigger event on clicking project file?

Time:05-29

I have created UI Toolkit view for particular file types, preview of sort. Now I want to update my view when user select some file in Project tab (i.e. selecting some texture in project tab and I should show it in my UI Toolkit tab). The only way I see right now is to create file explorer in my view which seems too much and I don't want to do this.

How I can do this? Where to register/unregister (in case window is closed) this callback for my root VisualElement and get file path?

CodePudding user response:

As far as i know there is no event relating to the project window that can be registered to. You could look into seeing if you'd be able to get data from it with reflection but you'll have to reverse engineer it.

The c# source code for the project browser can be found here.

There does however seem to be a way to get a selection but i'm not very familliar with it. http://answers.unity.com/answers/1640054/view.html

CodePudding user response:

Found an answer myself. Wrapped it up into visual element which you can put into root and listen for currently selected file:

public class FileExplorerEventListener : VisualElement
{        
    private readonly string _fileExtension;
    private UnityEngine.Object _currentAsset;

    public UnityEngine.Object CurrentAsset => _currentAsset;
    public string FileExtension => _fileExtension;
    public string CurrentFilePath => GetFilePath(_currentAsset);
    public event EventHandler<FileExplorerCurrentFileChangedEventArgs> OnCurrentFileChanged;

    public class FileExplorerCurrentFileChangedEventArgs : EventArgs
    {
        public string FileExtension { get; set; }

        public string FilePath { get; set; }

        public UnityEngine.Object Asset { get; set; }
    }

    public FileExplorerEventListener(string fileExtension)
    {
        _fileExtension = fileExtension;
        this.visible = false;
    }

    public void Update()
    {
        if (!EditorApplication.isPlaying)
        {
            var obj = Selection.activeObject;

            if (obj != _currentAsset)
            {
                var filePath = GetFilePath(obj);
                if (filePath != null && filePath.EndsWith(_fileExtension, StringComparison.InvariantCultureIgnoreCase))
                {
                    _currentAsset = obj;

                    OnCurrentFileChanged?.Invoke(this, new FileExplorerCurrentFileChangedEventArgs
                    {
                        Asset = obj,
                        FilePath = filePath,
                        FileExtension = _fileExtension
                    });
                }
            }
        }
    }

    private static string GetFilePath(UnityEngine.Object obj)
    {
        if (obj == null)
            return null;
        try
        {
            return AssetDatabase.GetAssetPath(obj);
        }
        catch
        {
            return null;
        }
    }
}

public void CreateGUI()
{
    // Each editor window contains a root VisualElement object
    var root = rootVisualElement;
    
    _fileListener = new FileExplorerEventListener(".pef.json");
    _fileListener.OnCurrentFileChanged  = _fileListener_OnCurrentFileChanged;
    root.Add(_fileListener);
}
public void Update()
{
    _fileListener.Update();
}

private void _fileListener_OnCurrentFileChanged(object sender, FileExplorerEventListener.FileExplorerCurrentFileChangedEventArgs e)
{
    var deserialized = JsonHelper.Deserialize<ProceduralView.ProceduralViewDto>(((TextAsset)e.Asset).text);
    proceduraView.Populate(deserialized);
}
  • Related