您最多选择25个主题
主题必须以中文或者字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
142 行
4.3 KiB
142 行
4.3 KiB
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.UIElements;
|
|
|
|
namespace Unity.MegaCity.UI
|
|
{
|
|
public enum ECharacterClass
|
|
{
|
|
Knight,
|
|
Ranger,
|
|
Wizard
|
|
}
|
|
|
|
[CreateAssetMenu]
|
|
public class CharacterData : ScriptableObject
|
|
{
|
|
public string m_CharacterName;
|
|
public ECharacterClass m_Class;
|
|
public Sprite m_PortraitImage;
|
|
}
|
|
|
|
public class CharacterListEntryController
|
|
{
|
|
Label m_NameLabel;
|
|
|
|
public void SetVisualElement(VisualElement visualElement)
|
|
{
|
|
m_NameLabel = visualElement.Q<Label>("Name");
|
|
}
|
|
|
|
public void SetCharacterData(CharacterData characterData)
|
|
{
|
|
m_NameLabel.text = characterData.m_CharacterName;
|
|
}
|
|
}
|
|
|
|
public class CharacterListController
|
|
{
|
|
// UXML template for list entries
|
|
VisualTreeAsset m_ListEntryTemplate;
|
|
|
|
// UI element references
|
|
ListView m_CharacterList;
|
|
Label m_CharClassLabel;
|
|
Label m_CharNameLabel;
|
|
|
|
public void InitializeCharacterList(VisualElement root, VisualTreeAsset listElementTemplate)
|
|
{
|
|
EnumerateAllCharacters();
|
|
|
|
// Store a reference to the template for the list entries
|
|
m_ListEntryTemplate = listElementTemplate;
|
|
|
|
// Store a reference to the character list element
|
|
m_CharacterList = root.Q<ListView>("List");
|
|
|
|
// Store references to the selected character info elements
|
|
m_CharClassLabel = root.Q<Label>("CharacterClass");
|
|
m_CharNameLabel = root.Q<Label>("Name");
|
|
|
|
FillCharacterList();
|
|
|
|
// Register to get a callback when an item is selected
|
|
m_CharacterList.onSelectionChange += OnCharacterSelected;
|
|
}
|
|
|
|
List<CharacterData> m_AllCharacters = new()
|
|
{
|
|
new()
|
|
{
|
|
m_CharacterName = "1111",
|
|
},
|
|
new()
|
|
{
|
|
m_CharacterName = "122111",
|
|
},
|
|
|
|
};
|
|
|
|
void EnumerateAllCharacters()
|
|
{
|
|
m_AllCharacters = new()
|
|
{
|
|
new()
|
|
{
|
|
m_CharacterName = "1111",
|
|
},
|
|
new()
|
|
{
|
|
m_CharacterName = "122111",
|
|
}
|
|
};
|
|
m_AllCharacters.AddRange(Resources.LoadAll<CharacterData>("Characters"));
|
|
}
|
|
|
|
void FillCharacterList()
|
|
{
|
|
// Set up a make item function for a list entry
|
|
m_CharacterList.makeItem = () =>
|
|
{
|
|
// Instantiate the UXML template for the entry
|
|
var newListEntry = m_ListEntryTemplate.Instantiate();
|
|
|
|
// Instantiate a controller for the data
|
|
var newListEntryLogic = new CharacterListEntryController();
|
|
|
|
// Assign the controller script to the visual element
|
|
newListEntry.userData = newListEntryLogic;
|
|
|
|
// Initialize the controller script
|
|
newListEntryLogic.SetVisualElement(newListEntry);
|
|
|
|
// Return the root of the instantiated visual tree
|
|
return newListEntry;
|
|
};
|
|
|
|
// Set up bind function for a specific list entry
|
|
m_CharacterList.bindItem = (item, index) =>
|
|
{
|
|
(item.userData as CharacterListEntryController).SetCharacterData(m_AllCharacters[index]);
|
|
};
|
|
|
|
// Set a fixed item height
|
|
m_CharacterList.fixedItemHeight = 45;
|
|
|
|
// Set the actual item's source list/array
|
|
m_CharacterList.itemsSource = m_AllCharacters;
|
|
}
|
|
|
|
void OnCharacterSelected(IEnumerable<object> selectedItems)
|
|
{
|
|
// Get the currently selected item directly from the ListView
|
|
var selectedCharacter = m_CharacterList.selectedItem as CharacterData;
|
|
|
|
|
|
// Fill in character details
|
|
m_CharClassLabel.text = selectedCharacter.m_Class.ToString();
|
|
m_CharNameLabel.text = selectedCharacter.m_CharacterName;
|
|
|
|
}
|
|
}
|
|
}
|