You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

553 lines
20 KiB

using System.Collections.Generic;
using UnityEngine;
using System.IO;
using MU3;
using System;
using UnityEngine.UI;
public class Main : MonoBehaviour
{
#region var
public GameObject chara;
private GameObject weapon;
private GameObject weaponL;
private GameObject weaponR;
private GameObject attachment;
private RuntimeAnimatorController originAnimator;
private SkinnedMeshRenderer charaFaceRender;
private MotionEffectManager charaMotion;
private int faceOverride = 0;
private string basePath;
public Dictionary<string, List<string>> charaAssetsNames = new Dictionary<string, List<string>>();
public Dictionary<string, List<string>> weaponAssetsNames = new Dictionary<string, List<string>>();
public Dictionary<string, List<string>> attachmentAssetsNames = new Dictionary<string, List<string>>();
public Dictionary<string, string> charaWeapon = new Dictionary<string, string>();
public Dictionary<string, string> attachmentPosition = new Dictionary<string, string>();
#endregion
void Start()
{
basePath = FolderPicker.GetPath();
loadCommonAssets();
loadCharaCSV();
loadWeaponCSV();
loadAttachmentCSV();
GameObject.Find("charaDropdown").GetComponent<Dropdown>().value = 14;
}
#region Init
private void loadCommonAssets()
{
var lines = CSVReader.ReadCSV(Application.streamingAssetsPath + "/data/common.csv");
foreach (var line in lines) AssetBundle.LoadFromFile(basePath + line[0]);
}
private void loadCharaCSV()
{
var options = new List<Dropdown.OptionData>();
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<string>();
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<Dropdown>().AddOptions(options);
}
private void loadWeaponCSV()
{
var options = new List<Dropdown.OptionData>();
var lines = CSVReader.ReadCSV(Application.streamingAssetsPath + "/data/weapon.csv");
foreach (var line in lines)
{
var assets = new List<string>();
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<Dropdown>().AddOptions(options);
GameObject.Find("weaponRDropdown").GetComponent<Dropdown>().AddOptions(options);
}
private void loadAttachmentCSV()
{
var options = new List<Dropdown.OptionData>();
var lines = CSVReader.ReadCSV(Application.streamingAssetsPath + "/data/attachment.csv");
foreach (var line in lines)
{
var assets = new List<string>();
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<Dropdown>().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<string>();
charaAssetsNames.TryGetValue(charaName, out assetNames);
loadPrefab(charaName, assetNames, ref chara);
if (charaWeapon[charaName] != null) loadWeapon(charaWeapon[charaName]);
originAnimator = chara.GetComponent<Animator>().runtimeAnimatorController;
charaFaceRender = chara.GetComponentsInChildren<SkinnedMeshRenderer>()[1];
charaMotion = chara.GetComponent<MotionEffectManager>();
charaMotion.isAttached = true;
charaMotion.attachmentMotionIndex = 0;
charaMotion.initialize();
}
#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<string>();
weaponAssetsNames.TryGetValue(weaponName, out assetNames);
loadPrefab(weaponName, assetNames, ref weapon);
}
public void loadWeaponL(string weaponName)
{
if (weapon != null) Destroy(weapon);
var assetNames = new List<string>();
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");
removeWeaponButtonListener("L");
if (childL != null)
{
addWeaponButtonListener(childL.gameObject.AddComponent<RemoveAnimationTransfrom>(), "L");
childL.SetParent(parent, true);
}
if (childR != null)
{
addWeaponButtonListener(childR.gameObject.AddComponent<RemoveAnimationTransfrom>(), "L");
childR.SetParent(parent, true);
}
}
}
public void loadWeaponR(string weaponName)
{
if (weapon != null) Destroy(weapon);
var assetNames = new List<string>();
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");
removeWeaponButtonListener("R");
if (childL != null)
{
addWeaponButtonListener(childL.gameObject.AddComponent<RemoveAnimationTransfrom>(), "R");
childL.SetParent(parent, true);
}
if (childR != null)
{
addWeaponButtonListener(childR.gameObject.AddComponent<RemoveAnimationTransfrom>(), "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<string>();
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 (weapon != null) Destroy(weapon);
if (i == 0)
{
chara.GetComponent<Animator>().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<AnimatorOverrideController>(animatorName);
chara.GetComponent<Animator>().runtimeAnimatorController = aoc;
bundle.Unload(false);
}
}
#endregion
#region Face
public void setFace(int i)
{
GameObject.Find("faceDropdown").GetComponent<Dropdown>().value = i;
faceOverride = i;
}
#endregion
#region Model Internal
void removeWeaponButtonListener(string LR)
{
GameObject.Find(string.Format("weapon{0}x", LR)).GetComponent<Button>().onClick.RemoveAllListeners();
GameObject.Find(string.Format("weapon{0}y", LR)).GetComponent<Button>().onClick.RemoveAllListeners();
GameObject.Find(string.Format("weapon{0}z", LR)).GetComponent<Button>().onClick.RemoveAllListeners();
GameObject.Find(string.Format("weapon{0}p", LR)).GetComponent<Button>().onClick.RemoveAllListeners();
GameObject.Find(string.Format("weapon{0}m", LR)).GetComponent<Button>().onClick.RemoveAllListeners();
}
void addWeaponButtonListener(RemoveAnimationTransfrom rat, string LR)
{
GameObject.Find(string.Format("weapon{0}x", LR)).GetComponent<Button>().onClick.AddListener(rat.xReverse);
GameObject.Find(string.Format("weapon{0}y", LR)).GetComponent<Button>().onClick.AddListener(rat.yReverse);
GameObject.Find(string.Format("weapon{0}z", LR)).GetComponent<Button>().onClick.AddListener(rat.zReverse);
GameObject.Find(string.Format("weapon{0}p", LR)).GetComponent<Button>().onClick.AddListener(rat.zPlus);
GameObject.Find(string.Format("weapon{0}m", LR)).GetComponent<Button>().onClick.AddListener(rat.zMinus);
}
void loadPrefab(string prefabName, List<string> assetNames, ref GameObject model)
{
if (model != null) Destroy(model);
List<AssetBundle> bundles = new List<AssetBundle>();
foreach (var assetName in assetNames)
{
var assetPath = basePath + assetName;
if (File.Exists(assetPath))
{
var bundle = AssetBundle.LoadFromFile(assetPath);
bundles.Add(bundle);
}
}
var prefabAssetPath = basePath + prefabName;
if (File.Exists(prefabAssetPath))
{
var bundle = AssetBundle.LoadFromFile(prefabAssetPath);
bundles.Add(bundle);
model = (GameObject)Instantiate(bundle.LoadAsset(prefabName));
}
Animator animator = model.GetComponent<Animator>();
if (animator) animator.cullingMode = AnimatorCullingMode.AlwaysAnimate;
foreach (var bundle in bundles) bundle.Unload(false);
}
#endregion
#region Action
public void setState(int i)
{
if (i == 6) removeWeapon();
if (i == 2 || i == 3) rotate180();
else rotate0();
SetInteger("State", i);
}
public void setRoom(int i)
{
setState(6);
string[] names = {
"RoomAwake",
"RoomPresent", // not implement
"RoomFavorite",
"RoomJoy",
"RoomFaint"
};
setTrigger(names[i]);
}
public void setTurn(float i)
{
setState(1);
setFloat("Turn", i);
}
public void setDamageScale(float i)
{
setState(1);
setFloat("Damage_Scale", i);
}
public void setDamage()
{
setState(1);
setTrigger("Damage");
}
public void setHolding(bool i)
{
setState(1);
setBool("Holding", i);
}
public void setAttack(int i)
{
setState(1);
if (i == 1 || i == 2 || i == 3) setFace(1);
//if (i == 0 || i == 1 || i == 2 || i == 3) charaMotion.TriggerEffect("StartEffect_attack_a_001");
string[] names = {
"Attack_A",
"Attack_A_002",
"Attack_A_003",
"Attack_A_004",
"Attack_B"
};
setTrigger(names[i]);
}
public void setBattleStartBoss()
{
rotate180();
setTrigger("BattleStartBoss");
setTrigger("EventStep");
}
public void setBattleStartPlayer()
{
rotate180();
setTrigger("BattleStartPlayer");
setTrigger("EventStep");
Invoke("EventStep", 0.2f);
Invoke("EventStep", 5f);
}
public void setSpeed(float value)
{
if (chara.GetComponent<Animator>() != null)
chara.GetComponent<Animator>().speed = value;
if (weapon && weapon.GetComponent<Animator>() != null)
weapon.GetComponent<Animator>().speed = value;
if (weaponL && weaponL.GetComponent<Animator>() != null)
weaponL.GetComponent<Animator>().speed = value;
if (weaponR && weaponR.GetComponent<Animator>() != null)
weaponR.GetComponent<Animator>().speed = value;
GameObject.Find("speedinput").GetComponent<InputField>().text = String.Format("{0:0.00}", value);
}
#endregion
#region Action Internal
void setTrigger(string triggerName)
{
if (chara.GetComponent<Animator>() != null)
chara.GetComponent<Animator>().SetTrigger(Animator.StringToHash(triggerName));
if (weapon && weapon.GetComponent<Animator>() != null)
weapon.GetComponent<Animator>().SetTrigger(Animator.StringToHash(triggerName));
if (weaponL && weaponL.GetComponent<Animator>() != null)
weaponL.GetComponent<Animator>().SetTrigger(Animator.StringToHash(triggerName));
if (weaponR && weaponR.GetComponent<Animator>() != null)
weaponR.GetComponent<Animator>().SetTrigger(Animator.StringToHash(triggerName));
}
void SetInteger(string triggerName, int value)
{
if (chara.GetComponent<Animator>() != null)
chara.GetComponent<Animator>().SetInteger(Animator.StringToHash(triggerName), value);
if (weapon && weapon.GetComponent<Animator>() != null)
weapon.GetComponent<Animator>().SetInteger(Animator.StringToHash(triggerName), value);
if (weaponL && weaponL.GetComponent<Animator>() != null)
weaponL.GetComponent<Animator>().SetInteger(Animator.StringToHash(triggerName), value);
if (weaponR && weaponR.GetComponent<Animator>() != null)
weaponR.GetComponent<Animator>().SetInteger(Animator.StringToHash(triggerName), value);
}
void setFloat(string triggerName, float value)
{
if (chara.GetComponent<Animator>() != null)
chara.GetComponent<Animator>().SetFloat(Animator.StringToHash(triggerName), value);
if (weapon && weapon.GetComponent<Animator>() != null)
weapon.GetComponent<Animator>().SetFloat(Animator.StringToHash(triggerName), value);
if (weaponL && weaponL.GetComponent<Animator>() != null)
weaponL.GetComponent<Animator>().SetFloat(Animator.StringToHash(triggerName), value);
if (weaponR && weaponR.GetComponent<Animator>() != null)
weaponR.GetComponent<Animator>().SetFloat(Animator.StringToHash(triggerName), value);
}
void setBool(string triggerName, bool value)
{
if (chara.GetComponent<Animator>() != null)
chara.GetComponent<Animator>().SetBool(Animator.StringToHash(triggerName), value);
if (weapon && weapon.GetComponent<Animator>() != null)
weapon.GetComponent<Animator>().SetBool(Animator.StringToHash(triggerName), value);
if (weaponL && weaponL.GetComponent<Animator>() != null)
weaponL.GetComponent<Animator>().SetBool(Animator.StringToHash(triggerName), value);
if (weaponR && weaponR.GetComponent<Animator>() != null)
weaponR.GetComponent<Animator>().SetBool(Animator.StringToHash(triggerName), value);
}
void EventStep()
{
setTrigger("EventStep");
}
void rotate180()
{
chara.transform.rotation = Quaternion.Euler(0, 180, 0);
if (weapon != null) weapon.transform.rotation = Quaternion.Euler(0, 180, 0);
if (attachment != null) attachment.transform.rotation = Quaternion.Euler(0, 180, 0);
}
void rotate0()
{
chara.transform.rotation = Quaternion.Euler(0, 0, 0);
if (weapon != null) weapon.transform.rotation = Quaternion.Euler(0, 0, 0);
if (attachment != null) attachment.transform.rotation = Quaternion.Euler(0, 0, 0);
}
#endregion
void Update()
{
if (Input.GetKeyDown(KeyCode.Alpha1)) setState(1);
else if (Input.GetKeyDown(KeyCode.Alpha2)) setState(2);
else if (Input.GetKeyDown(KeyCode.Alpha3)) setState(3);
else if (Input.GetKeyDown(KeyCode.Alpha4)) setState(4);
else if (Input.GetKeyDown(KeyCode.Alpha5)) setState(5);
else if (Input.GetKeyDown(KeyCode.Alpha6)) setState(6);
else if (Input.GetKeyDown(KeyCode.V)) setRoom(0);
else if (Input.GetKeyDown(KeyCode.B)) setRoom(2);
else if (Input.GetKeyDown(KeyCode.N)) setRoom(3);
else if (Input.GetKeyDown(KeyCode.M)) setRoom(4);
else if (Input.GetKeyDown(KeyCode.A)) setAttack(0);
else if (Input.GetKeyDown(KeyCode.S)) setAttack(1);
else if (Input.GetKeyDown(KeyCode.D)) setAttack(2);
else if (Input.GetKeyDown(KeyCode.F)) setAttack(3);
else if (Input.GetKeyDown(KeyCode.G)) setAttack(4);
else if (Input.GetKeyDown(KeyCode.X)) setDamage();
else if (Input.GetKeyDown(KeyCode.J)) setBattleStartPlayer();
else if (Input.GetKeyDown(KeyCode.K)) setBattleStartBoss();
else if (Input.GetKeyDown(KeyCode.Space))
GameObject.Find("stopbutton").GetComponent<Button>().onClick.Invoke();
else if (Input.GetKeyDown(KeyCode.Z))
{
Toggle t = GameObject.Find("holdingtoggle").GetComponent<Toggle>();
t.isOn = !t.isOn;
}
// if (Input.GetMouseButtonDown(0)) {}
}
void LateUpdate()
{
if (faceOverride != 0)
{
float u = (faceOverride == 1 || faceOverride == 3 ? 0f : 0.5f);
float v = (faceOverride == 1 || faceOverride == 2 ? 0f : 0.5f);
MaterialPropertyBlock block = new MaterialPropertyBlock();
charaFaceRender.GetPropertyBlock(block);
block.SetVector("_DecalTex_ST", new Vector4(1f, 1f, u, v));
charaFaceRender.SetPropertyBlock(block);
}
}
}