You've already forked UniversalViewer
							
							fixes
This commit is contained in:
		
							
								
								
									
										140
									
								
								Assets/DanMachi/Scripts/UI/CharacterDropdown.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										140
									
								
								Assets/DanMachi/Scripts/UI/CharacterDropdown.cs
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,140 @@ | ||||
| using TMPro; | ||||
| using System.Linq; | ||||
| using UnityEngine; | ||||
| using UnityEngine.Events; | ||||
| using System.Collections.Generic; | ||||
| using System; | ||||
|  | ||||
| public class CharacterDropdown : MonoBehaviour | ||||
| { | ||||
|     [SerializeField] private TMP_Dropdown MainDropdown; | ||||
|     [SerializeField] private TMP_InputField MainLabel; | ||||
|     public List<string> DropdownLabels = new List<string>() { "Character", "Costume" }; | ||||
|     public List<TMP_Dropdown> Dropdowns = new List<TMP_Dropdown>(); | ||||
|     public List<TMP_InputField> Labels = new List<TMP_InputField>(); | ||||
|     private bool _awake; | ||||
|  | ||||
|     private void Awake() | ||||
|     { | ||||
|         if (_awake) return; | ||||
|         _awake = true; | ||||
|         Rebuild(); | ||||
|     } | ||||
|  | ||||
|     private void Rebuild() | ||||
|     { | ||||
|         var labelParent = MainLabel.transform.parent; | ||||
|         var dropdownParent = MainDropdown.transform.parent; | ||||
|  | ||||
|         MainDropdown.ClearOptions(); | ||||
|         Dropdowns.Clear(); | ||||
|         Dropdowns.Add(MainDropdown); | ||||
|  | ||||
|         foreach (var child in dropdownParent.GetComponentsInChildren<TMP_Dropdown>()) | ||||
|         { | ||||
|             if (child != MainDropdown) Destroy(child); | ||||
|         } | ||||
|         foreach (var child in labelParent.GetComponentsInChildren<TMP_Dropdown>()) | ||||
|         { | ||||
|             if (child != MainLabel) Destroy(child); | ||||
|         } | ||||
|  | ||||
|         MainDropdown.name = DropdownLabels[0]; | ||||
|         MainLabel.text = DropdownLabels[0]; | ||||
|  | ||||
|         foreach (var label in DropdownLabels.Skip(1)) | ||||
|         { | ||||
|             var dd = Instantiate(MainDropdown, dropdownParent); | ||||
|             dd.name = "dropdown_" + label; | ||||
|             var ll = Instantiate(MainLabel, labelParent); | ||||
|             ll.name = label + " (TMP)"; | ||||
|             ll.text = label; | ||||
|             Dropdowns.Add(dd); | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     public void SetData(List<CharacterAsset> characters) | ||||
|     { | ||||
|         Awake(); | ||||
|         SetDropdownData(0, characters.Select(c => c.Category).Distinct().ToList(), true, true); | ||||
|  | ||||
|         UnityAction<int> groupCallback = (index) => | ||||
|         { | ||||
|             if (index == 0) return; | ||||
|  | ||||
|             var category = GetOption(0); | ||||
|             SetDropdownData(1, characters.Where(c => c.Category == category).Select(c => c.Id).Distinct().ToList(), true, true); | ||||
|             //SetDropdownData(2, new List<string>(), true, true); | ||||
|         }; | ||||
|  | ||||
|         UnityAction<int> charaCallback = (index) => | ||||
|         { | ||||
|             if (index == 0) return; | ||||
|  | ||||
|             var category = GetOption(0); | ||||
|             var id = GetOption(1); | ||||
|             var costumes = characters.Where(c => c.Category == category && c.Id == id); | ||||
|             var costume = costumes.FirstOrDefault(c => c.Costume == "common"); | ||||
|             costume ??= costumes.First(); | ||||
|             DanMachiModelBuilder.Instance.SpawnCharacter(costume); | ||||
|             //SetDropdownData(2, characters.Where(c => c.Category == category && c.Id == id).Select(c => c.Costume).ToList(), true, true); | ||||
|         }; | ||||
|  | ||||
|         //UnityAction<int> costumeCallback = (index) => | ||||
|         //{ | ||||
|         //    var chara = DanMachiAssetLibrary.Instance.GetCharacter(GetOption(0), GetOption(1), GetOption(2)); | ||||
|         //    StartCoroutine(DanMachiModelBuilder.Instance.SpawnAsset(AssetTypes.Character, chara)); | ||||
|         //}; | ||||
|  | ||||
|         SetCallbacks(new List<UnityAction<int>>() | ||||
|         { | ||||
|             groupCallback, | ||||
|             charaCallback | ||||
|         }); | ||||
|     } | ||||
|  | ||||
|     public void SetCallbacks(List<UnityAction<int>> callbacks) | ||||
|     { | ||||
|         if (callbacks.Count < Dropdowns.Count) | ||||
|         { | ||||
|             Error.Log(Color.red, $"Incorrect number of callbacks for dropdowns. Got {callbacks.Count}, required {Dropdowns.Count}"); | ||||
|         } | ||||
|  | ||||
|         for (int i = 0; i < Dropdowns.Count; i++) | ||||
|         { | ||||
|             Dropdowns[i].onValueChanged.RemoveAllListeners(); | ||||
|             Dropdowns[i].onValueChanged.AddListener(callbacks[i]); | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     public void OnDropdownSelected(int selection) | ||||
|     { | ||||
|         if (selection == 0) | ||||
|         { | ||||
|             Debug.LogError("Delete Current"); | ||||
|         } | ||||
|         StartCoroutine(ModelBuilder.GetInstance().SpawnAsset(AssetTypes.Character, MainDropdown.options[selection].text)); | ||||
|     } | ||||
|  | ||||
|     public void SetDropdownData(string label, List<string> options, bool nullValue = false, bool sortValues = false) | ||||
|     { | ||||
|         var depth = DropdownLabels.IndexOf(label); | ||||
|         SetDropdownData(depth, options, nullValue, sortValues); | ||||
|     } | ||||
|  | ||||
|     public string GetOption(int depth) | ||||
|     { | ||||
|         if (Dropdowns[depth].value == 0) return null; | ||||
|         return Dropdowns[depth].options[Dropdowns[depth].value].text; | ||||
|     } | ||||
|  | ||||
|     public void SetDropdownData(int depth, List<string> options, bool nullValue = false, bool sortValues = false) | ||||
|     { | ||||
|         if (depth >= Dropdowns.Count || depth < 0) | ||||
|         { | ||||
|             Error.Log(Color.red, $"Dropdown {depth} out of range"); | ||||
|             return; | ||||
|         } | ||||
|         ModelViewerInterface.SetDropdownData(Dropdowns[depth], options, nullValue, sortValues); | ||||
|     } | ||||
| } | ||||
							
								
								
									
										11
									
								
								Assets/DanMachi/Scripts/UI/CharacterDropdown.cs.meta
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										11
									
								
								Assets/DanMachi/Scripts/UI/CharacterDropdown.cs.meta
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,11 @@ | ||||
| fileFormatVersion: 2 | ||||
| guid: eaaf892cb6c1a98408a9e1e8e75c5b79 | ||||
| MonoImporter: | ||||
|   externalObjects: {} | ||||
|   serializedVersion: 2 | ||||
|   defaultReferences: [] | ||||
|   executionOrder: 0 | ||||
|   icon: {instanceID: 0} | ||||
|   userData:  | ||||
|   assetBundleName:  | ||||
|   assetBundleVariant:  | ||||
							
								
								
									
										63
									
								
								Assets/DanMachi/Scripts/UI/CharacterPanel.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										63
									
								
								Assets/DanMachi/Scripts/UI/CharacterPanel.cs
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,63 @@ | ||||
| using System; | ||||
| using System.Linq; | ||||
| using UnityEngine; | ||||
|  | ||||
| public class CharacterPanel : MonoBehaviour | ||||
| { | ||||
|     public ExtendedDropdown AnimationSetDropdown; | ||||
|     public ExtendedDropdown AnimationDropdown; | ||||
|     public ExtendedDropdown CostumeDropdown; | ||||
|     private DanMachiCharacterContainer _selectedChara; | ||||
|  | ||||
|     public static CharacterPanel Create(DanMachiCharacterContainer chara) | ||||
|     { | ||||
|         var panel = Instantiate(DanMachiAssetLibrary.Instance.CharacterPanel, ModelViewerInterface.GetInstance().DynamicPanels.transform); | ||||
|         panel.Init(chara); | ||||
|         return panel; | ||||
|     } | ||||
|  | ||||
|     public void Init(DanMachiCharacterContainer chara) | ||||
|     { | ||||
|         _selectedChara = chara; | ||||
|         CostumeDropdown.SetOptions(DanMachiAssetLibrary.Instance.Characters.Where(a => a.Category == chara.Data.Category && a.Id == chara.Data.Id).Select(a => a.Costume).ToList()); | ||||
|         CostumeDropdown.SetValueWithoutNotify(chara.Data.Costume); | ||||
|         AnimationSetDropdown.SetOptions(DanMachiAssetLibrary.Instance.Animations.Where(a => a.Category == chara.Data.Category && a.Id == chara.Data.Id).Select(a => a.Costume).ToList(), true); | ||||
|     } | ||||
|  | ||||
|     public void SelectCostume(int value) | ||||
|     { | ||||
|         var pose = _selectedChara.SerializeFrame(); | ||||
|         var data = _selectedChara.Data; | ||||
|         var newData = DanMachiAssetLibrary.Instance.Characters.First(c => c.Category == data.Category && c.Id == data.Id && c.Costume == CostumeDropdown.options[value].text); | ||||
|         var chara = DanMachiModelBuilder.Instance.SpawnCharacter(newData); | ||||
|         chara.PastePose(pose, PoseLoadOptions.All()); | ||||
|     } | ||||
|  | ||||
|     public void SelectAnimationSet(int value) | ||||
|     { | ||||
|         if(value == 0) | ||||
|         { | ||||
|             AnimationDropdown.SetOptions(null); | ||||
|             return; | ||||
|         } | ||||
|         var data = _selectedChara.Data; | ||||
|         var animSet = DanMachiAssetLibrary.Instance.Animations.First(a => a.Category == data.Category && a.Id == data.Id && a.Costume == AnimationSetDropdown.options[value].text); | ||||
|  | ||||
|         _selectedChara.LoadAnimationSet(animSet); | ||||
|  | ||||
|         AnimationDropdown.SetOptions(_selectedChara.Animations.Select(a=>a.name).ToList(), true); | ||||
|     } | ||||
|  | ||||
|     public void SelectAnimation(int value) | ||||
|     { | ||||
|         if (value == 0) | ||||
|         { | ||||
|             _selectedChara.PlayAnimation((AnimationClip)null); | ||||
|             return; | ||||
|         } | ||||
|         else | ||||
|         { | ||||
|             _selectedChara.PlayAnimation(_selectedChara.Animations[value-1]); | ||||
|         } | ||||
|     } | ||||
| } | ||||
							
								
								
									
										11
									
								
								Assets/DanMachi/Scripts/UI/CharacterPanel.cs.meta
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										11
									
								
								Assets/DanMachi/Scripts/UI/CharacterPanel.cs.meta
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,11 @@ | ||||
| fileFormatVersion: 2 | ||||
| guid: 26a550c137eb4ff42aebcf10b11d5626 | ||||
| MonoImporter: | ||||
|   externalObjects: {} | ||||
|   serializedVersion: 2 | ||||
|   defaultReferences: [] | ||||
|   executionOrder: 0 | ||||
|   icon: {instanceID: 0} | ||||
|   userData:  | ||||
|   assetBundleName:  | ||||
|   assetBundleVariant:  | ||||
		Reference in New Issue
	
	Block a user