Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
transrateZcsv_txt_To_TransformZdata_asset.cs
using UnityEngine;
using UnityEngine.UI;
using UnityEditor;
using UnityEditor.Animations;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text.RegularExpressions;
using System.Linq;
using System;
using UnityEngine;
using UnityEditor;
using UnityEditorInternal;
//using System.Reflection;
public class Transrate_Z_Data_Information
{
public TransformZdata.StateIdEnum zDataType;
public int[] zTransIntArray;
}
//public class SdDataToEffectPrefabMaker : MonoBehaviour {
/// <summary>
/// ///移動値をTransZcsv.txtをTransformZdataに変換しておくクラス。
/// </summary>
public class transrateZcsv_txt_To_TransformZdata_asset : EditorWindow
{
static TextAsset inputTextAsset;
void OnGUI ()
{
GUILayout.Label ("TransZcsv.text To TransformZdata.asset", EditorStyles.boldLabel);
GUILayout.Space (10f);
inputTextAsset = EditorGUILayout.ObjectField ("TextAsset", inputTextAsset, typeof(TextAsset), false) as TextAsset;
if (inputTextAsset == null) {
GUILayout.Label ("Set TextAsset .");
}
//go = Selection.activeGameObject;
//FolderName = go.name;
//GUILayout.Label ("SD Data Folder To Effect Folder and Prefab", EditorStyles.boldLabel);
GUILayout.Space (20f);
if (GUILayout.Button ("TransZcsv.text To TransformZdata.asset", GUILayout.Width (300f))) {
TransZcsvToTransformZdataButtonDown(inputTextAsset);
}
}
public static List<Transrate_Z_Data_Information> Transrate_Z_Data_Information_Arr;
public static string CreateAssetPathAndName = "";
public static void TransZcsvToTransformZdataButtonDown(TextAsset csvAsset)//ボタン押されたら
{
///////////////////////////////////////
//string outputStrArr
Transrate_Z_Data_Information_Arr = new List<Transrate_Z_Data_Information>();//初期化
///////////////////////////////////////
if (Assert(csvAsset != null, "Need to set CSV Text"))
{
return;
}
string csv = csvAsset.text;
Debug.Log("csvAsset=" + csvAsset);
Debug.LogWarning("ParceCSV(csv)ーーーーーーーーーーーーーーーーーーー開始");
ParceCSV(csv);
Debug.LogWarning("ParceCSV(csv)ーーーーーーーーーーーーーーーーーーー終了");
// Debug.Log("===============AnimationClipArr======================Start");
for (int i = 0; i < 10; i++) {
//Debug.Log("AnimationClipArr ["+i+"]="+AnimationClipArr[i]);
}
// Debug.Log("===============AnimationClipArr======================End");
CreateAssetPathAndName = TransZ_CreateAsset<TransformZdata>("Assets/Tools/Motion/Editor");
Debug.Log(" TransZcsvToTransformZdataButtonDown() ### CreateAssetPathAndName=" + CreateAssetPathAndName);
TransformZdata transformZdataAsset = (TransformZdata)AssetDatabase.LoadAssetAtPath(CreateAssetPathAndName, typeof(TransformZdata));
Debug.Log(" TransZcsvToTransformZdataButtonDown() ### transformZdataAsset" + transformZdataAsset);
writeTransZ(transformZdataAsset, CreateAssetPathAndName);
AssetDatabase.Refresh ();
}
/// <summary>
/// ボタン押されたらCSV処理終わったら関数から直接呼ばれるScriptableObjectに構造体と配列 書き込むクラス
/// </summary>
/// <param name="transformZdataAsset"></param>
/// <param name="path"></param>
public static void writeTransZ(TransformZdata transformZdataAsset, string path)
{
Debug.Log(" writeTransZ(transformZdataAsset= " + transformZdataAsset + " path= " + path + ")");
// TransformZdata.StateIdEnum xEnumList()
List<TransformZdata.StateIdEnum> MotionEnumList = new List<TransformZdata.StateIdEnum>();
for (int i = 0; i < Transrate_Z_Data_Information_Arr.Count; i++)
{
Transrate_Z_Data_Information info = Transrate_Z_Data_Information_Arr[i];
MotionEnumList.Add(info.zDataType);
///http://kan-kikuchi.hatenablog.com/entry/MonsterDataMaker
TransformZdata.Param param = new TransformZdata.Param();
param.stateIdEnum = info.zDataType;
param.ArrName = info.zDataType.ToString();
param.intArr = info.zTransIntArray;
transformZdataAsset.list.Add(param);
}// for (int i = 0; i < Transrate_Z_Data_Information_Arr.Count; i++)
//保存
ScriptableObject obj = AssetDatabase.LoadAssetAtPath(path, typeof(ScriptableObject)) as ScriptableObject;
EditorUtility.SetDirty(obj);
Debug.Log(path + "を作成");
AssetDatabase.ImportAsset(path);
AssetDatabase.Refresh();
}
/// <summary>
/// ボタン押されたら関数にCSVをわたされたらパースする関数
/// </summary>
/// <param name="csv"></param>
public static void ParceCSV(string csv)//ファイルごと
{
// split to lines
string[] lines = csv.Split('\n');
//Debug.Log("[]lines = "+ []lines);
char[] trims = { '\r', ' ' };
Debug.Log("trims = " + trims);
Debug.Log("lines.Length; = " + lines.Length);
for (int i = 0; i < lines.Length; i++)
{
string s = lines[i].Trim(trims);
ParseLine(s);//行ごと
}
// return clips.ToArray();
}
/// <summary>
/// CSVの行を処理する
/// </summary>
/// <param name="line"></param>
public static void ParseLine(string line)
{
if(line != "")
{
string[] lineArr = line.Split(","[0]);
Debug.Log("ParseLine(lineArr.Length= " + lineArr.Length + ")");
string motionName = lineArr[0];
// MotionNameToType(motionName);
string[] zValueArr = new string[lineArr.Length - 0];
System.Array.Copy(lineArr, 1, zValueArr, 0, lineArr.Length - 1);
Debug.Log("ParseLine(motionName= " + motionName + ")");
Debug.Log("ParseLine(zValueArr= " + string.Join(",", zValueArr) + ")");
Transrate_Z_Data_Information myTransrate_Z_Data_Information = new Transrate_Z_Data_Information();
TransformZdata.StateIdEnum motionType = MotionNameToType(motionName);
myTransrate_Z_Data_Information.zDataType = motionType;
/////////////////////////////////////////////////////DataConstDammy.cs/////////////////////////////// private static DataMotionKey MotionKeyListLineParser(string line)
List<int> keyList = new List<int>();
for (int loopMotionKey = 0; loopMotionKey < zValueArr.Length; ++loopMotionKey)
{
if (string.IsNullOrEmpty(zValueArr[loopMotionKey]) == false)
{
int result = 0;
if (int.TryParse(zValueArr[loopMotionKey], out result))
{
keyList.Add(result);
}
}
}
/////////////////////////////////////////////////////DataConstDammy.cs/////////////////////////////// private static DataMotionKey MotionKeyListLineParser(string line)
// motionkey.MoveList = keyList.ToArray();
int[] zIntArr = keyList.ToArray();
Debug.Log("ParseLine(zIntArr= " + DebugIntArray(zIntArr) + ")");
//myTransrate_Z_Data_Information.zDataType
myTransrate_Z_Data_Information.zTransIntArray = keyList.ToArray();
Debug.Log("");
Debug.Log("myTransrate_Z_Data_Information.zDataType = " + myTransrate_Z_Data_Information.zDataType);
Debug.Log("myTransrate_Z_Data_Information.zTransIntArray = " + DebugIntArray( myTransrate_Z_Data_Information.zTransIntArray));
Transrate_Z_Data_Information_Arr.Add(myTransrate_Z_Data_Information);
}//if(line != "")
}
////////////////////////////////////////////////////////////////////////////////
/// <summary>
/// //////////////////////////////////モーション名の文字列からEnumの型を返すクラス
/// </summary>
/// <param name="motionName"></param>
//[MenuItem("Tools/Motion/Develop/TransZcsv To TransformZdata EnumLoop Test2")]
//public static void MotionNameToType()
public static TransformZdata.StateIdEnum MotionNameToType(string motionName)
{
//string motionName = "motion_01Rod_003_WalkRight";
TransformZdata.StateIdEnum genaricStateIdEnum = TransformZdata.StateIdEnum.WalkFront;
//Enumのアイテムをforeachで使う https://zenmai.wordpress.com/2009/11/11/enum%E3%81%AE%E3%82%A2%E3%82%A4%E3%83%86%E3%83%A0%E3%82%92foreach%E3%81%A7%E4%BD%BF%E3%81%86%E3%80%8Cc/
foreach (TransformZdata.StateIdEnum stateIdEnumLoop in Enum.GetValues(typeof(TransformZdata.StateIdEnum)))
{
//Enum列挙型を文字列に
string stateIdEnumLoopStr = stateIdEnumLoop.ToString();
//文字列に型名が入ってるか調べる。
if (motionName.Contains(stateIdEnumLoopStr))
{
Debug.Log("stateIdEnumLoopStr= " + stateIdEnumLoopStr);
// 型名から型を取得。
//C# で列挙型への変換 http://qiita.com/tyfkda/items/f0289934fbbb1fbac82b
//stringから Enum列挙型への変換
genaricStateIdEnum = (TransformZdata.StateIdEnum)Enum.Parse(typeof(TransformZdata.StateIdEnum), stateIdEnumLoopStr);
Debug.Log("genaricStateIdEnum= " + genaricStateIdEnum);
Debug.Log("genaricStateIdEnum.GetType() = " + genaricStateIdEnum.GetType());
}
}
//1回テスト
return genaricStateIdEnum;
}
public static string DebugIntArray(int[] intarr)
{
string Debug_zIntArr_str = "";
for (int i = 0; i < intarr.Length; i++)
{
Debug_zIntArr_str = Debug_zIntArr_str + intarr[i] + ",";
}
// Debug.Log("ParseLine(zIntArr= " + Debug_zIntArr_str + ")");
return Debug_zIntArr_str;
}
/// <summary>
// This makes it easy to create, name and place unique new ScriptableObject asset files.
//CreateAsset<TransformZdata> ();
//CreateAsset<TransformZdata> ("Assets/Tools/Motion/Editor");
//public static string SaveDir;//ここに保存されるので注意
/// </summary>
public static string TransZ_CreateAsset<T>(string path= "Assets/Tools/Motion/Editor") where T : ScriptableObject
{
T asset = ScriptableObject.CreateInstance<T>();
string filename = "_TransZdata";
string folderName = "";
// folderName = path;
SaveDir = path;
folderName = "";
string filetype = ".asset";
string assetPathAndName = "";
assetPathAndName = getSavePath(folderName, filename,filetype);
Debug.Log("transrateZcsv_txt_To_TransformZdata_asset.cs ####### public static void CreateAsset<T> () where T : ScriptableObject assetPathAndName= " + assetPathAndName);
AssetDatabase.CreateAsset(asset, assetPathAndName);
AssetDatabase.SaveAssets();
AssetDatabase.Refresh();
EditorUtility.FocusProjectWindow();
Selection.activeObject = asset;
return assetPathAndName;
}
public static string SaveDir;//ここに保存されるので注意
/// <summary>
/// 保存用パスの生成
/// </summary>
/// <param name="folderName"></param>
/// <param name="filename"></param>
/// <param name="filetype"></param>
/// <returns></returns>
public static string getSavePath(string folderName, string filename, string filetype)
{
Debug.Log("----010--- getSavePath ----- folderName = "+ folderName + " filename= "+ filename+ " filetype="+ filetype);
string objectName = filename;
if (filetype == ".asset")
{
}
string dirPath = SaveDir;
Debug.Log("----020-- getSavePath -----dirPath=" + dirPath);
string path = dirPath + "/" + objectName + filetype;
string MakeDir;
if (folderName == "")
{
MakeDir = dirPath;
Debug.Log("---030--- getSavePath -----フォルダ名指定なし folderName =" + folderName + " MakeDir=" + MakeDir);
}
else
{
MakeDir = dirPath + "/" + folderName;
Debug.Log("---030--- getSavePath -----フォルダ名指定あり folderName =" + folderName + " MakeDir=" + MakeDir);
if (AssetDatabase.IsValidFolder(MakeDir))
{
Debug.Log("-- - 040-- - getSavePath---- - " + MakeDir + " フォルダありました!");
}
else {
Debug.Log("-- - 040-- - getSavePath---- - " + MakeDir + " ないのでつくりますです!");
string guid = AssetDatabase.CreateFolder(dirPath, folderName);
string newFolderPath = AssetDatabase.GUIDToAssetPath(guid);
Debug.Log("-- - 050-- - getSavePath---- - " + newFolderPath + " = newFolderPath 作りました!");
Debug.Log("-- - 060-- - getSavePath---- - " + MakeDir + " = MakeDir 作りました!");
}
}
path = MakeDir + "/" + objectName + filetype;
if (File.Exists(path))
{
for (int i = 1; ; i++)
{
//path = string.Format ("{0}/{1}({2}).asset", dirPath, objectName, i);
path = MakeDir + "/" + objectName + "_" + i + filetype;
if (!File.Exists(path))
break;
}
}
Debug.Log("------ getSavePath --------- path=" + path);
return path;
}
////////////////////////////////////////////////////////////////////////////////////
/// <summary>
/// アラート用関数
/// </summary>
/// <param name="b"></param>
/// <param name="error"></param>
/// <returns></returns>
///////////////////////////////////////////////////////////////////////////////
public static bool Assert(bool b, string error)
{
if (!b)
{
DisplayDialog("Warning", error);
return true;
}
else {
return false;
}
}
/// <summary>
/// ディスプレイダイアログ
/// </summary>
/// <param name="title"></param>
/// <param name="message"></param>
public static void DisplayDialog(string title, string message)
{
Debug.LogWarning(message);
EditorUtility.DisplayDialog(title, message, "OK");
}
//////////////////////////////////////////////////////////////////////////////////////////////////////
#region Static
/// <summary>
/// Open the tool window
/// </summary>
[MenuItem("Tools/Motion/Develop/TransZcsv To TransformZdata")]
static public void TransZcsvToTransformZdataWindow3()
{
EditorWindow.GetWindow<transrateZcsv_txt_To_TransformZdata_asset> (true, "TransZcsv To TransformZdata", true);
}
#endregion
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.