Skip to main content
se añadieron 11559 caracteres en el cuerpo
Origen Enlace

Edit 1: Agregue el código de mi proyecto. Esta hecho en Unity

GalleryItem.cs

    public class GalleryItem : MonoBehaviour
{
    #region VARIABLES

    [SerializeField]
    private GalleryContextMenu contextMenu;

    [Header("Settings")]

    [SerializeField]
    internal Color normalItemColor = Color.white;

    [SerializeField]
    internal Color hoveredItemColor = new Color32(225, 225, 255, 255);

    [SerializeField]
    internal Color selectedItemColor = new Color32(0, 175, 255, 255);

    [Serializable]
    private struct FiletypeIcon
    {
        public string extension;
        public Sprite icon;
    }

    [Header("Icons")]

    [SerializeField]
    private FiletypeIcon[] filetypeIcons;

    //Listas
    private readonly List<int> selectedFileEntries = new List<int>(4);

    private readonly List<FileItem> allItems = new List<FileItem>(16);

    private readonly List<WidgetItem> allWidgetItems = new List<WidgetItem>();

    private List<GameObject> buttonFileList = new List<GameObject>();

    internal List<string> allFiles = new List<string>();

    //Indice para cada item
    internal int sharedValue;

    internal string fullFilePath;

    //Cantidad de items
    private int items;

    [Header("Canvas")]

    //Contenedor de items
    public RectTransform content;

    //Item a instanciar
    public GameObject itemPrefab;

    //Canvas que contiene la galeria de items
    public Canvas canvas;
    public RectTransform rectTransform;

    #endregion

    #region MÉTODOS DE INICIALIZACIÓN

    private void Awake()
    {
        items = 0;
    }

    private void InitializeDataItem()
    {
        for (int j = 0; j < filetypeIcons.Length; j++)
        {
            if (fullFilePath.Contains(filetypeIcons[j].extension))
                allItems[items].SetFile(filetypeIcons[j].icon, Path.GetFileNameWithoutExtension(fullFilePath));
        }
    }

    #endregion

    #region MÉTODOS PARA ABRIR EL EXPLORADOR DE ARCHIVOS

    public void StartFileBrowser()
    {
        StartCoroutine(ShowLoadingCoroutine());
    }

    IEnumerator ShowLoadingCoroutine()
    {
        yield return FileBrowser.WaitForLoadDialog(FileBrowser.PickMode.FilesAndFolders, true, null, null, "Abrir archivos y carpetas", "Cargar");

        if (FileBrowser.Success)
        {
            fullFilePath = FileBrowser.Result[0].ToLowerInvariant();
            OnCreateItem("item", itemPrefab, content);
            OnCreateWidgetItem("WidgetButton");
        }
    }

    #endregion

    #region MÉTODO PARA AGREGAR Y ELEIMINAR ELEMENTOS DE LA GALERÍA
    public void DeleteItem()
    {
        Destroy(buttonFileList[sharedValue]);
        Destroy(allItems[sharedValue]);

        buttonFileList.RemoveAt(sharedValue);
        allItems.RemoveAt(sharedValue);
        allWidgetItems.RemoveAt(sharedValue);
        allFiles.RemoveAt(sharedValue);

        items -= 1;
        Refresh();
    }

    private void Refresh()
    {
        for (int i = 0; i < buttonFileList.Count; i++)
            buttonFileList[i].name = "item_" + i;

        for (int i = 0; i < allWidgetItems.Count; i++)
        {
            allWidgetItems[i].gameObject.name = "WidgetButton" + i;
        }

        if (buttonFileList.Count == 0)
            items = 0;
    }

    private void OnCreateWidgetItem(string name)
    {
        WidgetItem widget = GameObject.Find("WidgetButton").GetComponentInChildren<WidgetItem>();
        widget.name = name + "_" + (items - 1);

        allWidgetItems.Add(widget);
    }

    private void OnCreateItem(string name, GameObject prefab, Transform parent)
    {
        FileItem item = (FileItem)Instantiate(prefab.GetComponent<FileItem>(), parent, false);            

        item.name = name + "_" + items;

        buttonFileList.Add(item.gameObject);
        allItems.Add(item);
        allFiles.Add(fullFilePath);

        InitializeDataItem();

        items += 1;
    }

    #endregion

    #region MENÚ CONTEXTUAL

    internal void OnContextMenuTriggered()
    {
        Vector2 position;
        RectTransformUtility.ScreenPointToLocalPointInRectangle(rectTransform, Input.mousePosition, canvas.worldCamera, out position);

        contextMenu.Show(position, false);
    }

    #endregion

    #region HOVER PARA ELEMENTOS

    public void OnItemSelected(FileItem item, bool isDobleClick)
    {
        if (item == null)
            return;

        if (!isDobleClick)
        {
            selectedFileEntries.Clear();
            selectedFileEntries.Add(item.Position);
        }

        for (int i = 0; i < allItems.Count; i++)
        {
            if (allItems[i].gameObject.activeSelf)
                allItems[i].SetSelected(false);
        }
        item.SetSelected(selectedFileEntries.Contains(item.Position));
    }

    public void OnSelectedWidgetItem()
    {
        for (int i = 0; i < allWidgetItems.Count; i++)
        {
            allWidgetItems[i].isPlaying = false;
            allWidgetItems[i].PlayingNull();
        }
    }

#endregion }

FileItem.cs

    public class GalleryItem : MonoBehaviour
{
    #region VARIABLES

    [SerializeField]
    private GalleryContextMenu contextMenu;

    [Header("Settings")]

    [SerializeField]
    internal Color normalItemColor = Color.white;

    [SerializeField]
    internal Color hoveredItemColor = new Color32(225, 225, 255, 255);

    [SerializeField]
    internal Color selectedItemColor = new Color32(0, 175, 255, 255);

    [Serializable]
    private struct FiletypeIcon
    {
        public string extension;
        public Sprite icon;
    }

    [Header("Icons")]

    [SerializeField]
    private FiletypeIcon[] filetypeIcons;

    //Listas
    private readonly List<int> selectedFileEntries = new List<int>(4);

    private readonly List<FileItem> allItems = new List<FileItem>(16);

    private readonly List<WidgetItem> allWidgetItems = new List<WidgetItem>();

    private List<GameObject> buttonFileList = new List<GameObject>();

    internal List<string> allFiles = new List<string>();

    //Indice para cada item
    internal int sharedValue;

    internal string fullFilePath;

    //Cantidad de items
    private int items;

    [Header("Canvas")]

    //Contenedor de items
    public RectTransform content;

    //Item a instanciar
    public GameObject itemPrefab;

    //Canvas que contiene la galeria de items
    public Canvas canvas;
    public RectTransform rectTransform;

    #endregion

    #region MÉTODOS DE INICIALIZACIÓN

    private void Awake()
    {
        items = 0;
    }

    private void InitializeDataItem()
    {
        for (int j = 0; j < filetypeIcons.Length; j++)
        {
            if (fullFilePath.Contains(filetypeIcons[j].extension))
                allItems[items].SetFile(filetypeIcons[j].icon, Path.GetFileNameWithoutExtension(fullFilePath));
        }
    }

    #endregion

    #region MÉTODOS PARA ABRIR EL EXPLORADOR DE ARCHIVOS

    public void StartFileBrowser()
    {
        StartCoroutine(ShowLoadingCoroutine());
    }

    IEnumerator ShowLoadingCoroutine()
    {
        yield return FileBrowser.WaitForLoadDialog(FileBrowser.PickMode.FilesAndFolders, true, null, null, "Abrir archivos y carpetas", "Cargar");

        if (FileBrowser.Success)
        {
            fullFilePath = FileBrowser.Result[0].ToLowerInvariant();
            OnCreateItem("item", itemPrefab, content);
            OnCreateWidgetItem("WidgetButton");
        }
    }

    #endregion

    #region MÉTODO PARA AGREGAR Y ELEIMINAR ELEMENTOS DE LA GALERÍA
    public void DeleteItem()
    {
        Destroy(buttonFileList[sharedValue]);
        Destroy(allItems[sharedValue]);

        buttonFileList.RemoveAt(sharedValue);
        allItems.RemoveAt(sharedValue);
        allWidgetItems.RemoveAt(sharedValue);
        allFiles.RemoveAt(sharedValue);

        items -= 1;
        Refresh();
    }

    private void Refresh()
    {
        for (int i = 0; i < buttonFileList.Count; i++)
            buttonFileList[i].name = "item_" + i;

        for (int i = 0; i < allWidgetItems.Count; i++)
        {
            allWidgetItems[i].gameObject.name = "WidgetButton" + i;
        }

        if (buttonFileList.Count == 0)
            items = 0;
    }

    private void OnCreateWidgetItem(string name)
    {
        WidgetItem widget = GameObject.Find("WidgetButton").GetComponentInChildren<WidgetItem>();
        widget.name = name + "_" + (items - 1);

        allWidgetItems.Add(widget);
    }

    private void OnCreateItem(string name, GameObject prefab, Transform parent)
    {
        FileItem item = (FileItem)Instantiate(prefab.GetComponent<FileItem>(), parent, false);            

        item.name = name + "_" + items;

        buttonFileList.Add(item.gameObject);
        allItems.Add(item);
        allFiles.Add(fullFilePath);

        InitializeDataItem();

        items += 1;
    }

    #endregion

    #region MENÚ CONTEXTUAL

    internal void OnContextMenuTriggered()
    {
        Vector2 position;
        RectTransformUtility.ScreenPointToLocalPointInRectangle(rectTransform, Input.mousePosition, canvas.worldCamera, out position);

        contextMenu.Show(position, false);
    }

    #endregion

    #region HOVER PARA ELEMENTOS

    public void OnItemSelected(FileItem item, bool isDobleClick)
    {
        if (item == null)
            return;

        if (!isDobleClick)
        {
            selectedFileEntries.Clear();
            selectedFileEntries.Add(item.Position);
        }

        for (int i = 0; i < allItems.Count; i++)
        {
            if (allItems[i].gameObject.activeSelf)
                allItems[i].SetSelected(false);
        }
        item.SetSelected(selectedFileEntries.Contains(item.Position));
    }

    public void OnSelectedWidgetItem()
    {
        for (int i = 0; i < allWidgetItems.Count; i++)
        {
            allWidgetItems[i].isPlaying = false;
            allWidgetItems[i].PlayingNull();
        }
    }

#endregion }

Esos son los scripts principales.

Edit 1: Agregue el código de mi proyecto. Esta hecho en Unity

GalleryItem.cs

    public class GalleryItem : MonoBehaviour
{
    #region VARIABLES

    [SerializeField]
    private GalleryContextMenu contextMenu;

    [Header("Settings")]

    [SerializeField]
    internal Color normalItemColor = Color.white;

    [SerializeField]
    internal Color hoveredItemColor = new Color32(225, 225, 255, 255);

    [SerializeField]
    internal Color selectedItemColor = new Color32(0, 175, 255, 255);

    [Serializable]
    private struct FiletypeIcon
    {
        public string extension;
        public Sprite icon;
    }

    [Header("Icons")]

    [SerializeField]
    private FiletypeIcon[] filetypeIcons;

    //Listas
    private readonly List<int> selectedFileEntries = new List<int>(4);

    private readonly List<FileItem> allItems = new List<FileItem>(16);

    private readonly List<WidgetItem> allWidgetItems = new List<WidgetItem>();

    private List<GameObject> buttonFileList = new List<GameObject>();

    internal List<string> allFiles = new List<string>();

    //Indice para cada item
    internal int sharedValue;

    internal string fullFilePath;

    //Cantidad de items
    private int items;

    [Header("Canvas")]

    //Contenedor de items
    public RectTransform content;

    //Item a instanciar
    public GameObject itemPrefab;

    //Canvas que contiene la galeria de items
    public Canvas canvas;
    public RectTransform rectTransform;

    #endregion

    #region MÉTODOS DE INICIALIZACIÓN

    private void Awake()
    {
        items = 0;
    }

    private void InitializeDataItem()
    {
        for (int j = 0; j < filetypeIcons.Length; j++)
        {
            if (fullFilePath.Contains(filetypeIcons[j].extension))
                allItems[items].SetFile(filetypeIcons[j].icon, Path.GetFileNameWithoutExtension(fullFilePath));
        }
    }

    #endregion

    #region MÉTODOS PARA ABRIR EL EXPLORADOR DE ARCHIVOS

    public void StartFileBrowser()
    {
        StartCoroutine(ShowLoadingCoroutine());
    }

    IEnumerator ShowLoadingCoroutine()
    {
        yield return FileBrowser.WaitForLoadDialog(FileBrowser.PickMode.FilesAndFolders, true, null, null, "Abrir archivos y carpetas", "Cargar");

        if (FileBrowser.Success)
        {
            fullFilePath = FileBrowser.Result[0].ToLowerInvariant();
            OnCreateItem("item", itemPrefab, content);
            OnCreateWidgetItem("WidgetButton");
        }
    }

    #endregion

    #region MÉTODO PARA AGREGAR Y ELEIMINAR ELEMENTOS DE LA GALERÍA
    public void DeleteItem()
    {
        Destroy(buttonFileList[sharedValue]);
        Destroy(allItems[sharedValue]);

        buttonFileList.RemoveAt(sharedValue);
        allItems.RemoveAt(sharedValue);
        allWidgetItems.RemoveAt(sharedValue);
        allFiles.RemoveAt(sharedValue);

        items -= 1;
        Refresh();
    }

    private void Refresh()
    {
        for (int i = 0; i < buttonFileList.Count; i++)
            buttonFileList[i].name = "item_" + i;

        for (int i = 0; i < allWidgetItems.Count; i++)
        {
            allWidgetItems[i].gameObject.name = "WidgetButton" + i;
        }

        if (buttonFileList.Count == 0)
            items = 0;
    }

    private void OnCreateWidgetItem(string name)
    {
        WidgetItem widget = GameObject.Find("WidgetButton").GetComponentInChildren<WidgetItem>();
        widget.name = name + "_" + (items - 1);

        allWidgetItems.Add(widget);
    }

    private void OnCreateItem(string name, GameObject prefab, Transform parent)
    {
        FileItem item = (FileItem)Instantiate(prefab.GetComponent<FileItem>(), parent, false);            

        item.name = name + "_" + items;

        buttonFileList.Add(item.gameObject);
        allItems.Add(item);
        allFiles.Add(fullFilePath);

        InitializeDataItem();

        items += 1;
    }

    #endregion

    #region MENÚ CONTEXTUAL

    internal void OnContextMenuTriggered()
    {
        Vector2 position;
        RectTransformUtility.ScreenPointToLocalPointInRectangle(rectTransform, Input.mousePosition, canvas.worldCamera, out position);

        contextMenu.Show(position, false);
    }

    #endregion

    #region HOVER PARA ELEMENTOS

    public void OnItemSelected(FileItem item, bool isDobleClick)
    {
        if (item == null)
            return;

        if (!isDobleClick)
        {
            selectedFileEntries.Clear();
            selectedFileEntries.Add(item.Position);
        }

        for (int i = 0; i < allItems.Count; i++)
        {
            if (allItems[i].gameObject.activeSelf)
                allItems[i].SetSelected(false);
        }
        item.SetSelected(selectedFileEntries.Contains(item.Position));
    }

    public void OnSelectedWidgetItem()
    {
        for (int i = 0; i < allWidgetItems.Count; i++)
        {
            allWidgetItems[i].isPlaying = false;
            allWidgetItems[i].PlayingNull();
        }
    }

#endregion }

FileItem.cs

    public class GalleryItem : MonoBehaviour
{
    #region VARIABLES

    [SerializeField]
    private GalleryContextMenu contextMenu;

    [Header("Settings")]

    [SerializeField]
    internal Color normalItemColor = Color.white;

    [SerializeField]
    internal Color hoveredItemColor = new Color32(225, 225, 255, 255);

    [SerializeField]
    internal Color selectedItemColor = new Color32(0, 175, 255, 255);

    [Serializable]
    private struct FiletypeIcon
    {
        public string extension;
        public Sprite icon;
    }

    [Header("Icons")]

    [SerializeField]
    private FiletypeIcon[] filetypeIcons;

    //Listas
    private readonly List<int> selectedFileEntries = new List<int>(4);

    private readonly List<FileItem> allItems = new List<FileItem>(16);

    private readonly List<WidgetItem> allWidgetItems = new List<WidgetItem>();

    private List<GameObject> buttonFileList = new List<GameObject>();

    internal List<string> allFiles = new List<string>();

    //Indice para cada item
    internal int sharedValue;

    internal string fullFilePath;

    //Cantidad de items
    private int items;

    [Header("Canvas")]

    //Contenedor de items
    public RectTransform content;

    //Item a instanciar
    public GameObject itemPrefab;

    //Canvas que contiene la galeria de items
    public Canvas canvas;
    public RectTransform rectTransform;

    #endregion

    #region MÉTODOS DE INICIALIZACIÓN

    private void Awake()
    {
        items = 0;
    }

    private void InitializeDataItem()
    {
        for (int j = 0; j < filetypeIcons.Length; j++)
        {
            if (fullFilePath.Contains(filetypeIcons[j].extension))
                allItems[items].SetFile(filetypeIcons[j].icon, Path.GetFileNameWithoutExtension(fullFilePath));
        }
    }

    #endregion

    #region MÉTODOS PARA ABRIR EL EXPLORADOR DE ARCHIVOS

    public void StartFileBrowser()
    {
        StartCoroutine(ShowLoadingCoroutine());
    }

    IEnumerator ShowLoadingCoroutine()
    {
        yield return FileBrowser.WaitForLoadDialog(FileBrowser.PickMode.FilesAndFolders, true, null, null, "Abrir archivos y carpetas", "Cargar");

        if (FileBrowser.Success)
        {
            fullFilePath = FileBrowser.Result[0].ToLowerInvariant();
            OnCreateItem("item", itemPrefab, content);
            OnCreateWidgetItem("WidgetButton");
        }
    }

    #endregion

    #region MÉTODO PARA AGREGAR Y ELEIMINAR ELEMENTOS DE LA GALERÍA
    public void DeleteItem()
    {
        Destroy(buttonFileList[sharedValue]);
        Destroy(allItems[sharedValue]);

        buttonFileList.RemoveAt(sharedValue);
        allItems.RemoveAt(sharedValue);
        allWidgetItems.RemoveAt(sharedValue);
        allFiles.RemoveAt(sharedValue);

        items -= 1;
        Refresh();
    }

    private void Refresh()
    {
        for (int i = 0; i < buttonFileList.Count; i++)
            buttonFileList[i].name = "item_" + i;

        for (int i = 0; i < allWidgetItems.Count; i++)
        {
            allWidgetItems[i].gameObject.name = "WidgetButton" + i;
        }

        if (buttonFileList.Count == 0)
            items = 0;
    }

    private void OnCreateWidgetItem(string name)
    {
        WidgetItem widget = GameObject.Find("WidgetButton").GetComponentInChildren<WidgetItem>();
        widget.name = name + "_" + (items - 1);

        allWidgetItems.Add(widget);
    }

    private void OnCreateItem(string name, GameObject prefab, Transform parent)
    {
        FileItem item = (FileItem)Instantiate(prefab.GetComponent<FileItem>(), parent, false);            

        item.name = name + "_" + items;

        buttonFileList.Add(item.gameObject);
        allItems.Add(item);
        allFiles.Add(fullFilePath);

        InitializeDataItem();

        items += 1;
    }

    #endregion

    #region MENÚ CONTEXTUAL

    internal void OnContextMenuTriggered()
    {
        Vector2 position;
        RectTransformUtility.ScreenPointToLocalPointInRectangle(rectTransform, Input.mousePosition, canvas.worldCamera, out position);

        contextMenu.Show(position, false);
    }

    #endregion

    #region HOVER PARA ELEMENTOS

    public void OnItemSelected(FileItem item, bool isDobleClick)
    {
        if (item == null)
            return;

        if (!isDobleClick)
        {
            selectedFileEntries.Clear();
            selectedFileEntries.Add(item.Position);
        }

        for (int i = 0; i < allItems.Count; i++)
        {
            if (allItems[i].gameObject.activeSelf)
                allItems[i].SetSelected(false);
        }
        item.SetSelected(selectedFileEntries.Contains(item.Position));
    }

    public void OnSelectedWidgetItem()
    {
        for (int i = 0; i < allWidgetItems.Count; i++)
        {
            allWidgetItems[i].isPlaying = false;
            allWidgetItems[i].PlayingNull();
        }
    }

#endregion }

Esos son los scripts principales.

Origen Enlace

Llamar método o clase usando generics

tengo una duda sobre como se implementan los generics y espero me puedan apoyar o guiar para implementarlo en mi en mi caso.

Bien, estoy trabajando en una lista de reproducción de archivos o una galería y este va poder reproducir varios tipos de formato de archivos(mp3, jpg, mp4, obj, ect) entonces quiero evitar modificar mi código para cada tipo de método que se encargue de importar estos tipos e chivos. He estado leyendo sobre generics y veo que si soluciona mi problema pero no se como implementarlo.

Podrían apoyarme con esto? o esto que estoy intentando hacer es posible?

Sin mas. Muchas gracias!