yasirkula / UnitySimpleFileBrowser

A uGUI based runtime file browser for Unity 3D (draggable and resizable)
MIT License
851 stars 111 forks source link

simple file browser for vr systems #22

Closed alitokur closed 4 years ago

alitokur commented 4 years ago

hi, is there any way to use this file browser for vr systems. I tried some solutions, -using a pointer instead of a mouse. -changing the canvas render mode "overlay to wordspace." in generally some of my applications work well, but "SimpleFileBrowserWindow" under the "SimpleFileBrowserCanvas" often cause problems.

yasirkula commented 4 years ago

Your workflow is correct. You just need to keep the file browser window active in your scene so that its Awake function is called and it sets itself as the singleton instance. You can deactivate the window in your Start function by calling FileBrowser.Hide(); if you want. If you are already doing these, then what sort of problems are occurring?

alitokur commented 4 years ago

I'm doing this for a mobile app and using a eyeRaycast to select objects. If the pointer stays on the object for a certain time, it can select it. As you can see in the pictures, "TopView" and "BottomWiew" are working well.

issue_1 issue_2 but I can't get any response in "MidWiew".

issue_3

i guess, the problem is related to the eyeRaycast script I use.

 List<RaycastResult> raycastResults = new List<RaycastResult>();
        m_eventSystem.RaycastAll(m_pointerEvent, raycastResults);

        // Detect selectable
        if (raycastResults.Count > 0)
        {
            foreach(var result in raycastResults)
            {
                var newSelectable = result.gameObject.GetComponentInParent<Selectable>();

                if (newSelectable)
                {
                   // Debug.Log("seçilebilir");
                    if (newSelectable != m_excluded && newSelectable != m_currentSelectable)
                    {
                        Select(newSelectable);
                        m_currentRaycastResult = result;
                    }
                    break;
                } 
                    //Debug.Log("secilemez");
            }
        }
        else
        {
            if(m_currentSelectable || m_excluded)
            {
                Select(null, null);
            }
        }

When i use the "Selectable", probably cannot select anything other than buttons,fields and scrollbar. This may be a weird issue so im sorry, this my first time with unit and c#.

yasirkula commented 4 years ago

Please make the following changes:

  1. ListItem.cs
using UnityEngine;

namespace SimpleFileBrowser
{
    [RequireComponent( typeof( RectTransform ) )]
    public class ListItem : UnityEngine.UI.Selectable
    {
        public object Tag { get; set; }
        public int Position { get; set; }

        private IListViewAdapter adapter;

        internal void SetAdapter( IListViewAdapter listView )
        {
            this.adapter = listView;
        }

        public override void Select()
        {
            base.Select();

            if( this is FileBrowserQuickLink )
                ( (FileBrowserQuickLink) this ).OnPointerClick( null );
            else if( this is FileBrowserItem )
                ( (FileBrowserItem) this ).OnPointerClick( null );
        }

        public void OnClick()
        {
            if( adapter.OnItemClicked != null )
                adapter.OnItemClicked( this );
        }
    }
}
  1. FileBrowserItem.cs
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;

namespace SimpleFileBrowser
{
    public class FileBrowserItem : ListItem, IPointerClickHandler, IPointerEnterHandler, IPointerExitHandler
    {
        #region Constants
        private const float DOUBLE_CLICK_TIME = 0.5f;
        #endregion

        #region Variables
        protected FileBrowser fileBrowser;

#pragma warning disable 0649
        [SerializeField]
        private Image background;

        [SerializeField]
        private Image icon;

        [SerializeField]
        private Text nameText;
#pragma warning restore 0649

        private float prevTouchTime = Mathf.NegativeInfinity;
        #endregion

        #region Properties
        private RectTransform m_transform;
        public RectTransform TransformComponent
        {
            get
            {
                if( m_transform == null )
                    m_transform = (RectTransform) transform;

                return m_transform;
            }
        }

        public string Name { get { return nameText.text; } }
        public bool IsDirectory { get; private set; }
        #endregion

        #region Initialization Functions
        public void SetFileBrowser( FileBrowser fileBrowser )
        {
            this.fileBrowser = fileBrowser;
        }

        public void SetFile( Sprite icon, string name, bool isDirectory )
        {
            this.icon.sprite = icon;
            nameText.text = name;

            IsDirectory = isDirectory;
        }
        #endregion

        #region Pointer Events
        public void OnPointerClick( PointerEventData eventData )
        {
            if( FileBrowser.SingleClickMode )
            {
                fileBrowser.OnItemSelected( this );
                fileBrowser.OnItemOpened( this );
            }
            else
            {
                if( Time.realtimeSinceStartup - prevTouchTime < DOUBLE_CLICK_TIME )
                {
                    if( fileBrowser.SelectedFilePosition == Position )
                        fileBrowser.OnItemOpened( this );

                    prevTouchTime = Mathf.NegativeInfinity;
                }
                else
                {
                    fileBrowser.OnItemSelected( this );
                    prevTouchTime = Time.realtimeSinceStartup;
                }
            }
        }

        public override void OnPointerEnter( PointerEventData eventData )
        {
            base.OnPointerEnter( eventData );

#if UNITY_EDITOR || ( !UNITY_ANDROID && !UNITY_IOS )
            if( fileBrowser.SelectedFilePosition != Position )
                background.color = fileBrowser.hoveredFileColor;
#endif
        }

        public override void OnPointerExit( PointerEventData eventData )
        {
            base.OnPointerExit( eventData );

#if UNITY_EDITOR || ( !UNITY_ANDROID && !UNITY_IOS )
            if( fileBrowser.SelectedFilePosition != Position )
                background.color = fileBrowser.normalFileColor;
#endif
        }
        #endregion

        #region Other Events
        public new void Select()
        {
            background.color = fileBrowser.selectedFileColor;
        }

        public void Deselect()
        {
            background.color = fileBrowser.normalFileColor;
        }

        public void SetHidden( bool isHidden )
        {
            Color c = icon.color;
            c.a = isHidden ? 0.5f : 1f;
            icon.color = c;

            c = nameText.color;
            c.a = isHidden ? 0.55f : 1f;
            nameText.color = c;
        }
        #endregion
    }
}
alitokur commented 4 years ago

huh, fixed! thank you sir! 🙏

sstamatis01 commented 1 year ago

Sorry to comment again on the same closed issue, but I was trying to add your simple file browser inside my VR application and I cant get raycast to select the files in the "MidView". Your proposed solution on FileBrowserItem.cs changes breaks the rest of the code in the current version of the package. Can you have a look again or at least give me some pointers as to what I should look into?

yasirkula commented 1 year ago

It must be related to ListItem now extending UnityEngine.UI.Selectable instead of MonoBehaviour. Can you check the code changes with that in mind? I'm currently unable to inspect this Issue due to my busy schedule.

sstamatis01 commented 1 year ago

Thanks for the quick reply and its ok if you cant have a look into it! The ListItem with UnityEngine.UI.Selectable doesn't break the current version of the package, but sadly raycast still doesn't work. I m not sure how the ListItem with UnityEngine.UI.Selectable affects FileBrowserItem.cs though. I will try to figure a solution somehow.