using System.Collections.Generic; using UnityEngine; using System.IO; using MU3; using System; using UnityEngine.UI; public class Main : MonoBehaviour { #region var private GameObject chara; private GameObject weapon; private GameObject weaponL; private GameObject weaponR; private GameObject attachment; private RuntimeAnimatorController originAnimator; private string basePath = "F:/Ongeki_Unity/ab/"; public Dictionary> charaAssetsNames = new Dictionary>(); public Dictionary> weaponAssetsNames = new Dictionary>(); public Dictionary> attachmentAssetsNames = new Dictionary>(); public Dictionary charaWeapon = new Dictionary(); public Dictionary attachmentPosition = new Dictionary(); #endregion void Start() { loadCommonAssets(); loadCharaCSV(); loadWeaponCSV(); loadAttachmentCSV(); GameObject.Find("charaDropdown").GetComponent().value = 14; } #region Init private void loadCommonAssets() { DirectoryInfo TheFolder = new DirectoryInfo(Application.streamingAssetsPath + "/common"); foreach (FileInfo NextFile in TheFolder.GetFiles()) { if (!NextFile.FullName.EndsWith(".meta")) { AssetBundle.LoadFromFile(NextFile.FullName); // Debug.Log(NextFile.FullName); } } } private void loadCharaCSV() { var options = new List(); var lines = CSVReader.ReadCSV(Application.streamingAssetsPath + "/data/chara.csv"); foreach (var line in lines) { charaWeapon[line[0]] = line[2] != string.Empty ? line[2] : null; var assets = new List(); foreach (var asset in line.GetRange(3, 7)) { if (asset != string.Empty) assets.Add(asset); } charaAssetsNames[line[0]] = assets; options.Add(new Dropdown.OptionData(line[1])); } GameObject.Find("charaDropdown").GetComponent().AddOptions(options); } private void loadWeaponCSV() { var options = new List(); var lines = CSVReader.ReadCSV(Application.streamingAssetsPath + "/data/weapon.csv"); foreach (var line in lines) { var assets = new List(); foreach (var asset in line.GetRange(2, 4)) { if (asset != string.Empty) assets.Add(asset); } weaponAssetsNames[line[0]] = assets; options.Add(new Dropdown.OptionData(line[1])); } GameObject.Find("weaponLDropdown").GetComponent().AddOptions(options); GameObject.Find("weaponRDropdown").GetComponent().AddOptions(options); } private void loadAttachmentCSV() { var options = new List(); var lines = CSVReader.ReadCSV(Application.streamingAssetsPath + "/data/attachment.csv"); foreach (var line in lines) { var assets = new List(); foreach (var asset in line.GetRange(3, 2)) { if (asset != string.Empty) assets.Add(asset); } attachmentAssetsNames[line[0]] = assets; attachmentPosition[line[0]] = line[2]; options.Add(new Dropdown.OptionData(line[1])); } GameObject.Find("attachmentDropdown").GetComponent().AddOptions(options); } #endregion #region Chara public void removeChara() { if (attachment != null) Destroy(attachment); } public void loadChara(string charaName) { removeChara(); removeWeapon(); removeAttachment(); var assetNames = new List(); charaAssetsNames.TryGetValue(charaName, out assetNames); loadPrefab(charaName, assetNames, ref chara); if (charaWeapon[charaName] != null) loadWeapon(charaWeapon[charaName]); } #endregion # region Weapon public void removeWeapon() { if (weapon != null) Destroy(weapon); if (weaponL != null) Destroy(weaponL); if (weaponR != null) Destroy(weaponR); } public void loadWeapon(string weaponName) { removeWeapon(); var assetNames = new List(); weaponAssetsNames.TryGetValue(weaponName, out assetNames); loadPrefab(weaponName, assetNames, ref weapon); } public void loadWeaponL(string weaponName) { if (weapon != null) Destroy(weapon); var assetNames = new List(); weaponAssetsNames.TryGetValue(weaponName, out assetNames); loadPrefab(weaponName, assetNames, ref weaponL); Transform parent = Utils.findChildRecursive(chara.transform, "pos_l_weapon"); if (parent != null) { for (int i = parent.childCount -1; i >=0 ; i--) Destroy(parent.GetChild(i).gameObject); Transform childL = Utils.findChildRecursive(weaponL.transform, "l_weapon"); Transform childR = Utils.findChildRecursive(weaponL.transform, "r_weapon"); if (childL != null) { addWeaponButtonListener(childL.gameObject.AddComponent(), "L"); childL.SetParent(parent, true); } if (childR != null) { addWeaponButtonListener(childR.gameObject.AddComponent(), "L"); childR.SetParent(parent, true); } } } public void loadWeaponR(string weaponName) { if (weapon != null) Destroy(weapon); var assetNames = new List(); weaponAssetsNames.TryGetValue(weaponName, out assetNames); loadPrefab(weaponName, assetNames, ref weaponR); Transform parent = Utils.findChildRecursive(chara.transform, "pos_r_weapon"); if (parent != null) { for (int i = parent.childCount -1; i >=0 ; i--) Destroy(parent.GetChild(i).gameObject); Transform childL = Utils.findChildRecursive(weaponR.transform, "l_weapon"); Transform childR = Utils.findChildRecursive(weaponR.transform, "r_weapon"); if (childL != null) { addWeaponButtonListener(childL.gameObject.AddComponent(), "R"); childL.SetParent(parent, true); } if (childR != null) { addWeaponButtonListener(childR.gameObject.AddComponent(), "R"); childR.SetParent(parent, true); } } } #endregion #region Attachment public void removeAttachment() { if (attachment != null) Destroy(attachment); } public void loadAttachment(string attachmentName) { var assetNames = new List(); attachmentAssetsNames.TryGetValue(attachmentName, out assetNames); if (attachmentPosition[attachmentName] == "右手") { loadPrefab(attachmentName, assetNames, ref weaponR); Transform parent = Utils.findChildRecursive(chara.transform, "pos_r_weapon"); if (parent != null) { for (int i = parent.childCount -1; i >=0 ; i--) Destroy(parent.GetChild(i).gameObject); weaponR.transform.SetParent(parent, false); } } else { removeAttachment(); loadPrefab(attachmentName, assetNames, ref attachment); Transform parent = Utils.findChildRecursive(chara.transform, "spine_a"); if (parent != null) attachment.transform.SetParent(parent, false); attachment.transform.rotation = attachment.transform.localRotation; if (attachmentName == "at_020102") { attachment.transform.localPosition = new Vector3(0, 0.1f, 0); attachment.transform.localRotation = Quaternion.Euler(0, -7f, -30); } } } #endregion #region Animator public void loadAnimator(int i) { if (i == 0) { chara.GetComponent().runtimeAnimatorController = originAnimator; } else { string[] names = { "", "ch_000000_01_002", "ch_000000_01_003", "ch_000000_02_002", "ch_000000_02_003", "ch_000000_03_002", "ch_000000_03_003", "ch_000000_04_002", "ch_000000_04_003", "ch_000000_05_002", "ch_000000_05_003", "ch_000000_06_002", "ch_000000_06_003", }; var animatorName = names[i]; var bundle = AssetBundle.LoadFromFile(basePath + animatorName); var aoc = bundle.LoadAsset(animatorName); if (originAnimator == null) originAnimator = chara.GetComponent().runtimeAnimatorController; chara.GetComponent().runtimeAnimatorController = aoc; bundle.Unload(false); } } #endregion #region Internal void addWeaponButtonListener(RemoveAnimationTransfrom rat, string LR) { GameObject.Find(string.Format("weapon{0}x", LR)).GetComponent